Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25760 Accepted Submission(s): 14830
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求联通块。。。为了复习一下dfs的基本流程==
java不友好的类让我把程序写的极其繁琐QAQ
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
boolean[][] v=new boolean[200][200];
char[][] k=new char[200][200];
int[] dx=new int[9];
int[] dy=new int[9];
dx[1]=1;dx[2]=0;dx[3]=-1;dx[4]=0;dx[5]=1;dx[6]=1;dx[7]=-1;dx[8]=-1;
dy[1]=0;dy[2]=1;dy[3]=0;dy[4]=-1;dy[5]=1;dy[6]=-1;dy[7]=1;dy[8]=-1;
while (s.hasNext())
{
int ans=0;
int n=s.nextInt();
int m=s.nextInt();
if (n==0) System.exit(-1);
for (int i=1;i<=n;i++)
{
String p=s.next();
for (int j=1;j<=p.length();j++)
{
k[i][j]=p.charAt(j-1);
v[i][j]=false;
}
}
slove kk=new slove(k,v,dx,dy,n,m);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if ((k[i][j]=='@')&&(!v[i][j]))
{
kk.dfs(i,j);ans+=1;
}
System.out.println(ans);
}
}
}
class slove
{
boolean[][] v=new boolean[200][200];
char[][] k=new char[200][200];
int[] dx=new int[9];
int[] dy=new int[9];
int tx=0,ty=0,ans=0,n,m;
public slove(char[][] k,boolean[][] v,int[] dx,int[] dy,int n,int m)
{
this.k=k;
this.v=v;
this.dx=dx;
this.dy=dy;
this.n=n;
this.m=m;
}
public void dfs(int x,int y)
{
v[x][y]=true;
for (int i=1;i<=8;i++)
{
tx=x+dx[i];ty=y+dy[i];
if ((!v[tx][ty])&&(k[tx][ty]=='@')&&(tx<=n)&&(ty<=m)&&(tx>0)&&(ty>0))
{
dfs(tx,ty);
}
}
}
}