• [LintCode] 598. Zombie in Matrix


    Given a 2D grid, each cell is either a wall 2, a zombie 1 or people 0 (the number zero, one, two).Zombies can turn the nearest people(up/down/left/right) into zombies every day, but can not through wall. How long will it take to turn all people into zombies? Return -1 if can not turn all people into zombies.

    Example

    Example 1:

    Input:
    [[0,1,2,0,0],
     [1,0,0,2,1],
     [0,1,0,0,0]]
    Output:
    2
    

    Example 2:

    Input:
    [[0,0,0],
     [0,0,0],
     [0,0,1]]
    Output:
    4


    public class Solution {
        /**
         * @param grid: a 2D integer grid
         * @return: an integer
         */
        int row;
        int col;
        public int zombie(int[][] grid) {
            // write your code here
            
            row = grid.length;
            col = grid[0].length;
            int numPeople = 0;
            Queue<Cell> queue = new LinkedList<>();
        
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (grid[i][j] == 1) {
                        queue.add(new Cell(i, j, grid[i][j]));
                        
                    } else if (grid[i][j] == 0) {
                        numPeople += 1;
                    }
                }
            }
            int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
            int res = 0;
            while(!queue.isEmpty()) {
                int size = queue.size();
                if (numPeople == 0) {
                    return res;
                }
                // for (int i = 0; i < row; i++) {
                //     System.out.println(Arrays.toString(grid[i]));
                // }
                // System.out.println();
                while (size-- > 0) {
                    Cell cur = queue.poll();
                    for (int[] direction: directions) {
                        int nxtX = cur.x + direction[0];
                        int nxtY = cur.y + direction[1];
                        if (isValid(nxtX, nxtY, grid)) {
                            Cell nxtCell = new Cell(nxtX, nxtY, grid[nxtX][nxtY]);
                            queue.offer(nxtCell);
                            grid[nxtX][nxtY] = 1;
                            numPeople -= 1;
                        }
                    }
                }
                res += 1;
            }
            return -1;
        }
        
        private boolean isValid(int x, int y, int[][] grid) {
            if (x >= 0 && x < row && y >= 0 && y < col && grid[x][y] == 0) {
                return true;
            }
            return false;
        }
    }
    
    class Cell {
        int x;
        int y;
        int val;
        public Cell(int x, int y, int val) {
            this.x = x;
            this.y = y;
            this.val = val;
        }
    }
  • 相关阅读:
    021: class, objects and instance: 特殊方法与属性
    A simple website
    020: class, objects and instance: 一个简单的例子,压缩文件中内容的替换
    019: class, objects and instance: property
    018: class, objects and instance: static method
    python算法之归并排序
    python算法之希尔排序
    python算法之快速排序
    python算法之插入排序
    python算法之选择排序
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12564227.html
Copyright © 2020-2023  润新知