#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define REV(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
using namespace std;
int n, m;
#define MAXN 4005
char a[MAXN][MAXN];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};
int dist[MAXN][MAXN];
bool valid(int i, int j){
return (i >= 1 && i <= n && j >= 1 && j <= m);
}
void solve(){
cin >> n >> m;
FOR(i, 1, n){
FOR(j, 1, m){
cin >> a[i][j];
}
}
deque<pair<int, int>> dq;
memset(dist, -1, sizeof dist);
dist[1][1] = 1;
dq.push_back({1, 1});
while (dq.size()){
int i, j; tie(i, j) = dq.front();
dq.pop_front();
FOR(k, 0, 3){
int ii = i + dy[k];
int jj = j + dx[k];
if (valid(ii, jj) && dist[ii][jj] == -1 && a[ii][jj] != '.'){
int w = (a[ii][jj] != a[i][j]);
dist[ii][jj] = dist[i][j] + w;
if (w == 0) dq.push_front({ii, jj});
else dq.push_back({ii, jj});
}
}
}
int res = 0;
FOR(i, 1, n) FOR(j, 1, m) res = max(res, dist[i][j]);
cout << res;
}
#define task "test"
int32_t main(){
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
solve();
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'int32_t main()':
tracks.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |