1.1011
2.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. <br>
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.<br>
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.<br>
Sample Input
1 1<br>*<br>3 5<br>*@*@*<br>**@**<br>*@*@*<br>1 8<br>@@****@*<br>5 5 <br>****@<br>*@@*@<br>*@**@<br>@@@*@<br>@@**@<br>0 0 <br>
Sample Output
0<br>1<br>2<br>2<br>
3.给出的图中@代表有石油,*代表没有石油,而在一个有石油的地方它的周围8个方向的地方如果也有石油,那么这2块石油是属于一块的,给出图,问图中有几块石油田.
如图1: 一个1 * 1 大小的图,只有一个*,就是没有石油.
图2: 3 * 5 大小的图中因为:
@ @
@
@ @
是连成一块的,所以图中只有一个油田
同理第3个图有3个油田.
解题方法:
4.dfs-采用深度优先搜索策略,对给出的图中一旦出现一个@则对其8个方向进行搜索,并对搜索过的@标记,直到一次搜索结束则油田总数加一,最后的总数即为所求的石油的方块数
5.#include<iostream>
#include<string.h>
#include<set>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<iomanip>
#include<cstdio>
using namespace std;
bool a[200][200];
char ww[200][200];
int n,m;
void ans(int i,int j)
{
int x=-1,y=-1;
if(i>=n||i<0||j>=m||j<0) return ;
else
{
for(x=-1;x<2;x++)
{
for(y=-1;y<2;y++)
{
if(x==0&&y==0) continue;
if(ww[i+x][j+y]=='@'&&!a[i+x][j+y])
{
a[i+x][j+y]=1;
ans(i+x,j+y);
}
}
}
}
}
int main()
{
//freopen("ww.txt","ww",stdin);
int i,z,k,j,t;
while(cin>>n>>m&&n!=0||m!=0)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>ww[i][j];
}
}
memset(a,0,sizeof(a));
t=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ww[i][j]=='@'&&a[i][j]==0)
{
a[i][j]=1;
ans(i,j);
t++;
}
}
}
cout<<t<<endl;
}
return 0;
}