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;
}
时间: 2024-11-03 10:40:35

openjudge-2815:城堡问题【简单DFS】的相关文章

Bailian2815 城堡问题【DFS】

2815:城堡问题 总时间限制: 1000ms 内存限制: 65536kB 描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | # # # # # #---#####---#####---#####---# 3 # | | # # # # # #---#########---#####---#---# 4 # # | | | | # # ###

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

Red and Black(简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12519    Accepted Submission(s): 7753 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int

hdu 4739Zhuge Liang&#39;s Mines(简单dfs,需要注意重点)

Zhuge Liang's Mines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1166    Accepted Submission(s): 505 Problem Description In the ancient three kingdom period, Zhuge Liang was the most famous

poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

POJ 1979 Red and Black (简单dfs)

题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w,h; char a[22][22]; int dir[4][2] = {-1,0,1,0,0,-1,0,1}; int ans = 0; void dfs(int x,int y){ if(x < 0 || x >= h || y < 0 || y &g

2815:城堡问题-DFS

题目链接:http://bailian.openjudge.cn/practice/2815/ #include<iostream> #include<string> #include<cstdio> #include<cstring> #include<queue> #include<map> #include<stack> #include<set> #include<vector> #incl

2815:城堡问题 输入处理,连通块

http://bailian.openjudge.cn/practice/2815?lang=en_US 题意:给你一个地图(二进制处理) 问有几个连通块,与最大连通块的面积. 题解:处理输入时,用四个if位运算判断.连通块模板处理. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<