Zombie in Matrix

Note:

This is to count the level. At beginning, since there are so many zombies, we need to add all of them to the Queue to start with. It is different than Number of Island. That one doesn't required the level, instead, it wants to count the zone, so we can start with one island.

For the level, be careful about the level got return. We need to figure out whether the farest elements level need to be counted or not

public class Solution {
    /**
     * @param grid  a 2D integer grid
     * @return an integer
     */
    class Coordinate {
        int x;
        int y;
        public Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    public static final int WALL = 2;
    public static final int ZOMBIE = 1;
    public static final int PEOPLE = 0;
    public static final int[] X = {0, -1, 1, 0};
    public static final int[] Y = {-1, 0, 0, 1};
    public int zombie(int[][] grid) {
        // Write your code here
        if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0) {
            return -1;
        }
        int day = eatPeople(grid);
        boolean isSurvive = isAnySurvivor(grid);
        if (isSurvive) {
            return -1;
        }
        return day;
    }
    private int eatPeople(int[][] grid) {
        Queue<Coordinate> q = new LinkedList<>();
        int nRow = grid.length;
        int nCol = grid[0].length;
        for (int i = 0; i < nRow; i++) {
            for (int j = 0; j < nCol; j++) {
                if (grid[i][j] == ZOMBIE) {
                    q.offer(new Coordinate(i, j));
                } 
            }
        }
        int day = 0;
        while (!q.isEmpty()) {
            int n = q.size();
            for (int i = 0; i < n; i++) {
                Coordinate zombie = q.poll();
                grid[zombie.x][zombie.y] = WALL;
                for (int j = 0; j < X.length; j++) {
                    Coordinate neighbor = new Coordinate(zombie.x + X[j], zombie.y + Y[j]);
                    //System.out.println(neighbor.x + " " + neighbor.y + isInBound(neighbor, nRow, nCol));
                    if (isInBound(neighbor, nRow, nCol) && grid[neighbor.x][neighbor.y] == PEOPLE) {
                            grid[neighbor.x][neighbor.y] = ZOMBIE;
                            q.offer(neighbor);
                        }
                }    
            }
            day++; 
        }
        return day - 1; // The day include zombies turning on last day. We need to process these and make sure no element is added to queue. But this last process shouldn't be counted.
    }
    
    private boolean isInBound(Coordinate node, int nRow, int nCol) {
        int x = node.x;
        int y = node.y;
        return x >= 0 && x < nRow && y >= 0 && y < nCol;
    }
    
    private boolean isAnySurvivor(int[][] grid) {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == PEOPLE) {
                    return true;
                } 
            }
        }
        return false;
    }
}

 

posted on 2017-06-15 11:22  codingEskimo  阅读(117)  评论(0编辑  收藏  举报

导航