#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'
using namespace std;
int N, M;
vector<vector<bool>> vis;
vector<vector<char>> grid;
queue<pii> q;
int mvt[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void dfs(int r, int c, char let)
{
if (r < 0 || c < 0 || r >= N || c >= M || grid[r][c] == '.' || vis[r][c])
return;
if (grid[r][c] != let)
{
q.push({r, c});
return;
}
vis[r][c] = true;
for (int i = 0; i < 4; i++)
{
int rx = r + mvt[i][0], cx = c + mvt[i][1];
dfs(rx, cx, let);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
grid.resize(N, vector<char>(M));
vis.resize(N, vector<bool>(M, false));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
cin >> grid[i][j];
q.push({0, 0});
int res = 0;
while (!q.empty())
{
int sz = q.size();
for (int _ = 0; _ < sz; _++)
{
int r, c;
tie(r, c) = q.front();
q.pop();
dfs(r, c, grid[r][c]);
}
++res;
}
cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
1884 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
644 KB |
Output is correct |
11 |
Correct |
2 ms |
948 KB |
Output is correct |
12 |
Correct |
6 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
3 ms |
600 KB |
Output is correct |
15 |
Correct |
12 ms |
1116 KB |
Output is correct |
16 |
Correct |
16 ms |
1112 KB |
Output is correct |
17 |
Correct |
9 ms |
1136 KB |
Output is correct |
18 |
Correct |
9 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
46 ms |
3988 KB |
Output is correct |
3 |
Correct |
309 ms |
34536 KB |
Output is correct |
4 |
Correct |
75 ms |
8652 KB |
Output is correct |
5 |
Correct |
255 ms |
19536 KB |
Output is correct |
6 |
Correct |
760 ms |
114584 KB |
Output is correct |
7 |
Correct |
2 ms |
1112 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
54 ms |
3936 KB |
Output is correct |
14 |
Correct |
27 ms |
2396 KB |
Output is correct |
15 |
Correct |
20 ms |
2648 KB |
Output is correct |
16 |
Correct |
24 ms |
1884 KB |
Output is correct |
17 |
Correct |
117 ms |
9364 KB |
Output is correct |
18 |
Correct |
82 ms |
9196 KB |
Output is correct |
19 |
Correct |
69 ms |
8448 KB |
Output is correct |
20 |
Correct |
68 ms |
7760 KB |
Output is correct |
21 |
Correct |
177 ms |
20304 KB |
Output is correct |
22 |
Correct |
247 ms |
19752 KB |
Output is correct |
23 |
Correct |
250 ms |
17004 KB |
Output is correct |
24 |
Correct |
183 ms |
19984 KB |
Output is correct |
25 |
Correct |
346 ms |
34384 KB |
Output is correct |
26 |
Correct |
891 ms |
781460 KB |
Output is correct |
27 |
Correct |
822 ms |
507020 KB |
Output is correct |
28 |
Correct |
785 ms |
114700 KB |
Output is correct |
29 |
Correct |
756 ms |
107828 KB |
Output is correct |
30 |
Correct |
751 ms |
210852 KB |
Output is correct |
31 |
Correct |
636 ms |
23380 KB |
Output is correct |
32 |
Correct |
755 ms |
374044 KB |
Output is correct |