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 ll long long
using namespace std;
const int nm = 1e6;
bool isValid(int x, int y, int n, int m, string snow[]){
if(x>=0 && x<n && y>=0 && y<m && snow[x][y]!='.')return true;
return false;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll h, w;
cin >> h >> w;
string snow[h];
for(int i = 0; i<h; i++)cin >> snow[i];
vector<vector<int>> depth(h, vector<int>(w));
deque<pair<int, int>> q;
q.push_back({0, 0});
depth[0][0] =1;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int ans = 0;
while(!q.empty()){
int i = q.front().first;
int j = q.front().second;
ans = max(depth[i][j], ans);
q.pop_front();
for(int k = 0; k<4; k++){
int x = i + dx[k], y = j + dy[k];
if(isValid(x, y, h, w, snow) && depth[x][y]==0){
if(snow[x][y]==snow[i][j]){
q.push_front({x, y});
depth[x][y] = depth[i][j];
}
else{
q.push_back({x, y});
depth[x][y] = depth[i][j] +1;
}
}
}
}
cout << ans << endl;
}
Compilation message (stderr)
tracks.cpp: In function 'int main()':
tracks.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |