题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 点击打开链接
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16165 Accepted Submission(s): 9266
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
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2
bfs 思路
#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 105
using namespace std;
char map[maxn][maxn];
int vis[maxn][maxn];
typedef struct{
int a , b;
}lct;
queue <lct> q;
int main()
{
int m, n;
while (scanf("%d%d", &m, &n)!=EOF && m && n){
int counts = 0;
for (int i = 0; i < m; i++){
getchar();
for (int j = 0; j < n; j++)
scanf("%c", &map[i][j]);
}
memset(vis, 0, sizeof(vis));
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if (map[i][j] == '@' && vis[i][j] == 0){
vis[i][j] = 1;
lct start;
start.a = i;
start.b = j;
q.push(start);
while (!q.empty()){
lct tmp = q.front();
q.pop();
for (int _i = -1; _i < 2; _i++)
for (int _j = -1; _j < 2; _j++){
lct news = tmp;
if (map[news.a + _i][news.b + _j] == '@' && vis[news.a + _i][news.b + _j] == 0 && news.a + _i >= 0 && news.a + _i < m && news.b + _j >= 0 && news.b + _j < n){
vis[news.a + _i][news.b + _j] = 1;
// printf("(%d,%d)+(%d,%d)\n", news.a , news.b, _i, _j);
news.a += _i;
news.b += _j;
q.push(news);
}
}
}
counts ++;
}
}
}
printf("%d\n", counts);
}
}
dfs 思路
#include <stdio.h>
#include <string.h>
#define maxn 105
bool vis[maxn][maxn];
char map[maxn][maxn];
int m, n;
int counts;
void dfs(int _i, int _j)
{
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++){
if (_i + i < 0 && _i + i > m) continue;
if (_j + j < 0 && _j + j > n) continue;
if (map[_i+i][_j+j] == '@' && vis[_i+i][_j+j] == 0){
vis[_i+i][_j+j] = 1;
dfs(_i+i,_j+j);
}
}
}
int main()
{
while (scanf("%d%d", &m, &n) != EOF && m && n){
memset(vis, 0, sizeof(vis));
memset(map, 0, sizeof(map));
for (int i = 0; i < m; i++){
getchar();
for (int j = 0; j < n; j++)
scanf("%c", &map[i][j]);
}
counts = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++){
if (map[i][j] == '@' && vis[i][j] == 0){
dfs(i,j);
counts++;
}
}
printf("%d\n", counts);
}
}