zoukankan      html  css  js  c++  java
  • POJNo.2386

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

    public class Main {
        int N;
        int M;
        char[][] garden;
        
        public Main(int N, int M, char[][] garden){
            this.N = N;
            this.M = M;
            this.garden = garden;
        }
        
        void dfs(int x, int y){
            garden[x][y] = '.';
            
            //printGarden();
            //System.out.println("=======================");
            
            for(int dx = -1; dx <= 1; dx++){
                for(int dy = -1; dy <= 1; dy++){
                    int nx = x + dx;
                    int ny = y + dy;
                    if(nx>=0 && nx< N && ny>= 0 && ny < M){
                        if(garden[nx][ny] == 'W'){
                            dfs(nx, ny);
                        }
                    }
                }
            }
        }
    
        int resolve(){
            int res = 0;
            for(int i = 0; i< N; i++){
                for(int j = 0; j< M; j++){
                    if(garden[i][j] == 'W'){
                        dfs(i,j);
                        res++;
                    }
                }
            }
            return res;
        }
        
        //打印整个过程
        void printGarden(){
            for(int i = 0; i< garden.length; i++){
                char[] row = garden[i];
                for(int j = 0; j<row.length; j++){
                    System.out.print(row[j]+" ");
                }
                System.out.println();
            }
        }
        
        public static void main(String[] args){
    
            char[][] garden = {
                    {'W','.','.','.','.','.','.','.','.','W','W','.'},
                    {'.','W','W','W','.','.','.','.','.','W','W','W'},
                    {'.','.','.','.','W','W','.','.','.','W','W','.'},
                    {'.','.','.','.','.','.','.','.','.','W','W','.'},
                    {'.','.','.','.','.','.','.','.','.','W','.','.'},
                    {'.','.','W','.','.','.','.','.','.','W','.','.'},
                    {'.','W','.','W','.','.','.','.','.','W','W','.'},
                    {'W','.','W','.','W','.','.','.','.','.','W','.'},
                    {'.','W','.','W','.','.','.','.','.','.','W','.'},
                    {'.','.','W','.','.','.','.','.','.','.','W','.'},
            };
            int N = garden.length;
            int M = garden[0].length;
            System.out.println(N+", "+M);
            
            Main test = new Main(N,M,garden);
            int res = test.resolve();
            System.out.println(res);
        }
        
    }
  • 相关阅读:
    程序活动记录&程序调试&多线程编程
    数据结构与算法
    C/C++
    Information Retrieval --- Retrieval Comment
    Information Retrieval --- Clustering
    Information Retrieval --- Classification
    Information Retrieval --- Web Search
    Information Retrieval --- Retrieval Enforce:Relevance Feedback & Query Expansion
    Information Retrieval --- Retrieval Model
    ubuntu server 安装vnc
  • 原文地址:https://www.cnblogs.com/gui0901/p/6384654.html
Copyright © 2011-2022 走看看