您的位置:首页 > 其它

HDU1241 简单的搜索 个人当作为简单模板

2016-11-12 21:11 411 查看
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
题解:给你一个油田的图,然后计算这个图的连通块。
从每个"@"格子出发.遍历它周围的"@"格子.每次访问一个格子的时候就给它标志一下就ok了,这样就可以在访问之前检查它是否有了编号。

下面附上自己随手画的最简单搜索题的流程图



#include "stdio.h"
#include "queue"
#include "string.h"
using namespace std;
int map[105][105];
int n,m;
struct node
{
int x;
int y;
};

int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,-1,-1,-1,1,1,-1};
void bfs(int x,int y)
{	queue<node>q;
node now ,next;
now.x=x;
now.y=y;
q.push(now);// 初始条件入队
map[now.x][now.y]=0;
while (!q.empty())//队是否空
{
now=q.front();//出队
q.pop();

//  此处判断是否满足最终条件
//	但是这一题并不需要找到最终对象 所以省略

for(int i=0;i<8;i++)//循环方向
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(next.x>=0&&next.x<n&&next.y<m&&next.y>=0)
{
if(map[next.x][next.y])//满足条件入队
{
map[next.x][next.y]=0;
q.push(next);
}
}
}
}
}
int main()
{
while (scanf("%d%d",&n,&m)==2&&n)
{
char t;
getchar();
int time =0;
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%c",&t);
if(t=='@')
map[i][j]=1;
}
getchar ();
}
for(int i=0;i<n;i++)// 判断连通块
{
for(int j=0;j<m;j++)
{
if(map[i][j]==1)
{
bfs(i,j);
time++;
}
}
}
printf("%d\n",time );
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: