POJ-1164 The Castle ------------DFS

The Castle
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5536   Accepted: 3116

Description

     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
 1 /* 功能Function Description:     POJ--1164
 2    开发环境Environment:          DEV C++ 4.9.9.1
 3    技术特点Technique:
 4    版本Version:
 5    作者Author:                   可笑痴狂
 6    日期Date:                      20120814
 7    备注Notes:                    ----DFS
 8          本题难点在于初始的判断
 9           根据下边的式子容易判断在每个方向上是否存在墙
10           map[i][j]= 1*x + 2*y + 4*m +8*n  (注意到存在倍数关系,可以根据取余运算判断)
11 */
12 #include<cstdio>
13 
14 int map[51][51];
15 int max,sum,r,c,t;
16 bool visit[51][51];
17 
18 void DFS(int i,int j)
19 {
20     /*
21     if(i<0||j<0||i>=r||j>=c)  //边界判断本题可以不考虑,因为城堡的最外边都是墙,在下边的搜索条件中不会被搜索到
22         return;
23     */
24     if(visit[i][j])
25         return;
26     else
27     {
28         visit[i][j]=true;
29         ++t;
30         if(map[i][j]<8)    //说明没有南墙
31             DFS(i+1,j);
32         else
33             map[i][j]%=8;
34         if(map[i][j]<4)    //说明没有东墙
35             DFS(i,j+1);
36         else
37             map[i][j]%=4;
38         if(map[i][j]<2)    //说明没有北墙
39             DFS(i-1,j);
40         if(map[i][j]%2==0) //说明没有西墙
41             DFS(i,j-1);
42     }
43 }
44 
45 int main()
46 {
47     int i,j;
48     while(scanf("%d%d",&r,&c)!=EOF)
49     {
50         for(i=0;i<r;++i)
51             for(j=0;j<c;++j)
52             {
53                 scanf("%d",&map[i][j]);
54                 visit[i][j]=false;
55             }
56         max=0;
57         sum=0;
58         for(i=0;i<r;++i)
59         {
60             for(j=0;j<c;++j)
61             {
62                 if(visit[i][j])
63                     continue;
64                 t=0;
65                 DFS(i,j);
66                 if(t>max)
67                     max=t;
68                 if(t)
69                     ++sum;
70             }
71         }
72         printf("%d\n%d\n",sum,max);
73     }
74     return 0;
75 }

 

你可能感兴趣的:(cast)