Submission #22536

#TimeUsernameProblemLanguageResultExecution timeMemory
22536JAESu_gak (#40)Young Zebra (KRIII5_YZ)C++14
2 / 7
33 ms12092 KiB
#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)

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 timeMemoryGrader output
Fetching results...