1 条题解

  • 0
    @ 2025-4-12 21:36:07

    C++ :

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int N = 60, M = 100010;
    int n, m, p;
    int a[N][N][4];
    bool b[N][N];
    int q[M][3];
    int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
    int maxx = -1, tot;
    void bfs(int x, int y){
        memset(q, 0, sizeof q);
        tot ++;
        int tail = 1, head = 0, xx, yy;
        q[1][1] = x, q[1][2] = y;
        while (head < tail){
            head ++;
            for (int i = 0; i < 4; i ++){
                xx = q[head][1] + dx[i];
                yy = q[head][2] + dy[i];
                if (xx > 0 && xx <= n && yy > 0 && yy <= m && b[xx][yy] && a[q[head][1]][q[head][2]][i] == 0){
                    tail ++;
                    b[xx][yy] = false;
                    q[tail][1] = xx;
                    q[tail][2] = yy;
                }
            }
        }
        maxx = max(tail - 1, maxx);
    }
    int main(){
        memset(b, true, sizeof b);
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= m; j ++){
                scanf("%d", &p);
                if (p >= 8) {
                    p -= 8;
                    a[i][j][1] = 1;
                }
                if (p >= 4) {
                    p -= 4;
                    a[i][j][3] = 1;
                }
                if (p >= 2){
                    p -= 2;
                    a[i][j][0] = 1;
                }
                if (p >= 1) {
                    p -= 1;
                    a[i][j][2] = 1;
                }
            }
        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= m; j ++)
                if (b[i][j]) bfs(i, j);
        printf("%d\n%d\n", tot, maxx);
        return 0;
    }
    

    Java :

    import java.util.Scanner;
    public class Main{
        static int map[][],vis[][],n,m,count;
        public static void main(String[]args){
            Scanner in=new Scanner(System.in);
            while(in.hasNextInt()){
                int n=in.nextInt();
                int m=in.nextInt();
                map=new int[n][m];
                vis=new int[n][m];
                int i,j;
                for(i=0;i<n;i++)
                    for(j=0;j<m;j++)
                        map[i][j]=in.nextInt();
                int maxroom=0,cnt=0;
                for(i=0;i<n;i++)
                    for(j=0;j<m;j++){
                        count=0;
                        dfs(i,j);
                        if(count!=0)
                            cnt++;
                        if(maxroom<count)
                            maxroom=count;
                    }
                System.out.println(cnt);
                System.out.println(maxroom);
            }
        }
        public static void dfs(int x,int y){
            if(vis[x][y]==1)
                return; //如果已经访问过,回到上一个位置
            else{
                count++;//否则,count加一统计该房间包含的方格数;并对该方格标记为已访问过。
                vis[x][y]=1;
            }
            if(map[x][y]<8)//判断是否有南墙,没有,则向下继续搜索
                dfs(x+1,y);
            else
                map[x][y]%=8;//有南墙,对8求余;向下判断。
            if(map[x][y]%8<4)//判断是否有东墙,没有,则向右继续搜索。
                dfs(x,y+1);
            else
                map[x][y]%=4;//有东墙,对4求余,继续向下判断。
            if(map[x][y]%4<2)//判断是否有北墙,没有,则向上继续搜索
                dfs(x-1,y);
            else
                map[x][y]%=2;//有北墙,对2求余,继续向下判断
            if(map[x][y]%2==0)//判读是否有西墙,没有,向左继续搜索
                dfs(x,y-1);
        }
    }
    
    • 1

    信息

    ID
    917
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者