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; otherwiseOutput
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
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2 题意: 一块区域中分布着油田,连在一起就属于一个油田,求油田个数?#include <iostream> #include <cstdio> #include <cstring> #define N 1000 using namespace std; char a[N][N]; int n, m; void dfs(int x,int y) { if (x < 0 ||x >= n || y <0 || y >= m) return ; if (a[x][y] == '@') { a[x][y] = '*'; dfs(x+1,y); dfs(x+1,y+1);dfs(x+1,y-1); dfs(x-1,y); dfs(x-1,y+1);dfs(x-1,y-1); dfs(x,y+1); dfs(x,y-1); } } int main() { while (scanf("%d%d", &n, &m) != EOF) { if(n == 0 && m== 0) break; getchar(); int flag = 0; memset(a, 0, sizeof(a)); for (int i = 0; i < n; i ++) // for (int j = 0; j < m; j++) scanf("%s",a[i]); for(int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { //printf("%c",a[i][j]); if(a[i][j] == '@'){ ++flag; dfs(i,j); } } } printf("%d\n",flag); } return 0; }
本文介绍了一个油田探测问题的解决方案,通过深度优先搜索算法来确定不同油田的数量。输入为矩形网格,每个单元格代表土地的一小块,含有油的地块会连接形成油田。
495

被折叠的 条评论
为什么被折叠?



