[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;
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12564227.html

时间: 2024-10-17 15:58:03

[LintCode] 598. Zombie in Matrix的相关文章

[LintCode] 598 Zombie in Matrix 解题报告

DescriptionGiven 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 p

Zombie in Matrix Lintcode

Given a 2D grid, each cell is either a wall 2, a zombie 1or 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 z

[LintCode] Search a 2D Matrix

Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer

LintCode &quot;Search a 2D Matrix II&quot;

Simply a revise of a genius Greedy algorithm seen on LeetCode - linear walking. class Solution { public: /** * @param matrix: A list of lists of integers * @param target: An integer you want to search in matrix * @return: An integer indicate the tota

lintcode 容易题:Matrix Zigzag Traversal 矩阵的之字型遍历

题目: 矩阵的之字型遍历 给你一个包含 m x n 个元素的矩阵 (m 行, n 列), 求该矩阵的之字型遍历. 样例 对于如下矩阵: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10, 11, 12] ] 返回 [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12] 解题: 感觉很简单,就是没有搞出来,程序来源 ,这里是先右上走,我换成先横着走就是不对了,表示不理解.另外一种方法,表示也不理解. java程序: public class Solut

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 难得的一次AC! 虽然感觉题

[LintCode] Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Example Given a matrix [ [1,2], [0,3] ], return[[0,2],[0,0]] Challenge Did you use extra space?A straight forward solution using O(mn) space is probably a b

Matrix Zigzag Traversal(LintCode)

Matrix Zigzag Traversal Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in ZigZag-order. Have you met this question in a real interview? Yes Example Given a matrix: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10, 11, 12] ]

lintcode 容易题:Search a 2D Matrix 搜索二维矩阵

题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] 给出 target = 3,返回 true 挑战 O(log(n) + log(m)) 时间复杂度 解题: 1.最简单的方法就是遍历整个矩阵,时间复杂度:O(log(mn)),这个应该等于O(long(