• Uva 572 Oil Deposits


    思路:可以用DFS求解。遍历这个二维数组,没发现一次未被发现的‘@’,便将其作为起点进行搜索。最后的答案,是这个遍历过程中发现了几次为被发现的‘@’

    import java.util.*;
    
    public class Main{
    
        public static void main(String[] args) {
            Scanner in = new Scanner( System.in );
            while( true ) {
                int rows, cols;
                rows = in.nextInt(); // read the rows
                cols = in.nextInt(); // read the cols
                if( rows == 0 && cols == 0 ) { // the input end
                    break;
                }
                OilDeposits od = new OilDeposits( rows, cols );
                od.readGrids( in ); // read the strings
                System.out.println( od.getPocketsAmount() );
            }
        }
    }
    
    class OilDeposits{
        private int rows, cols;
        private char grids[][];
        private boolean vis[][];
        private final int dir[][] = { // the eight directions
                {-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1}
            };
    
        private final char EMPTY = '*';
        private final char EXIST = '@';
        
        public OilDeposits( int rows, int cols ) {
            this.rows = rows;
            this.cols = cols;
            this.grids = new char[ rows + 5 ][ cols + 5 ];
            this.vis = new boolean[ rows ][ cols ];
            for( int i=0; i<rows; i++ ) {
                for( int j=0; j<cols; j++ ) {
                    this.vis[i][j] = false;
                }
            }
        }
    
        private boolean isValid( int row, int col ) { // whether it can visit this position
            if( row < 0 || row >= this.rows || col < 0 || col >= this.cols || vis[row][col] || this.grids[row][col] == EMPTY ) {
                return false;
            }
            return true;
        }
    
        public void readGrids( Scanner in ) {
            String str;
            for( int i=0; i<rows; i++ ) {
                this.grids[i] = in.next().toCharArray();
            }
        }
    
        public void Search( int row, int col ) { // search this grid and its adjacent grids
            if( !this.isValid( row, col ) ) {
                return ;
            }
            this.vis[row][col] = true;
            for( int i=0; i<8; i ++ ) {
                Search( row+dir[i][0], col+dir[i][1] );
            }
        }
    
        public int getPocketsAmount() {
            int counter = 0;
            for( int i=0; i<this.rows; i++ ) {
                for( int j=0; j<this.cols; j++ ) {
                    if( this.grids[i][j] == '@' && !this.vis[i][j] ) {
                        counter ++;
                        this.Search( i, j );
                    }
                }
            }
            return counter;
        }
    }
    

     其中之所以将Scanner的对象进行传递,是因为如果重新声明一个Scanner的对象,会使输入出错,错误的具体表现为一直等待输入,尽管已经在Console中输入了数据。猜测可能是未关闭上一个Scanner的对象导致的,所以这里将Scanner的对象进行传递。

  • 相关阅读:
    资源网站
    远程服务SSH
    Samba服务配置
    练习raid5
    raid(0、1)
    NFS服务器配置(windows访问)
    LVM磁盘阵列
    ISCSI服务器配置
    Zibbix监控
    MySQL Replication配置
  • 原文地址:https://www.cnblogs.com/Emerald/p/4544682.html
Copyright © 2020-2023  润新知