zoj2412

来源:互联网 发布:淘宝买家数据采集 编辑:程序博客网 时间:2024/06/05 20:23


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) ;      }   }}


0 0
原创粉丝点击