#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char> (m));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cin >> grid[i][j];
}
}
vector<vector<int>> distances(n, vector<int> (m));
vector<int> dir_x = {1, -1, 0, 0}, dir_y = {0, 0, 1, -1};
deque<pair<int, int>> q;
q.push_front({0, 0});
distances[0][0] = 1;
int ans = 0;
while(q.size())
{
auto src = q.front();
q.pop_front();
for(int i = 0; i < 4; i++)
{
int x = src.first + dir_x[i], y = src.second + dir_y[i];
if(x >= 0 && x < n && y >= 0 && y < m && grid[x][y] != '.' && distances[x][y] == 0)
{
if(grid[x][y] == grid[src.first][src.second])
{
distances[x][y] = distances[src.first][src.second];
q.push_front({x, y});
}
else
{
distances[x][y] = distances[src.first][src.second] + 1;
q.push_back({x, y});
}
ans = max(ans, distances[x][y]);
}
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
1880 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
8 ms |
1584 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
824 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
1000 KB |
Output is correct |
13 |
Correct |
4 ms |
948 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
13 ms |
1884 KB |
Output is correct |
16 |
Correct |
14 ms |
1884 KB |
Output is correct |
17 |
Correct |
12 ms |
1880 KB |
Output is correct |
18 |
Correct |
9 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
860 KB |
Output is correct |
2 |
Correct |
77 ms |
9552 KB |
Output is correct |
3 |
Correct |
582 ms |
94412 KB |
Output is correct |
4 |
Correct |
150 ms |
22352 KB |
Output is correct |
5 |
Correct |
354 ms |
53332 KB |
Output is correct |
6 |
Correct |
929 ms |
108252 KB |
Output is correct |
7 |
Correct |
2 ms |
856 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
3 ms |
808 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
484 KB |
Output is correct |
13 |
Correct |
70 ms |
9664 KB |
Output is correct |
14 |
Correct |
38 ms |
5724 KB |
Output is correct |
15 |
Correct |
40 ms |
6352 KB |
Output is correct |
16 |
Correct |
30 ms |
4004 KB |
Output is correct |
17 |
Correct |
180 ms |
24296 KB |
Output is correct |
18 |
Correct |
138 ms |
23892 KB |
Output is correct |
19 |
Correct |
156 ms |
22360 KB |
Output is correct |
20 |
Correct |
125 ms |
20564 KB |
Output is correct |
21 |
Correct |
375 ms |
55344 KB |
Output is correct |
22 |
Correct |
324 ms |
53332 KB |
Output is correct |
23 |
Correct |
364 ms |
45908 KB |
Output is correct |
24 |
Correct |
332 ms |
53840 KB |
Output is correct |
25 |
Correct |
577 ms |
94432 KB |
Output is correct |
26 |
Correct |
648 ms |
122752 KB |
Output is correct |
27 |
Correct |
846 ms |
121088 KB |
Output is correct |
28 |
Correct |
947 ms |
108268 KB |
Output is correct |
29 |
Correct |
921 ms |
106568 KB |
Output is correct |
30 |
Correct |
869 ms |
110736 KB |
Output is correct |
31 |
Correct |
649 ms |
61116 KB |
Output is correct |
32 |
Correct |
768 ms |
109828 KB |
Output is correct |