首页 > 专题系列 > Java解POJ > POJ 3051 Satellite Photographs [解题报告] Java
2013
11-12

POJ 3051 Satellite Photographs [解题报告] Java

Satellite Photographs

问题描述 :

Farmer John purchased satellite photos of W x H pixels of his farm (1 <= W <= 80, 1 <= H <= 1000) and wishes to determine the largest 'contiguous' (connected) pasture. Pastures are contiguous when any pair of pixels in a pasture can be connected by traversing adjacent vertical or horizontal pixels that are part of the pasture. (It is easy to create pastures with very strange shapes, even circles that surround other circles.)

Each photo has been digitally enhanced to show pasture area as an asterisk (‘*’) and non-pasture area as a period (‘.’). Here is a 10 x 5 sample satellite photo:

..*.....**

.**..*****

.*...*....

..****.***

..****.***

This photo shows three contiguous pastures of 4, 16, and 6 pixels. Help FJ find the largest contiguous pasture in each of his satellite photos.

输入:

* Line 1: Two space-separated integers: W and H

* Lines 2..H+1: Each line contains W “*” or “.” characters representing one raster line of a satellite photograph.

输出:

* Line 1: The size of the largest contiguous field in the satellite photo.

样例输入:

10 5
..*.....**
.**..*****
.*...*....
..****.***
..****.***

样例输出:

16

解题代码:

//* @author: [email protected]
import java.io.*;
public class Main
{
	static int[][] p;
	static int a,b,max=0,cnt;
	public static void main(String[] args) throws IOException
	{
		InputStreamReader is=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(is);
		String[] ss=in.readLine().split(" ");
		a=Integer.parseInt(ss[0]);
		b=Integer.parseInt(ss[1]);
		p=new int[b+2][a+2];
		for(int i=1;i<=b;i++)
		{
		  for(int j=1;j<=a;j++)
			p[i][j]=in.read();
		  in.readLine();
		}
		for(int i=1;i<=b;i++)
		{
		  for(int j=1;j<=a;j++)
		  {
			cnt=0;
			f(i,j);
			if(cnt>max) max=cnt;
		   }
		}
		System.out.println(max);
	}
	static void f(int x,int y)
	{
		if(p[x][y]!='*')return;
		p[x][y]=0;
		cnt++;
		f(x,y+1);
		f(x,y-1);
		f(x+1,y);
		f(x-1,y);
	}
}

  1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.