首页 > 专题系列 > Java解POJ > POJ 1562 Oil Deposits [解题报告] Java
2013
11-09

POJ 1562 Oil Deposits [解题报告] Java

Oil Deposits

问题描述 :

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.

输入:

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.

输出:

are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

样例输入:

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

样例输出:

0
1
2
2

解题代码:

//* @author: [email protected]
import java.util.Scanner;
public class Main
{
  public static void main(String[] args)
   {
	Scanner in=new Scanner(System.in);
	while(true)
	{
	int a=in.nextInt();
	int b=in.nextInt();
	if(a==0&&b==0)break;
	int count=0;
	int[][] arr=new int[a+2][b+2];
	for(int i=1;i<=a;i++){
		String s=in.next();
		for(int j=1;j<=b;j++)
	          arr[i][j]=s.charAt(j-1);
	}
	for(int i=1;i<=a;i++)
	  for(int j=1;j<=b;j++)
		if(arr[i][j]==64){
			find(arr,i,j);
			count++;
		}
	System.out.println(count);
        }
				
     }
	
 public static void find(int[][] a,int i,int j)
  {
	a[i][j]=65;
	for(int k=i-1;k<=i+1;k++)
          for(int w=j-1;w<=j+1;w++)
	    if(a[k][w]==64)find(a,k,w);
 }
}

  1. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience

  2. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。