例1 Oil Deposits
Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either *', representing the absence of oil, or
@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
Sample Output
- 0
- 1
- 2
- 2
C++代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include<iostream> #include<vector> using namespace std; //Flood Fill char maze[101][101]; bool mark[101][101]; int n, m; int go[][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, -1, -1, 1}; //8个相邻点与当前位置的坐标差 void DFS(int x, int y){ for(int i = 0; i < 8; i++){ //遍历八个相邻点 int nx = x + go[i][0]; int ny = y + go[i][1]; if(nx < 1 || nx > n || ny < 1 || ny > m)continue;//越界处理 if(maze[nx][ny] == '*')continue;//非油田 if(mark[nx][ny])continue;//已经标记 mark[nx][ny] = true; DFS(nx, ny); } } int main(){ while(cin>>n>>m){ if(n == 0 && m == 0)break; for(int i = 1; i <= n; i++){ scanf("%s", maze[i] + 1); } fill(mark[0], mark[0] + 101 * 101, false); int ans = 0; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(mark[i][j])continue; if(maze[i][j] == '*')continue; DFS(i, j); ans++; } } printf("%d\n", ans); } return 0; } |
例2 Temple of the bone
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
Sample Output
- NO
- YES
C++代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include<iostream> #include<vector> using namespace std; int n, m, t; char maze[8][8]; bool success; int go[][2] = {1, 0, -1, 0, 0, 1, 0, -1}; //四方向行走坐标差 void DFS(int x, int y, int time){ for(int i = 0; i < 4; i++){ int nx = x + go[i][0]; int ny = y + go[i][1]; if(nx < 1 || nx > n || ny < 1 || ny > m)continue; if(maze[nx][ny] == 'X')continue; //如果为墙跳过 if(maze[nx][ny] == 'D'){ if(time + 1 == t){ success = true; return; }else continue; //否则该状态的后序状态不可能为答案(经过的点不能再经过) } maze[nx][ny] = 'X'; //该位置不能继续走下去,修改为wall DFS(nx, ny, time + 1); maze[nx][ny] = '.'; //若其后续状态遍历完毕,则退回上层状态 //if(success) return; } } int main(){ while(cin>>n>>m>>t){ if(n == 0 && m == 0 && t == 0)break; for(int i = 1; i <= n; i++){ scanf("%s", maze[i] + 1); } success = false; /* int sx, sy; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(maze[i][j] == 'D'){ sx = i; sy = i; } } } */ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(maze[i][j] == 'S'){ //这一步可以优化, 进行剪枝 maze[i][j] = 'X'; DFS(i, j, 0); } } } printf("%s\n", success ? "YES" : "NO"); } return 0; } |