#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef short int shit;
#define ff first
#define ss second
bool check(int n, int m, int y, int x){
return x>=0&&x<m&&y>=0&&y<n;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(NULL);
// freopen("odometer.in", "r", stdin);
// freopen("odometer.out", "w", stdout);
int n, m;
cin >> n >> m;
vector<vector<char>> plain(n, vector<char>(m));
vector<vector<int>> depth(n, vector<int>(m, 0));
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cin >> plain[i][j];
}
}
int dy[] = {-1,0,1,0};
int dx[] = {0,1,0,-1};
int ans = 1;
deque<pair<int,int>> dq;
dq.push_back({0,0});
depth[0][0] = 1;
while (!dq.empty()){
pair<int,int> coords = dq.front(); dq.pop_front();
ans = max(ans, depth[coords.ff][coords.ss]);
for(int i = 0; i < 4; ++i){
int yy = coords.ff+dy[i];
int xx = coords.ss+dx[i];
if(check(n,m,yy,xx)){
if(plain[yy][xx]!=plain[coords.ff][coords.ss]&plain[yy][xx]!='.'&&depth[yy][xx]==0){
dq.push_back({yy,xx});
depth[yy][xx] = depth[coords.ff][coords.ss] + 1;
}else if(plain[yy][xx]==plain[coords.ff][coords.ss]&plain[yy][xx]!='.'&&depth[yy][xx]==0){
dq.push_front({yy,xx});
depth[yy][xx] = depth[coords.ff][coords.ss];
}
}
}
}
cout << ans << '\n';
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:42:33: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
42 | if(plain[yy][xx]!=plain[coords.ff][coords.ss]&plain[yy][xx]!='.'&&depth[yy][xx]==0){
tracks.cpp:45:39: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
45 | }else if(plain[yy][xx]==plain[coords.ff][coords.ss]&plain[yy][xx]!='.'&&depth[yy][xx]==0){
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
1968 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
1372 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
1032 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
864 KB |
Output is correct |
15 |
Correct |
11 ms |
1884 KB |
Output is correct |
16 |
Correct |
13 ms |
1756 KB |
Output is correct |
17 |
Correct |
8 ms |
1916 KB |
Output is correct |
18 |
Correct |
6 ms |
1376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
860 KB |
Output is correct |
2 |
Correct |
43 ms |
9208 KB |
Output is correct |
3 |
Correct |
293 ms |
87032 KB |
Output is correct |
4 |
Correct |
72 ms |
20824 KB |
Output is correct |
5 |
Correct |
180 ms |
49128 KB |
Output is correct |
6 |
Correct |
637 ms |
101884 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
1052 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
660 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
44 ms |
9220 KB |
Output is correct |
14 |
Correct |
26 ms |
5468 KB |
Output is correct |
15 |
Correct |
18 ms |
5980 KB |
Output is correct |
16 |
Correct |
23 ms |
3932 KB |
Output is correct |
17 |
Correct |
117 ms |
22608 KB |
Output is correct |
18 |
Correct |
79 ms |
22352 KB |
Output is correct |
19 |
Correct |
72 ms |
20816 KB |
Output is correct |
20 |
Correct |
65 ms |
19444 KB |
Output is correct |
21 |
Correct |
168 ms |
50928 KB |
Output is correct |
22 |
Correct |
180 ms |
49236 KB |
Output is correct |
23 |
Correct |
229 ms |
42576 KB |
Output is correct |
24 |
Correct |
163 ms |
49740 KB |
Output is correct |
25 |
Correct |
457 ms |
87236 KB |
Output is correct |
26 |
Correct |
341 ms |
116836 KB |
Output is correct |
27 |
Correct |
466 ms |
116816 KB |
Output is correct |
28 |
Correct |
635 ms |
101808 KB |
Output is correct |
29 |
Correct |
617 ms |
97784 KB |
Output is correct |
30 |
Correct |
563 ms |
105552 KB |
Output is correct |
31 |
Correct |
574 ms |
56656 KB |
Output is correct |
32 |
Correct |
441 ms |
106760 KB |
Output is correct |