poj 1164 The Castle (入门深搜)

题目:

   1   2   3   4   5   6   7  

#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(Figure 1)

# = Wall
| = No wall
- = No wall

Figure 1 shows the map of a castle.Write a program that calculates 
1. how many rooms the castle has 
2. how big the largest room is 
The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls. 

Input

Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 1 (= wall to the west), 2 (= wall to the north), 4 (= wall to the east), 8 (= wall to the south). Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1. The castle always has at least two rooms.

Output

Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).

Sample Input

4
7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

Sample Output

5
9
题意:
给你两个数n,m;分别表示接下来矩阵中的格子的横竖长度,接下来是对矩阵的描述。。
分析:
简单入门深搜,主要目的是掌握深搜的基本框架。
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 int room[51][51];
 7 int color[51][51];
 8 int roomnum=0;
 9 int roomArea;
10 int maxroomArea=0;
11 void dfs(int r,int c)
12 {
13     if(color[r][c])
14         return;
15     roomArea++;
16     color[r][c]=roomnum;
17     if((room[r][c]&1)==0) dfs(r,c-1);
18     if((room[r][c]&2)==0) dfs(r-1,c);
19     if((room[r][c]&4)==0) dfs(r,c+1);
20     if((room[r][c]&8)==0) dfs(r+1,c);
21 }
22 int main()
23 {
24     int x,y;
25     cin>>x>>y;
26     for(int i=0;i<x;i++)
27         for(int j=0;j<y;j++)
28         cin>>room[i][j];
29     memset(color,0,sizeof(color));
30     for(int i=0;i<x;i++)
31         for(int j=0;j<y;j++)
32         if(!color[i][j])
33     {
34         roomnum++;
35         roomArea=0;
36         dfs(i,j);
37         maxroomArea=max(maxroomArea,roomArea);
38     }
39     cout<<roomnum<<endl;
40     cout<<maxroomArea<<endl;
41     return 0;
42 }
View Code
原文地址:https://www.cnblogs.com/forwin/p/4809964.html