zoj2412


import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

public class Main{
	
	   public static void main(String[] args) throws Exception{
		      Scanner cin = new Scanner(new BufferedInputStream(System.in)) ;
			  PrintWriter cout = new PrintWriter(System.out);
			  
			  while(cin.hasNext()){
				    int n = cin.nextInt() , m = cin.nextInt() ;
				    if(n == -1 && m == -1) break ;
				    new Task().solve(n, m , cin , cout) ;
			  }
			  cout.flush()  ;
	   }
	   
}

class Task{
	
	  static class  Node{
		     public boolean  left , right , up , down ;
		     
		     public Node(boolean u , boolean d , boolean l ,boolean r){
		    	    up = u ; 
		    	    down = d ;
		    	    left = l ;
		    	    right = r ;
		     } 
		     
		     public boolean cango(int d , Node o){
		    	    switch(d){
						case 0:
							if(up && o.down) return true ;  break;
						case 1:
							if(down && o.up)  return true ;  break;
						case 2:
							if(left && o.right) return true ; break;
						case 3:
							if(right && o.left) return true ; break;		
						default: break;
					}
		    	    return false ;
		     } 
	  }
	  
	  HashMap<Character , Node> map = new HashMap<Character, Task.Node>() ;
	  
	  {
		    map.put('A', new Node(true, false ,true , false)) ;
		    map.put('B', new Node(true, false, false, true)) ;
		    map.put('C', new Node(false, true ,true ,false )) ;
		    map.put('D', new Node(false, true ,false ,true )) ;
		    map.put('E', new Node(true, true ,false ,false )) ;
		    map.put('F', new Node(false, false ,true ,true )) ;
		    map.put('G', new Node(true, false,true ,true )) ;
		    map.put('H', new Node(true, true, true , false)) ;
		    map.put('I', new Node(false, true ,true ,true )) ;
		    map.put('J', new Node(true, true, false ,true )) ;
		    map.put('K', new Node(true, true, true ,true )) ;
	  }
	
	  char[][]  str  ;
	  boolean[][]  vis ; 
	  int[][] dir = new int[][]{{-1,0}, {1,0} , {0,-1} ,{0,1}} ;
	  int n , m ;
	  
	  public  boolean can(int x , int y){
		      return  0 <= x && x < n && 0 <= y && y < m ;
	  }
	  
	  public  void  solve(int n ,  int m , Scanner cin , PrintWriter cout){
		      this.n = n ; this.m = m ;
		      str = new char[n][m] ;
		      for(int i = 0 ; i < n ; i++) str[i] = cin.next().toCharArray() ;
		      
		      vis = new boolean[n][m] ;
		      int sum = 0 ;
		      for(int i = 0 ; i < n ; i++){
		    	   for(int j = 0 ; j < m ; j++){
		    		    if(! vis[i][j]){
		    		    	  sum++ ;
		    		    	  dfs(i , j) ;
		    		     }
		    	   }
		      }
		      
		      cout.println(sum) ;
		   // cout.flush() ; 
	  }
	  
	  public  void  dfs(int x , int y){
		      vis[x][y] = true ;
		      for(int i = 0 ; i < dir.length ; i++){
		    	    int nx = x + dir[i][0] ;
		    	    int ny = y + dir[i][1] ;
		    	    if(can(nx , ny) && ! vis[nx][ny] && map.get(str[x][y]).cango(i , map.get(str[nx][ny]))) 
		    	        	dfs(nx, ny) ;
		      } 
	  }
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值