深度搜索,石油采集,

来源:互联网 发布:mac防蹭网软件 编辑:程序博客网 时间:2024/06/10 05:17

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

0122
 

代码:

#include<stdio.h>   int dir[2][8]={{-1,0,1,-1,1,-1,0,1},{-1,-1,-1,0,0,1,1,1}}; //注意题目是八个方向;   char c[101][101];    void dfs(int x,int y)  {          int dx,dy,i;          for(i=0;i<8;i++)          {                  dx=x+dir[0][i];                  dy=y+dir[1][i];                  if(c[dx][dy]=='@')                  {                          c[dx][dy]='*';                          dfs(dx,dy);     //向八个方向搜索;                     }          }  }  int main()  {          int m,n,j,i;          while(scanf("%d %d",&m,&n)&&(m||n))          { getchar();//这里也是,有段时间输出一直不对,输入1 1就直接输出0了= =还一直不知道为什么;后来再看了学长代码才知道没有吸收空格,又傻逼了= =;                for(i=0;i<m;i++)                  {                          for(j=0;j<n;j++)                          {                                 scanf("%c",&c[i][j]);                          }  getchar();//吸收空格;                }                  int num=0;  //初始为0;                for(i=0;i<m;i++)                  {                          for(j=0;j<n;j++)                          {                                  if(c[i][j]=='@')                                  {                                          c[i][j]='*';                                          num++;  //一直卡在这里,不知道怎么表达num加,后来才明白搜索时,如果他八个方向找不到临近的'@',就直接返回找了= =原谅我傻逼了                                        dfs(i,j);                                  }                                                            }                  }                  printf("%d\n",num);          }          return 0;  }  

瓶颈:

1;num的值,是这个题我一直不知道怎么转换的,因为题目要求是输出不同数量的采矿点数目,傻逼的以为要很麻烦的循环判断,一直卡着;

2;用scanf输入字符串= =,又忘记空格,tab区等的处理了= =;

3;有大神告诉我说,定义全局变量num,用桶排的思想来得到答案= =,= =我个渣渣能试出来再发大神化的代码好了;


0 0
原创粉丝点击