Poj Solution 2386

http://poj.org/problem?id=2386

//* @author: 82638882@163.com
import java.util.Scanner;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  int a=in.nextInt();
  int b=in.nextInt();
  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]==87){
    find(arr,i,j);
    count++;
      }
   System.out.println(count);
                
   }

  public static void find(int[][] a,int i,int j)
  {
   if(a[i][j]==87)
   {
    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]==87)find(a,k,w);
   }
 }
}
											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *