• [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;
        }
    }
  • 相关阅读:
    appfabric cache配置,实验记录
    appfabric cache存储ef 查询结果的bug
    CruiseControl.NET svn获取 自动编译 ftp上传
    @Ajax.RenderAction 把局部页改为ajax加载
    分布式架构下的mvc 异步controller ajax comet 长连接
    win7重装iis,搞死
    验证码识别的基本思路及方法
    C# 获取程序当前文件夹
    在c#中 限制文本框只能输入数字
    string字符串 获取指定位置范围的子字符串
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12564227.html
Copyright © 2020-2023  润新知