POJ 1562Oil Deposits深搜

来源:互联网 发布:mac os10.6.8镜像下载 编辑:程序博客网 时间:2024/05/17 12:49

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 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

注意1:组的维数必须使用大于等于一的常量表达式来定义,此常量表达式只能包含整型字面值常量,枚举类型,或者用常量表达式初始化的整型,
非const变量和到运行阶段才知道其值的const变量都不能用来定义数组的维数
注意2:初无意将pic数组设为int型 字符可以按字符型读入存入整型数组里,存放的是ASCII码,可以字符类型变量按照整型输出,也是ASCII码。
不可以的是:但是字符类型不可以按照整型读入,存入的数字不知道是个是啥
不可以的是:整型数组里不可以存放字符串 所以如果pic是二维整型数组,不可以像下面一样一层循环按照字符串读入。可以一个一个字符的读入

#include<iostream>#include<cstdio>using namespace std;int main(){    char a;    int b[2];    scanf("%d",&a);    b[0] = a;    printf("%d\n",a);    printf("%c",a);    return 0;} 

这里写图片描述

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;const int maxt = 102;//不加const会报错 影响下面数组开辟空间 int flag[maxt][maxt];//用来标记所属连通集的编号 char pic[maxt][maxt];int m, n;//m行n列 void dfs(int a, int b, int id);int main(){    while(cin>>m>>n&&m&&n)    {        memset(flag,0,sizeof(flag));        for(int i = 0;i<m;i++)        {            scanf("%s",pic[i]); //字符型数组可以读入字符串        }        int cnt = 0;        for(int i=0;i<m;i++)        {            for(int j=0;j<n;j++)            {                if(flag[i][j]==0&&pic[i][j]=='@')//第一次判定是否该位置满足条件                 dfs(i,j,++cnt);            }        }        cout<<cnt<<endl;    }    return 0; }void dfs(int a, int b, int id){    if(a==-1||a==m||b==-1||b==n) return;//越界    //当前的位置不满足条件 不再继续深搜     if(flag[a][b]>0||pic[a][b]!='@')return ;//每一次深搜下一个位置都再次判断是否满足条件 (和上面第一次判断不要落了某次)     flag[a][b] = id;     //尝试四个方向是不是属于同一连通集     for(int dir = -1; dir<=1;dir++)    {        for(int rid = -1;rid<=1;rid++)        {        //  if(dir== 0||rid==0)break;判断错误         if(dir!=0||rid!=0) dfs(a+dir,b+rid,id);         }    } }
原创粉丝点击