srm 578

第二题:

N*M cells in the bottom of a cage, each cell have a birld, it can be empty or a goose or a duck. 

you are given a String[] describe the cells, 'v' for a bird,   '.' for empty. 

calculate the maximum number of goose there may have.

The goose are placed with two restiction: 1. at least one goose existis, 2. if a cell is a goose, then any cells within 

Manhattan distance are also goose.

Manhattan Distance: for cell (i,j) and (i1, j1), Manhattan Distance is |i1-i|+|j1-j|

Use depth search can solve this problem.

做题的时候没看清within (<=), 写成==dist 了, 坑爹- -

public class GooseInZooDivTwo {
    int N;
    int M;
    char f[][];
    boolean b[][];
    void dfs(int i, int j, int d){
        
        b[i][j]=true;
        for(int i1=0;i1<N;i1++){
            for(int j1=0;j1<M;j1++){
                if(f[i1][j1]=='v'&&!b[i1][j1]&&Math.abs(i1-i)+Math.abs(j1-j)<=d){
                    //b[i1][j1]=true;
                    dfs(i1,j1,d);
                }
            }
        }
    }
    public int count(String[] field, int dist){
        N=field.length;
        M=field[0].length();
        f=new char[N][M];
        b=new boolean[N][M];
        int i=0,j=0;
        for(i=0;i<N;i++){
            for(j=0;j<M;j++){
                f[i][j]=field[i].charAt(j);
                b[i][j]=false;
            }
        }
        i=0;j=0;
        int cnt=0;
        for(i=0;i<N;i++){
            for(j=0;j<M;j++){
                if(f[i][j]=='v'&&b[i][j]==false){
                    //b[i][j]=true;
                    dfs(i,j,dist);
                    cnt++;
                }
            }
        }
        //System.out.println(cnt);
       int r=1;
       for(i=0;i<cnt;i++){
           r=r*2;
           r=r%1000000007;
       }
       r=r-1;
       System.out.println(r);
       return (int)(r);
    }
    public static void main(String args[]){
        String str[]=new String[]{
                "v.v..................v............................"
                ,".v......v..................v.....................v"
                ,"..v.....v....v.........v...............v......v..."
                ,".........vvv...vv.v.........v.v..................v"
                ,".....v..........v......v..v...v.......v..........."
                ,"...................vv...............v.v..v.v..v..."
                ,".v.vv.................v..............v............"
                ,"..vv.......v...vv.v............vv.....v.....v....."
                ,"....v..........v....v........v.......v.v.v........"
                ,".v.......v.............v.v..........vv......v....."
                ,"....v.v.......v........v.....v.................v.."
                ,"....v..v..v.v..............v.v.v....v..........v.."
                ,"..........v...v...................v..............v"
                ,"..v........v..........................v....v..v..."
                ,"....................v..v.........vv........v......"
                ,"..v......v...............................v.v......"
                ,"..v.v..............v........v...............vv.vv."
                ,"...vv......v...............v.v..............v....."
                ,"............................v..v.................v"
                ,".v.............v.......v.........................."
                ,"......v...v........................v.............."
                ,".........v.....v..............vv.................."
                ,"................v..v..v.........v....v.......v...."
                ,"........v.....v.............v......v.v............"
                ,"...........v....................v.v....v.v.v...v.."
                ,"...........v......................v...v..........."
                ,"..........vv...........v.v.....................v.."
                ,".....................v......v............v...v...."
                ,".....vv..........................vv.v.....v.v....."
                ,".vv.......v...............v.......v..v.....v......"
                ,"............v................v..........v....v...."
                ,"................vv...v............................"
                ,"................v...........v........v...v....v..."
                ,"..v...v...v.............v...v........v....v..v...."
                ,"......v..v.......v........v..v....vv.............."
                ,"...........v..........v........v.v................"
                ,"v.v......v................v....................v.."
                ,".v........v................................v......"
                ,"............................v...v.......v........."
                ,"........................vv.v..............v...vv.."
                ,".......................vv........v.............v.."
                ,"...v.............v.........................v......"
                ,"....v......vv...........................v........."
                ,"....vv....v................v...vv..............v.."
                ,".................................................."
                ,"vv........v...v..v.....v..v..................v...."
                ,".........v..............v.vv.v.............v......"
                ,".......v.....v......v...............v............."
                ,"..v..................v................v....v......"
                ,".....v.....v.....................v.v......v......."};
        String str2[]=new String[]{"vvv"};
        new GooseInZooDivTwo().count(str,3);
    }
}

 

 

posted @ 2013-05-03 13:24  xxx's blog  阅读(372)  评论(0编辑  收藏  举报