openjudge-2815:城堡问题【简单DFS】

#include<iostream>

#include<cstring>

using namespace std;

#define Size 50

int rooms[Size+1][Size+1];

bool visited[Size+1][Size+1];// 每个格子的状态 访问与否

int RoomArea; //  城堡中每一个房间的面积



void DFS( int x, int y )

{

        if( visited[x][y] )

            return ;

        visited[x][y] = 1;

        RoomArea++;

        if( (rooms[x][y] & 1) == 0 ) DFS( x, y-1 );   //向西搜索

        if( (rooms[x][y] & 2) == 0 ) DFS( x-1, y );   //向北

        if( (rooms[x][y] & 4) == 0  ) DFS( x, y+1 ); //向东

        if( (rooms[x][y] & 8) == 0 ) DFS( x+1, y );  //向南

}



int main()

{

        int row, column, RoomNum, MaxArea;

        cin>>row>>column;

        memset( visited, 0, sizeof(visited) );



        for( int i=0; i<row; i++ )

            for( int j=0; j<column; j++ )

                cin>>rooms[i][j];



        RoomNum = MaxArea = 0;

        for( int i=0; i<row; i++ )

            for( int j=0; j<column; j++ )//遍历每个格子 求得RoomNum

                {

                    if( visited[i][j] )

                        continue;



                    RoomNum++;

                    RoomArea = 0;

                    DFS( i, j );

                    MaxArea = max( RoomArea, MaxArea );

                }

        cout<<RoomNum<<endl;

        cout<<MaxArea<<endl;



        return 0;

}

 

你可能感兴趣的:(open)