This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define Y8o "main"
#define maxn (int) 4005
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define f first
#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
int m, n;
char a[maxn][maxn];
int dp[maxn][maxn];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
bool exist(int u, int v) {
return 1 <= u && u <= m && 1 <= v && v <= n && a[u][v] != '.';
}
void bfs() {
dp[1][1] = 1;
deque<pii> q;
q.push_back({ 1, 1 });
while(q.size()) {
int i, j; tie(i, j) = q.front();
q.pop_front();
for(int t = 0; t <= 3; t ++) {
int u = i + dx[t], v = j + dy[t];
if(exist(u, v) && dp[u][v] == 0) {
if(a[u][v] == a[i][j]) {
dp[u][v] = dp[i][j];
q.push_front({ u, v });
}
else {
dp[u][v] = dp[i][j] + 1;
q.push_back({ u, v });
}
}
}
}
}
void solve() {
cin >> m >> n ;
for(int i = 1; i <= m; i ++) for(int j = 1; j <= n; j ++)
cin >> a[i][j] ;
bfs();
int ans = 0;
for(int i = 1; i <= m; i ++) for(int j = 1; j <= n; j ++)
ans = max(ans, dp[i][j]);
cout << ans;
}
int main() {
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'void iof()':
tracks.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |