#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int H, W;
vector<string> meadow;
vector<vector<int>> val;
queue<pair<int, int>> q;
int switches;
char animal;
bool cnt = false;
void dfs(int row, int col)
{
if (row < 0 || col < 0 || row == H || col == W || meadow[row][col] == '.')
return;
if (val[row][col] != 0 && cnt)
return;
cnt = true;
if (animal != meadow[row][col])
{
q.push({row, col});
val[row][col] = switches + 1;
return;
}
val[row][col] = switches;
dfs(row + 1, col);
dfs(row - 1, col);
dfs(row, col + 1);
dfs(row, col - 1);
}
int main()
{
cin.tie(NULL) -> sync_with_stdio(false);
cin >> H >> W;
meadow.resize(H);
val.resize(H, vector<int>(W));
for (auto& i : meadow)
cin >> i;
q.push({0, 0});
val[0][0] = 1;
while (!q.empty())
{
auto [row, col] = q.front();
q.pop();
switches = val[row][col];
animal = meadow[row][col];
cnt = false;
dfs(row, col);
}
int ans = 0;
for (int i = 0; i < H; ++i)
for (int j = 0; j < W; ++j)
ans = max(ans, val[i][j]);
cout << ans << endl;
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1884 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 |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
976 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 |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
2120 KB |
Output is correct |
16 |
Correct |
8 ms |
2024 KB |
Output is correct |
17 |
Correct |
5 ms |
1884 KB |
Output is correct |
18 |
Correct |
6 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
29 ms |
10088 KB |
Output is correct |
3 |
Correct |
156 ms |
97364 KB |
Output is correct |
4 |
Correct |
43 ms |
23124 KB |
Output is correct |
5 |
Correct |
126 ms |
54864 KB |
Output is correct |
6 |
Correct |
533 ms |
154848 KB |
Output is correct |
7 |
Correct |
1 ms |
1112 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1232 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
27 ms |
10032 KB |
Output is correct |
14 |
Correct |
16 ms |
5976 KB |
Output is correct |
15 |
Correct |
10 ms |
6492 KB |
Output is correct |
16 |
Correct |
14 ms |
4560 KB |
Output is correct |
17 |
Correct |
73 ms |
25116 KB |
Output is correct |
18 |
Correct |
47 ms |
24668 KB |
Output is correct |
19 |
Correct |
45 ms |
23224 KB |
Output is correct |
20 |
Correct |
37 ms |
21584 KB |
Output is correct |
21 |
Correct |
95 ms |
56660 KB |
Output is correct |
22 |
Correct |
136 ms |
54864 KB |
Output is correct |
23 |
Correct |
153 ms |
47448 KB |
Output is correct |
24 |
Correct |
104 ms |
55352 KB |
Output is correct |
25 |
Correct |
208 ms |
97392 KB |
Output is correct |
26 |
Runtime error |
642 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Correct |
706 ms |
399432 KB |
Output is correct |
28 |
Correct |
551 ms |
154856 KB |
Output is correct |
29 |
Correct |
546 ms |
145244 KB |
Output is correct |
30 |
Correct |
578 ms |
203752 KB |
Output is correct |
31 |
Correct |
354 ms |
62960 KB |
Output is correct |
32 |
Correct |
736 ms |
425240 KB |
Output is correct |