#include <bits/stdc++.h>
using namespace std;
char dat[404][404];
bool visit[404][404];
int ans[404][404];
pair<int, int> depth[404][404];
vector<pair<int, int>> components;
bool infinite;
int n, m;
int dr[4][2] =
{
1, 0,
0, 1,
-1, 0,
0, -1
};
void dfs(int x, int y, int dx, int dy)
{
visit[x][y] = true;
depth[x][y] = {dx, dy};
components.emplace_back(x, y);
for (int i = 0; i < 4; i++)
{
int nx = x + dr[i][0];
int ny = y + dr[i][1];
int ndx = dx, ndy = dy;
if (nx < 0)
{
--ndx;
nx += n;
}
if (nx >= n)
{
++ndx;
nx -= n;
}
if (ny < 0)
{
--ndy;
ny += m;
}
if (ny >= m)
{
++ndy;
ny -= m;
}
if (dat[x][y] != dat[nx][ny]) continue;
if (visit[nx][ny])
{
if (depth[nx][ny] != make_pair(ndx, ndy)) infinite = true;
continue;
}
dfs(nx, ny, ndx, ndy);
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%s", dat[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (visit[i][j]) continue;
infinite = false;
components.clear();
dfs(i, j, 0, 0);
for (auto &e : components)
{
if (infinite) ans[e.first][e.second] = -1;
else ans[e.first][e.second] = components.size();
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) printf("%d ", ans[i][j]);
printf("\n");
}
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:68:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
YZ.cpp:72:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", dat[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
14412 KB |
Output is correct |
2 |
Correct |
19 ms |
9932 KB |
Output is correct |
3 |
Correct |
23 ms |
11652 KB |
Output is correct |
4 |
Correct |
19 ms |
6848 KB |
Output is correct |
5 |
Correct |
23 ms |
6080 KB |
Output is correct |
6 |
Correct |
16 ms |
5492 KB |
Output is correct |
7 |
Correct |
19 ms |
8284 KB |
Output is correct |
8 |
Correct |
13 ms |
9192 KB |
Output is correct |
9 |
Correct |
16 ms |
8288 KB |
Output is correct |
10 |
Correct |
23 ms |
8272 KB |
Output is correct |
11 |
Correct |
19 ms |
16192 KB |
Output is correct |
12 |
Correct |
16 ms |
16180 KB |
Output is correct |
13 |
Correct |
23 ms |
9644 KB |
Output is correct |
14 |
Correct |
19 ms |
9680 KB |
Output is correct |
15 |
Correct |
33 ms |
7964 KB |
Output is correct |
16 |
Correct |
26 ms |
9412 KB |
Output is correct |
17 |
Correct |
19 ms |
10172 KB |
Output is correct |
18 |
Correct |
13 ms |
10172 KB |
Output is correct |
19 |
Correct |
23 ms |
4252 KB |
Output is correct |
20 |
Correct |
23 ms |
9396 KB |
Output is correct |
21 |
Correct |
19 ms |
11816 KB |
Output is correct |
22 |
Correct |
19 ms |
11668 KB |
Output is correct |
23 |
Correct |
16 ms |
8220 KB |
Output is correct |
24 |
Correct |
23 ms |
9456 KB |
Output is correct |
25 |
Correct |
33 ms |
8428 KB |
Output is correct |
26 |
Correct |
0 ms |
4252 KB |
Output is correct |
27 |
Correct |
0 ms |
4252 KB |
Output is correct |
28 |
Correct |
0 ms |
4444 KB |
Output is correct |
29 |
Correct |
0 ms |
4444 KB |
Output is correct |
30 |
Correct |
0 ms |
4252 KB |
Output is correct |
31 |
Correct |
0 ms |
4252 KB |
Output is correct |
32 |
Correct |
0 ms |
4252 KB |
Output is correct |
33 |
Correct |
0 ms |
4252 KB |
Output is correct |
34 |
Correct |
0 ms |
4252 KB |
Output is correct |
35 |
Correct |
0 ms |
4252 KB |
Output is correct |
36 |
Correct |
0 ms |
4856 KB |
Output is correct |
37 |
Correct |
0 ms |
4700 KB |
Output is correct |
38 |
Correct |
0 ms |
4252 KB |
Output is correct |
39 |
Correct |
0 ms |
4724 KB |
Output is correct |
40 |
Correct |
0 ms |
4792 KB |
Output is correct |