#include<cstdio>
int n, m;
char s[410][410];
int chk[410][410];
int dim[410][410][2];
int ans[410][410];
int cn;
int scon;
int con[200100][2];
void dfs(int x, int y, int px, int py, char z) {
if (x < 0) {
dfs(x + n, y, px - 1, py, z);
return;
}
if (x >= n) {
dfs(x - n, y, px + 1, py, z);
return;
}
if (y < 0) {
dfs(x, y + m, px, py - 1, z);
return;
}
if (y >= m) {
dfs(x, y - m, px, py + 1, z);
return;
}
if (s[x][y] != z)return;
if (chk[x][y] != 0) {
if (px == dim[x][y][0] && py == dim[x][y][1])return;
scon = 1;
return;
}
chk[x][y] = 1;
con[cn][0] = x;
con[cn][1] = y;
cn++;
dim[x][y][0] = px;
dim[x][y][1] = py;
dfs(x + 1, y, px, py, z);
dfs(x - 1, y, px, py, z);
dfs(x, y + 1, px, py, z);
dfs(x, y - 1, px, py, z);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", s[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (chk[i][j] == 0) {
cn = 0;
scon = 0;
dfs(i, j, 0, 0, s[i][j]);
for (int k = 0; k < cn; k++) {
if (scon)ans[con[k][0]][con[k][1]] = -1;
else ans[con[k][0]][con[k][1]] = cn;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", ans[i][j]);
}
printf("\n");
}
return 0;
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:45:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
YZ.cpp:47:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
9620 KB |
Output is correct |
2 |
Correct |
16 ms |
9308 KB |
Output is correct |
3 |
Correct |
16 ms |
9468 KB |
Output is correct |
4 |
Correct |
13 ms |
6708 KB |
Output is correct |
5 |
Correct |
23 ms |
6388 KB |
Output is correct |
6 |
Correct |
16 ms |
5812 KB |
Output is correct |
7 |
Correct |
19 ms |
9716 KB |
Output is correct |
8 |
Correct |
23 ms |
9808 KB |
Output is correct |
9 |
Correct |
23 ms |
9720 KB |
Output is correct |
10 |
Correct |
19 ms |
9056 KB |
Output is correct |
11 |
Correct |
19 ms |
9992 KB |
Output is correct |
12 |
Correct |
36 ms |
12428 KB |
Output is correct |
13 |
Correct |
26 ms |
8772 KB |
Output is correct |
14 |
Correct |
19 ms |
7988 KB |
Output is correct |
15 |
Correct |
29 ms |
6524 KB |
Output is correct |
16 |
Correct |
23 ms |
7440 KB |
Output is correct |
17 |
Correct |
16 ms |
7204 KB |
Output is correct |
18 |
Correct |
19 ms |
7200 KB |
Output is correct |
19 |
Correct |
16 ms |
5468 KB |
Output is correct |
20 |
Correct |
23 ms |
7376 KB |
Output is correct |
21 |
Correct |
9 ms |
10280 KB |
Output is correct |
22 |
Correct |
16 ms |
7756 KB |
Output is correct |
23 |
Correct |
19 ms |
6520 KB |
Output is correct |
24 |
Correct |
23 ms |
6480 KB |
Output is correct |
25 |
Correct |
23 ms |
8124 KB |
Output is correct |
26 |
Correct |
0 ms |
5468 KB |
Output is correct |
27 |
Correct |
0 ms |
5468 KB |
Output is correct |
28 |
Correct |
0 ms |
5476 KB |
Output is correct |
29 |
Correct |
0 ms |
5472 KB |
Output is correct |
30 |
Correct |
0 ms |
5468 KB |
Output is correct |
31 |
Correct |
0 ms |
5468 KB |
Output is correct |
32 |
Correct |
0 ms |
5468 KB |
Output is correct |
33 |
Correct |
0 ms |
5468 KB |
Output is correct |
34 |
Correct |
0 ms |
5468 KB |
Output is correct |
35 |
Correct |
0 ms |
5468 KB |
Output is correct |
36 |
Correct |
0 ms |
5680 KB |
Output is correct |
37 |
Correct |
0 ms |
5512 KB |
Output is correct |
38 |
Correct |
0 ms |
5468 KB |
Output is correct |
39 |
Correct |
0 ms |
5532 KB |
Output is correct |
40 |
Correct |
0 ms |
5632 KB |
Output is correct |