Oil Deposits ( 简单搜索,连通性)

来源:互联网 发布:甲骨文 ibm做啥软件 编辑:程序博客网 时间:2024/04/29 09:18

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 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
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<stdio.h>#include<cstring>#include<queue>using namespace std;char mp[101][101];int vis[101][101];int n,m;int check(int x,int y){    if(x<1||x>n||y<1||y>m||vis[x][y]==1||mp[x][y]=='*')        return 1;    return 0;}void  dfs(int x,int y){   if(check(x,y))//撞到南墙了,也就返回上一个调用他的函数,然后往另一个方向撞        return;   vis[x][y]=1;//标注已经进行过搜索  //就是一群傻叉,只认识一个方向和返回上一个节点  //然后再在上一个节点进行这个方向下的其他方向的操作  //有点像撞一个点,开出来一个函数,也就是8个方向,一个人先试,不行换另一个方向,  //都不行返回上一个节点  //这些函数也是按照先后顺序生成的,这个函数执行完直接返回上一个函数,上面的那个就像是  //验证的工作,看看可不可以再开出来一个新的函数,也就是说节点,不可以就返回了上一个节点  //写一下怎么实现回溯的,因为我们写的语句都是有顺序的我们执行的时候也是按照这个顺序来执行的  //先走第一方向的点,好可以的话,就又出现了一个函数,我们再去尝试,第一个方向还是1,但是不行了、  //2 . 3 ...都不行,这时候,新生成整个函数就执行完了,也就销毁了,我们就会到第一个点,尝试2      dfs(x,y+1);   dfs(x,y-1);   dfs(x-1,y);   dfs(x+1,y);   dfs(x+1,y+1);   dfs(x+1,y-1);   dfs(x-1,y+1);   dfs(x-1,y-1);}int main(){    while(~scanf("%d %d",&n,&m))    {        int ans=0;        if(m==0) break;        memset(mp,0,sizeof(mp));        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)            scanf("%s",mp[i]+1);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)                if(mp[i][j]=='@'&&vis[i][j]==0)                {                    dfs(i,j);//这一步就是对他周围进行标记                    ans++;//只要之前他们不是连着的,就是一块新的油田                }        }        printf("%d\n",ans);    }    return 0;}

广搜

#include<stdio.h>#include<cstring>#include<queue>using namespace std;//比较简单的一个广搜char mp[101][101];int vis[101][101];int move_x[9]={0,0,0,-1,-1,-1,1,1,1};//定义这八个方向int move_y[9]={-1,0,1,-1,0,1,-1,0,1};int n,m;//因为我们需要队列,放入的点还需要附加信息,比如位置信息还有其他的,所以定义一个节点的信息typedef struct{    int x;    int y;}Node;//这就是判断一个点是不是可以被放进队列,也就是说可不可以当成一个新的节点再去搜索int check(int x,int y){    if(x<1||x>n||y<1||y>m||mp[x][y]=='*'||vis[x][y]==1)        return 1;    return 0;}void  bfs(int x,int y){    queue<Node> q;    //开头的话,把信息给节点放入到队列中    Node now;    now.x=x;    now.y=y;    q.push(now);    while(q.size())//只要队列没用完就说明还没有遍历完周围的情况    {      Node now=q.front();//得到首节点的信息,然后想8个方向观察      q.pop();//扔掉首节点      for(int i=0;i<9;i++)      {        Node temp;        temp.x=now.x+move_x[i],temp.y=now.y+move_y[i];        if(check(temp.x,temp.y))//检查我模拟的这步对不对,超界,看过,题目要求,不能是*            continue;        vis[temp.x][temp.y]=1;//进行标记        q.push(temp);//放入队列中      }    }}int main(){    while(~scanf("%d %d",&n,&m))    {        int ans=0;        if(m==0) break;        memset(mp,0,sizeof(mp));        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)            scanf("%s",mp[i]+1);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)                if(mp[i][j]=='@'&&vis[i][j]==0)                {                    bfs(i,j);//这一步就是对他周围进行标记                    ans++;//只要之前他们不是连着的,就是一块新的油田                }        }        printf("%d\n",ans);    }    return 0;}
原创粉丝点击