1 条题解

  • 0
    @ 2025-8-24 21:21:26

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar 1124828077ccj
    **

    搬运于2025-08-24 21:21:25,当前版本为作者最后更新于2016-06-04 15:29:49,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    胰,怎么没人说DFS,谁说DFS过不了,看以下AC代码,搜索过了标记一下,下次搜索时就不用在找一次了

    
    #include<cstdio>
    #include<cstring>
    int n,m,ans[100002],x,y,f[1002][1002];
    char s[1002][1002];
    void dfs(int r,int c,int z,int lll){
        if (r<0 || r>=n || c<0 || c>=n || f[r][c]!=-1 || s[r][c]-'0'!=z)return;
        f[r][c]=lll;ans[lll]++;
        dfs(r-1,c,!z,lll);dfs(r+1,c,!z,lll);dfs(r,c-1,!z,lll);dfs(r,c+1,!z,lll);
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for (int i=0;i<n;i++)
        scanf("%s",s[i]);
        memset(f,-1,sizeof(f));
        for (int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);x--;y--;
            if (f[x][y]==-1)dfs(x,y,s[x][y]-'0',i);else ans[i]=ans[f[x][y]];
        }
        for (int i=0;i<m;i++)
        printf("%d\n",ans[i]);
        return 0;
    }
    
    
    • 1

    信息

    ID
    143
    时间
    1000ms
    内存
    128MiB
    难度
    3
    标签
    递交数
    2
    已通过
    2
    上传者