#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
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 |
1 |
Correct |
9 ms |
3676 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
2776 KB |
Output is correct |
5 |
Correct |
2 ms |
1372 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
484 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
1628 KB |
Output is correct |
13 |
Correct |
2 ms |
1452 KB |
Output is correct |
14 |
Correct |
2 ms |
1604 KB |
Output is correct |
15 |
Correct |
8 ms |
3676 KB |
Output is correct |
16 |
Correct |
9 ms |
3716 KB |
Output is correct |
17 |
Correct |
7 ms |
3676 KB |
Output is correct |
18 |
Correct |
5 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1136 KB |
Output is correct |
2 |
Correct |
39 ms |
21332 KB |
Output is correct |
3 |
Correct |
296 ms |
212052 KB |
Output is correct |
4 |
Correct |
80 ms |
50004 KB |
Output is correct |
5 |
Correct |
200 ms |
119632 KB |
Output is correct |
6 |
Correct |
620 ms |
226272 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
45 ms |
21200 KB |
Output is correct |
14 |
Correct |
24 ms |
12632 KB |
Output is correct |
15 |
Correct |
20 ms |
13660 KB |
Output is correct |
16 |
Correct |
19 ms |
8960 KB |
Output is correct |
17 |
Correct |
107 ms |
54144 KB |
Output is correct |
18 |
Correct |
83 ms |
53620 KB |
Output is correct |
19 |
Correct |
82 ms |
50164 KB |
Output is correct |
20 |
Correct |
65 ms |
46160 KB |
Output is correct |
21 |
Correct |
171 ms |
123732 KB |
Output is correct |
22 |
Correct |
203 ms |
119404 KB |
Output is correct |
23 |
Correct |
213 ms |
102928 KB |
Output is correct |
24 |
Correct |
170 ms |
120660 KB |
Output is correct |
25 |
Correct |
313 ms |
216148 KB |
Output is correct |
26 |
Correct |
380 ms |
215648 KB |
Output is correct |
27 |
Correct |
483 ms |
225452 KB |
Output is correct |
28 |
Correct |
581 ms |
226260 KB |
Output is correct |
29 |
Correct |
606 ms |
224508 KB |
Output is correct |
30 |
Correct |
508 ms |
223200 KB |
Output is correct |
31 |
Correct |
422 ms |
136436 KB |
Output is correct |
32 |
Correct |
409 ms |
226492 KB |
Output is correct |