將問題的各狀態之間的轉移關系描述
為一個圖,則深度優先搜索遍歷整個圖的
框架為:
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
百練2815 城堡問題
描述
1 2 3 4 5 6 7
#############################
1 # | # | # | | #
#####---#####---#---#####---#
2 # # | # # # # #
#---#####---#####---#####---#
3 # | | # # # # #
#---#########---#####---#---#
4 # # | | | | # #
#############################
(圖 1)
# = Wall
| = No wall
- = No wall
圖1是一個城堡的地形圖。請你編寫一個程序,計算城堡一共有多少房間,最大的房間有多大。城堡被分割成mn(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
個格子
1 // By LYLtim 2 // 2015.2.16 3 4 #include <iostream> 5 6 using namespace std; 7 8 int m, n, roomNum = 0, maxRoomAero = 0, curRoomAera; 9 int map[50][50], color[50][50] = {0}; 10 11 void dfs(int i, int j) { 12 color[i][j] = roomNum; 13 curRoomAera++; 14 if (((map[i][j] & 1) == 0) && (j > 0) && !color[i][j-1]) dfs(i, j-1); 15 if (((map[i][j] & 2) == 0) && (i > 0) && !color[i-1][j]) dfs(i-1, j); 16 if (((map[i][j] & 4) == 0) && (j+1 < n) && !color[i][j+1]) dfs(i, j+1); 17 if (((map[i][j] & 8) == 0) && (i+1 < m) && !color[i+1][j]) dfs(i+1, j); 18 } 19 20 int main() 21 { 22 cin >> m >> n; 23 for( int i = 0; i < m; i++) 24 for (int j = 0; j < n; j++) 25 cin >> map[i][j]; 26 for( int i = 0; i < m; i++) 27 for (int j = 0; j < n; j++) 28 if (!color[i][j]) { 29 roomNum++; 30 curRoomAera = 0; 31 dfs(i, j); 32 if (curRoomAera > maxRoomAero) 33 maxRoomAero = curRoomAera; 34 } 35 cout << roomNum << endl << maxRoomAero; 36 }
1 // By LYLtim 2 // 2015.2.17 3 4 #include <iostream> 5 #include <stack> 6 7 using namespace std; 8 9 int m, n, roomNum = 0, curRoomAera; 10 int map[50][50], color[50][50] = {0}; 11 12 struct Room 13 { 14 int x, y; 15 Room(int x, int y):x(x),y(y) {} 16 }; 17 18 void dfs(int startX, int startY) { 19 stack<Room> stack; 20 stack.push(Room(startX, startY)); 21 int x, y; 22 while (!stack.empty()) { 23 Room topRoom = stack.top(); 24 x = topRoom.x; 25 y = topRoom.y; 26 if (color[x][y]) 27 stack.pop(); 28 else { 29 curRoomAera++; 30 color[x][y] = roomNum; 31 if (((map[x][y] & 1) == 0) && (y > 0) && !color[x][y-1]) 32 stack.push(Room(x, y-1)); 33 if (((map[x][y] & 2) == 0) && (x > 0) && !color[x-1][y]) 34 stack.push(Room(x-1, y)); 35 if (((map[x][y] & 4) == 0) && (y+1 < n) && !color[x][y+1]) 36 stack.push(Room(x, y+1)); 37 if (((map[x][y] & 8) == 0) && (x+1 < m) && !color[x+1][y]) 38 stack.push(Room(x+1, y)); 39 } 40 } 41 } 42 43 int main() 44 { 45 int maxRoomAero = 0; 46 cin >> m >> n; 47 for( int i = 0; i < m; i++) 48 for (int j = 0; j < n; j++) 49 cin >> map[i][j]; 50 for( int i = 0; i < m; i++) 51 for (int j = 0; j < n; j++) 52 if (!color[i][j]) { 53 roomNum++; 54 curRoomAera = 0; 55 dfs(i, j); 56 if (curRoomAera > maxRoomAero) 57 maxRoomAero = curRoomAera; 58 } 59 cout << roomNum << endl << maxRoomAero <<; 60 }