#include<stdio.h>
#include<string.h>
int n, m, nn, mm, ans[410][410];
int str, stc, cnt;
char arr[1210][1210];
bool chk[1210][1210], f_chk[1210][1210];
void dfs(int r, int c)
{
if(r%n==str%n && c%m==stc%m)
{
if(r!=str || c!=stc){ cnt=-1; return; }
}
++cnt;
chk[r][c] = true;
if(arr[r][c]==arr[r-1][c] && r>1 && !chk[r-1][c]) dfs(r-1, c);
if(cnt==-1) return;
if(arr[r][c]==arr[r+1][c] && r<nn && !chk[r+1][c]) dfs(r+1, c);
if(cnt==-1) return;
if(arr[r][c]==arr[r][c-1] && c>1 && !chk[r][c-1]) dfs(r, c-1);
if(cnt==-1) return;
if(arr[r][c]==arr[r][c+1] && c<mm && !chk[r][c+1]) dfs(r, c+1);
if(cnt==-1) return;
return;
}
void fill_cnt(int r, int c)
{
int x, y;
f_chk[r][c]=true;
x = r%n?r%n:n;
y = c%m?c%m:m;
ans[x][y] = cnt;
if(arr[r][c]==arr[r-1][c] && r>1 && !f_chk[r-1][c]) fill_cnt(r-1, c);
if(arr[r][c]==arr[r+1][c] && r<nn && !f_chk[r+1][c]) fill_cnt(r+1, c);
if(arr[r][c]==arr[r][c-1] && c>1 && !f_chk[r][c-1]) fill_cnt(r, c-1);
if(arr[r][c]==arr[r][c+1] && c<mm && !f_chk[r][c+1]) fill_cnt(r, c+1);
}
int main()
{
int i, j;
scanf("%d%d",&n, &m);
for(i=1; i<=n; ++i)
{
scanf("%s", arr[i]+1);
strcpy(arr[1205]+1, arr[i]+1);
strcpy(arr[i]+m+1, arr[1205]+1);
strcpy(arr[i]+m+m+1, arr[1205]+1);
}
for(i=n+1; i<=n*3; ++i)
{
strcpy(arr[i]+1, arr[i-n]+1);
}
nn = n*3;
mm = m*3;
for(i=n+1; i<=n*2; ++i)
{
for(j=m+1; j<=m*2; ++j)
{
if(ans[i-n][j-m]!=0) continue;
str = i;
stc = j;
cnt = 0;
dfs(i, j);
fill_cnt(i, j);
}
}
for(i=1; i<=n; ++i)
{
for(j=1; j<=m; ++j) printf("%d ", ans[i][j]);
printf("\n");
}
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:40:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n, &m);
^
YZ.cpp:43:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", arr[i]+1);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
29 ms |
10140 KB |
Output is partially correct |
2 |
Incorrect |
26 ms |
10228 KB |
Extra information in the output file |
3 |
Halted |
0 ms |
0 KB |
- |