您的位置:首页 > 其它

深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

2015-02-18 10:59 363 查看
将问题的各状态之间的转移关系描述
为一个图,则深度优先搜索遍历整个图的
框架为:
Dfs(v) {
if( v 访问过)
return;
将v标记为访问过;
对和v相邻的每个点u: Dfs(u);
}
int main() {
while(在图中能找到未访问过的点 k)
Dfs(k);
}

例题:

POJ1164 The Castle

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

Source

IOI 1994

百练2815 城堡问题

描述

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≤15)描述。用一个数字表示方块周围的墙,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


解题思路
 对每一个 方块,深度优先搜索,从而给这个方
块能够到达的所有位置染色。最后统计一共用
了几种颜色,以及每种颜色的数量。
 比如
1 1 2 2 3 3 3
1 1 1 2 3 4 3
1 1 1 5 3 5 3
1 5 5 5 5 5 3
 从而一共有5个房间,最大的房间(1)占据9
个格子

// By LYLtim
// 2015.2.16

#include <iostream>

using namespace std;

int m, n, roomNum = 0, maxRoomAero = 0, curRoomAera;
int map[50][50], color[50][50] = {0};

void dfs(int i, int j) {
color[i][j] = roomNum;
curRoomAera++;
if (((map[i][j] & 1) == 0) && (j > 0) && !color[i][j-1]) dfs(i, j-1);
if (((map[i][j] & 2) == 0) && (i > 0) && !color[i-1][j]) dfs(i-1, j);
if (((map[i][j] & 4) == 0) && (j+1 < n) && !color[i][j+1]) dfs(i, j+1);
if (((map[i][j] & 8) == 0) && (i+1 < m) && !color[i+1][j]) dfs(i+1, j);
}

int main()
{
cin >> m >> n;
for( int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> map[i][j];
for( int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (!color[i][j]) {
roomNum++;
curRoomAera = 0;
dfs(i, j);
if (curRoomAera > maxRoomAero)
maxRoomAero = curRoomAera;
}
cout << roomNum << endl << maxRoomAero;
}


用栈模拟递归

// By LYLtim
// 2015.2.17

#include <iostream>
#include <stack>

using namespace std;

int m, n, roomNum = 0, curRoomAera;
int map[50][50], color[50][50] = {0};

struct Room
{
int x, y;
Room(int x, int y):x(x),y(y) {}
};

void dfs(int startX, int startY) {
stack<Room> stack;
stack.push(Room(startX, startY));
int x, y;
while (!stack.empty()) {
Room topRoom = stack.top();
x = topRoom.x;
y = topRoom.y;
if (color[x][y])
stack.pop();
else {
curRoomAera++;
color[x][y] = roomNum;
if (((map[x][y] & 1) == 0) && (y > 0) && !color[x][y-1])
stack.push(Room(x, y-1));
if (((map[x][y] & 2) == 0) && (x > 0) && !color[x-1][y])
stack.push(Room(x-1, y));
if (((map[x][y] & 4) == 0) && (y+1 < n) && !color[x][y+1])
stack.push(Room(x, y+1));
if (((map[x][y] & 8) == 0) && (x+1 < m) && !color[x+1][y])
stack.push(Room(x+1, y));
}
}
}

int main()
{
int maxRoomAero = 0;
cin >> m >> n;
for( int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> map[i][j];
for( int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (!color[i][j]) {
roomNum++;
curRoomAera = 0;
dfs(i, j);
if (curRoomAera > maxRoomAero)
maxRoomAero = curRoomAera;
}
cout << roomNum << endl << maxRoomAero <<;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: