poj 1164城堡问题(DFS)

#include <iostream>
#include <cstring>
using namespace std;

int R,C; //行列数
int rooms[60][60];
int color[60][60]; //方块是否被标记过
int maxRoomArea=0,roomNum=0;
int roomArea;

void DFS(int i,int j)
{
    if(color[i][j]) //该点已经被访问过
        return;
    roomArea++;
    color[i][j]=roomNum;  //标记该点为已访问过
    if((rooms[i][j] & 1)==0)
        DFS(i,j-1);     //西面没有墙,向west走
    if((rooms[i][j] & 2)==0)
        DFS(i-1,j);     //北面没有墙,向north走
    if((rooms[i][j] & 4)==0)
        DFS(i,j+1);     //东面没有墙,向east走
    if((rooms[i][j] & 8)==0)
        DFS(i+1,j);     //南面没有墙,向south走
}

int main()
{
    cin>>R>>C;
    for(int i=1; i<=R; i++)
        for(int j=1; j<=C; j++)
            cin>>rooms[i][j];
    memset(color,0,sizeof(color));
    for(int i=1; i<=R; i++)
        for(int j=1; j<=C; j++)
        {
            if(color[i][j]==0)
            {
                roomArea=0;
                roomNum++;
                DFS(i,j);

                maxRoomArea=max(roomArea,maxRoomArea);
            }
        }

    cout<<roomNum<<endl;
    cout<<maxRoomArea<<endl;



}

原文地址:https://www.cnblogs.com/zhanyeye/p/9746092.html