#include <bits/stdc++.h>
using namespace std;
const int mxN=4e3;
int n, m, ans, nb[4][2]={{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
string g[mxN];
vector<array<int, 2>> tp[2];
bool a(int i, int j) {
return i>=0&&i<n&&j>=0&&j<m&&g[i][j]!='.';
}
void dfs(int i, int j) {
char pc=g[i][j];
g[i][j]='.';
for(int k=0; k<4; ++k) {
int ni=i+nb[k][0], nj=j+nb[k][1];
if(!a(ni, nj))
continue;
if(g[ni][nj]==pc)
dfs(ni, nj);
else
tp[ans&1^1].push_back({ni, nj});
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=0; i<n; ++i)
cin >> g[i];
tp[0].push_back({0, 0});
for(; tp[ans&1].size(); ++ans) {
for(array<int, 2> u : tp[ans&1])
if(a(u[0], u[1]))
dfs(u[0], u[1]);
tp[ans&1].clear();
}
cout << ans;
}
Compilation message
tracks.cpp: In function 'void dfs(int, int)':
tracks.cpp:23:10: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
tp[ans&1^1].push_back({ni, nj});
~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
1400 KB |
Output is correct |
2 |
Correct |
3 ms |
1400 KB |
Output is correct |
3 |
Correct |
3 ms |
1400 KB |
Output is correct |
4 |
Correct |
12 ms |
2496 KB |
Output is correct |
5 |
Correct |
4 ms |
2496 KB |
Output is correct |
6 |
Correct |
3 ms |
2496 KB |
Output is correct |
7 |
Correct |
2 ms |
2496 KB |
Output is correct |
8 |
Correct |
3 ms |
2496 KB |
Output is correct |
9 |
Correct |
2 ms |
2496 KB |
Output is correct |
10 |
Correct |
4 ms |
2496 KB |
Output is correct |
11 |
Correct |
4 ms |
2496 KB |
Output is correct |
12 |
Correct |
8 ms |
2496 KB |
Output is correct |
13 |
Correct |
4 ms |
2496 KB |
Output is correct |
14 |
Correct |
4 ms |
2496 KB |
Output is correct |
15 |
Correct |
16 ms |
2496 KB |
Output is correct |
16 |
Correct |
22 ms |
2740 KB |
Output is correct |
17 |
Correct |
10 ms |
2740 KB |
Output is correct |
18 |
Correct |
12 ms |
4116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4116 KB |
Output is correct |
2 |
Correct |
44 ms |
5872 KB |
Output is correct |
3 |
Correct |
229 ms |
29304 KB |
Output is correct |
4 |
Correct |
49 ms |
29304 KB |
Output is correct |
5 |
Correct |
188 ms |
29304 KB |
Output is correct |
6 |
Correct |
809 ms |
99188 KB |
Output is correct |
7 |
Correct |
3 ms |
99188 KB |
Output is correct |
8 |
Correct |
4 ms |
99188 KB |
Output is correct |
9 |
Correct |
4 ms |
99188 KB |
Output is correct |
10 |
Correct |
3 ms |
99188 KB |
Output is correct |
11 |
Correct |
3 ms |
99188 KB |
Output is correct |
12 |
Correct |
3 ms |
99188 KB |
Output is correct |
13 |
Correct |
44 ms |
99188 KB |
Output is correct |
14 |
Correct |
26 ms |
99188 KB |
Output is correct |
15 |
Correct |
17 ms |
99188 KB |
Output is correct |
16 |
Correct |
25 ms |
99188 KB |
Output is correct |
17 |
Correct |
112 ms |
99188 KB |
Output is correct |
18 |
Correct |
57 ms |
99188 KB |
Output is correct |
19 |
Correct |
48 ms |
99188 KB |
Output is correct |
20 |
Correct |
54 ms |
99188 KB |
Output is correct |
21 |
Correct |
136 ms |
99188 KB |
Output is correct |
22 |
Correct |
184 ms |
99188 KB |
Output is correct |
23 |
Correct |
222 ms |
99188 KB |
Output is correct |
24 |
Correct |
128 ms |
99188 KB |
Output is correct |
25 |
Correct |
294 ms |
99188 KB |
Output is correct |
26 |
Correct |
1243 ms |
791708 KB |
Output is correct |
27 |
Correct |
932 ms |
791708 KB |
Output is correct |
28 |
Correct |
774 ms |
791708 KB |
Output is correct |
29 |
Correct |
767 ms |
791708 KB |
Output is correct |
30 |
Correct |
833 ms |
791708 KB |
Output is correct |
31 |
Correct |
686 ms |
791708 KB |
Output is correct |
32 |
Correct |
941 ms |
791708 KB |
Output is correct |