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>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
char grid[n][m];
ll dist[n][m];
int visited[n][m];
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> grid[i][j];
dist[i][j] = 0;
visited[i][j] = 0;
}
}
deque<pair<int, int>> q;
dist[0][0] = 1;
q.push_back({0, 0});
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
ll ans = 1;
while (!q.empty()){
auto x = q.front();
q.pop_front();
ans = max(ans, dist[x.first][x.second]);
for (auto y : dir){
int nx = x.first + y.first;
int ny = x.second + y.second;
if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
if (grid[nx][ny] == '.') continue;
if (dist[nx][ny] != 0) continue;
if (grid[nx][ny] == grid[x.first][x.second]){
dist[nx][ny] = dist[x.first][x.second];
q.push_front({nx, ny});
}else{
dist[nx][ny] = dist[x.first][x.second]+1;
q.push_back({nx, ny});
}
}
}
cout << ans << endl;
}
Compilation message (stderr)
tracks.cpp: In function 'int main()':
tracks.cpp:16:9: warning: variable 'visited' set but not used [-Wunused-but-set-variable]
16 | int visited[n][m];
| ^~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |