# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22536 | JAESu_gak (#40) | Young Zebra (KRIII5_YZ) | C++14 | 33 ms | 12092 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |