1.链接地址:

http://bailian.openjudge.cn/practice/2815/

http://poj.org/problem?id=1164

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述

1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(图 1)

# = Wall
| = No wall
- = No wall

图1是一个城堡的地形图。请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。城堡被分割成mn(m≤50,n≤50)个方块,每个方块可以有0~4面墙。
输入
程序从标准输入设备读入数据。第一行是两个整数,分别是南北向、东西向的方块数。在接下来的输入行里,每个方块用一个数字(0≤p≤50)描 述。用一个数字表示方块周围的墙,1表示西墙,2表示北墙,4表示东墙,8表示南墙。每个方块用代表其周围墙的数字之和表示。城堡的内墙被计算两次,方块 (1,1)的南墙同时也是方块(2,1)的北墙。输入的数据保证城堡至少有两个房间。
输出
城堡的房间数、城堡中最大房间所包括的方块数。结果显示在标准输出设备上。
样例输入
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 
样例输出
5
9
来源
1164

3.思路:

深搜

4.代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 int idx_x[] = {-1,0,1,0};
 7 int idx_y[] = {0,-1,0,1};
 8 
 9 int m,n;
10 
11 int f(int **arr,int i,int j)
12 {
13     //cout << "f(" << i << "," << j << ")" << endl;
14     
15     /*for(int y = 0; y < m; ++y)
16     {
17         for(int x = 0; x < n; ++x)
18         {
19             cout << arr[y][x] << " ";
20         }
21         cout << endl;
22     }*/
23 
24     if(i < 0 || i >= m || j < 0 || j >= n || arr[i][j] == -1) return 0;
25     int value = arr[i][j];
26 
27     int res = 1;
28     int k;
29     arr[i][j] = -1;
30     for(k = 0; k < 4; ++k)
31     {
32         if(value % 2 == 0) res += f(arr,i + idx_y[k],j + idx_x[k]);
33         value /= 2;
34     }
35     return res;
36 }
37 
38 int main()
39 {
40     //freopen("C://input.txt","r",stdin);
41 
42     int i,j;
43 
44     //int m,n;
45     cin >> m >> n;
46 
47     int **arr = new int*[m];
48     for(i = 0; i < m; ++i) arr[i] = new int[n];
49 
50     for(i = 0; i < m; ++i)
51     {
52         for(j = 0; j < n; ++j)
53         {
54             cin >> arr[i][j];
55         }
56     }
57 
58 
59     int count = 0;
60     int max = 0;
61     int num;
62     for(i = 0; i < m; ++i)
63     {
64         for(j = 0; j < n; ++j)
65         {
66             if(arr[i][j] != -1)
67             {
68                 num = f(arr,i,j);
69                 //cout << "num=" << num << endl;
70                 if(max < num) max = num;
71                 ++count;
72             }
73         }
74     }
75     cout << count << endl;
76     cout << max << endl;
77 
78     for(i = 0; i < m; ++i) delete [] arr[i];
79     delete [] arr;
80 
81     return 0;
82 }

 

posted on 2014-02-20 21:50  mobileliker  阅读(627)  评论(0编辑  收藏  举报