A - Oil Deposits
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
- 题意;油田用@表示,非油田用×表示;如果两块油田相邻则算成一块(包括对角线)。给定图形问有多少油田。
- 思路:将所有油田的位置记录下来,然后依次搜索,搜索完一块油田要注意将其清除或用visited数组保存是否访问。搜索完一块油田数加一。也可以用深搜。
- 失误:主要在输入部分出问题了。用%c时第三组数据总是不对,用了%s就对了。问学长才知道是将空格粘上去了。最后提交两种方法都行。我把实验的都放到上面,谨记!!!
- 代码如下:
#include<cstdio>
#include<cstring>
#include<queue>//要用queue除了该文件外还需命名空间
using namespace std;
const int MAXN=1e3+10;
char map[MAXN][MAXN];
int m,n;
struct node{ //位置信息
int x;
int y;
}stu[MAXN*MAXN];
int dir[9][2]={ //方向
0,0,
1,-1,
1,0,
1,1,
0,1,
-1,1,
-1,0,
-1,-1,
0,-1
};
bool judge(node go) //搜索条件
{
bool a=go.x>=1&&go.x<=m;
bool b=go.y>=1&&go.y<=n;
bool c=map[go.x][go.y]=='@';
return a&&b&&c;
}
int bfs(int x,int y)
{
if(map[x][y]=='@')//判断是否已经被清除
{
queue <node> que;//添加头节点
node spos;
spos.x=x;
spos.y=y;
que.push(spos);
while(!que.empty())//当队列非空时搜索
{
node now;
now=que.front();//取出节点
que.pop();
for(int i=1;i<=8;++i)//搜索与节点联通且符合条件的
{
node go;
go.x=now.x+dir[i][0];
go.y=now.y+dir[i][1];
if(judge(go))//如果符合 入队清除
{
map[go.x][go.y]='*';
que.push(go);
}
}
}
return 1;
}
return 0;
}
int main()
{
int i,j,cnt,ans;
while(~scanf("%d%d",&m,&n),m)
{
char cc=getchar();//主要是输出格式 两种方法都行
cnt=0;
// memset(map,'0',sizeof(map));
for(i=1;i<=m;++i)
{
// scanf("%s",map[i]+1);
// for(j=1;j<=n;++j)
// {
// if(map[i][j]=='@')
// {
// ++cnt;
// stu[cnt].x=i;
// stu[cnt].y=j;
// }
// }
// char ee=getchar();
for(j=1;j<=n;++j)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
{
++cnt;
stu[cnt].x=i;
stu[cnt].y=j;
}
}
map[i][j]='\0';
getchar();
}
// for(i=1;i<=m;++i)
//
// printf("%s\n",map[i]+1);
// printf("%d\n",cnt);
ans=0;
for(i=1;i<=cnt;++i)
{
ans+=bfs(stu[i].x,stu[i].y);
}
printf("%d\n",ans);
}
return 0;
}
/*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
*/