Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18082 Accepted Submission(s): 10420
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
题意:探采石油的场景....
给出两个数,代表限定的范围,在里面,@代表有油的地方,* 代表没油的地方,然后每个有油的地方,如果它的周围临近的八个地方有油的话,那么也会被发现,现在让你找最少挖几次,可以把这些地方的油全部开采完毕,也就是全部发现.....
也就是找到有几个独立的有某个元素的集合.......不连通的个数...
分析:
这个算是简单的 dfs 问题,只需要简单的递归来枚举,判断,计数就可以了,不过递归要注意条件的判断.......
因为有八个方向,那么需要每次递归八次,这样写有点累赘,见到有人用常量数组来特殊处理,不过我想了另外一种方法,用个双循环来替代了数组,虽然并没有简化,相反,还复杂了不少,但是我觉得,什么时候都要有自己的创新的地方,完全跟着别人的思路走,根本不可能比别人走的更远....
嘿嘿,个人见解,继续努力吧!!
#include<stdio.h>
int t,n,m,sum;
char x[105][105];
void rab(int a,int b)
{
int i,j;
if(a<0||a>n-1||b<0||b>m-1)//越界就退出...
{
return;
}
if(x[a][b]=='*')//没有也退出...
{
return;
}
x[a][b]='*';//使用过就标记
for(i=-1;i<2;++i)//虽然比较复杂点,但是很好的想法
{
for(j=-1;j<2;++j)
{
if(i|j)
{
rab(a+i,b+j);
}
}
}
/*rab(a-1,b);//这样枚举有点意思,哈哈
rab(a+1,b);
rab(a,b-1);
rab(a,b+1);
rab(a-1,b-1);
rab(a-1,b+1);
rab(a+1,b-1);
rab(a+1,b+1);*/
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m),m|n)
{
for(i=0;i<n;++i)
{
getchar();
for(j=0;j<m;++j)
{
scanf("%c",&x[i][j]);//输入
}
}
sum=0;
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
if(x[i][j]=='@')//查找
{
rab(i,j);//处理...
++sum;//统计....
}
}
}
printf("%d\n",sum);
}
return 0;
}
这样的递归方法很巧妙,一次就可以把所有满足条件的给全部处理了,虽然递归有点慢,不过还是很方便的.
过去很久了,再次写这个题,代码也比以前精炼多了,不过还是像以前一样马虎...
/*
2016年1月14日14:19
*/
#include<stdio.h>
#include<string.h>
int n,m;
char map[105][105];
void dfs(int x,int y)
{
if(x<0||x>=n||y<0||y>=m||map[x][y]=='*')
{
return;
}
map[x][y]='*';
for(int i=-1;i<=1;++i)
{
for(int j=-1;j<=1;++j)
{
dfs(x+i,y+j);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m),n||m)
{
for(int i=0;i<n;++i)
{
scanf("%s",map[i]);
}
int ans=0;
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(map[i][j]=='@')
{
++ans;
dfs(i,j);
}
}
}
printf("%d\n",ans);
}
return 0;
}