#include<stdio.h>
#include<string.h>
int n, m, ans[410][410], str, stc, cnt;
int dx[4]={-1, 0, 0, 1}, dy[4]={0, 1, -1, 0};
char arr[410][410];
bool chk[410][410], f_chk[410][410];
void dfs(int r, int c, int br, int bc)
{
++cnt;
chk[r][c]=true;
for(int i=0; i<4; ++i)
{
int x = dx[i]+r;
int y = dy[i]+c;
int rr=br, cc=bc;
if(x==n) { ++rr; x-=n; }
if(x==-1){ --rr; x+=n; }
if(y==m) { ++cc; y-=m; }
if(y==-1){ --cc; y+=m; }
if(arr[x][y]!=arr[r][c]) continue;
if(x==str && y==stc && !(rr==0 && cc==0))
{
cnt=-1;
return;
}
if(!chk[x][y])
dfs(x, y, rr, cc);
if(cnt==-1) return;
}
}
void fill_cnt(int r, int c)
{
f_chk[r][c]=true;
ans[r][c]=cnt;
for(int i=0; i<4; ++i)
{
int x = dx[i]+r;
int y = dy[i]+c;
x%=n; y%=m;
if(x<0) x+=n;
if(y<0) y+=m;
if(arr[x][y]!=arr[r][c]) continue;
if(!f_chk[x][y])
fill_cnt(x, y);
}
}
int main()
{
int i, j;
scanf("%d%d", &n, &m);
for(i=0; i<n; ++i)scanf("%s", arr[i]);
for(i=0; i<n; ++i)
{
for(j=0; j<m; ++j)
{
if(ans[i][j]) continue;
str=i;
stc=j;
cnt=0;
dfs(i, j, 0, 0);
fill_cnt(i, j);
}
}
for(i=0; i<n; ++i)
{
for(j=0; j<m; ++j) printf("%d ", ans[i][j]);
printf("\n");
}
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:50: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:51:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=0; i<n; ++i)scanf("%s", arr[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
5192 KB |
Output is correct |
2 |
Correct |
29 ms |
5596 KB |
Output is correct |
3 |
Partially correct |
29 ms |
7640 KB |
Output is partially correct |
4 |
Partially correct |
29 ms |
4948 KB |
Output is partially correct |
5 |
Partially correct |
23 ms |
3604 KB |
Output is partially correct |
6 |
Correct |
26 ms |
3060 KB |
Output is correct |
7 |
Correct |
26 ms |
4664 KB |
Output is correct |
8 |
Partially correct |
26 ms |
7964 KB |
Output is partially correct |
9 |
Correct |
23 ms |
4664 KB |
Output is correct |
10 |
Correct |
23 ms |
6004 KB |
Output is correct |
11 |
Correct |
29 ms |
12092 KB |
Output is correct |
12 |
Correct |
19 ms |
12088 KB |
Output is correct |
13 |
Partially correct |
19 ms |
7668 KB |
Output is partially correct |
14 |
Partially correct |
26 ms |
4708 KB |
Output is partially correct |
15 |
Correct |
19 ms |
5172 KB |
Output is correct |
16 |
Partially correct |
29 ms |
5616 KB |
Output is partially correct |
17 |
Partially correct |
26 ms |
7080 KB |
Output is partially correct |
18 |
Correct |
26 ms |
7080 KB |
Output is correct |
19 |
Correct |
33 ms |
2264 KB |
Output is correct |
20 |
Correct |
23 ms |
5600 KB |
Output is correct |
21 |
Correct |
23 ms |
8724 KB |
Output is correct |
22 |
Correct |
33 ms |
6964 KB |
Output is correct |
23 |
Correct |
26 ms |
3232 KB |
Output is correct |
24 |
Correct |
26 ms |
3960 KB |
Output is correct |
25 |
Correct |
29 ms |
4636 KB |
Output is correct |
26 |
Correct |
0 ms |
2264 KB |
Output is correct |
27 |
Correct |
0 ms |
2264 KB |
Output is correct |
28 |
Correct |
0 ms |
2268 KB |
Output is correct |
29 |
Correct |
0 ms |
2272 KB |
Output is correct |
30 |
Correct |
0 ms |
2264 KB |
Output is correct |
31 |
Correct |
0 ms |
2264 KB |
Output is correct |
32 |
Partially correct |
0 ms |
2264 KB |
Output is partially correct |
33 |
Partially correct |
0 ms |
2264 KB |
Output is partially correct |
34 |
Correct |
0 ms |
2264 KB |
Output is correct |
35 |
Correct |
0 ms |
2264 KB |
Output is correct |
36 |
Correct |
0 ms |
2580 KB |
Output is correct |
37 |
Correct |
0 ms |
2448 KB |
Output is correct |
38 |
Correct |
0 ms |
2264 KB |
Output is correct |
39 |
Correct |
0 ms |
2468 KB |
Output is correct |
40 |
Correct |
0 ms |
2596 KB |
Output is correct |