[LintCode] 598 Zombie in Matrix 解题报告

Description
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
Given a matrix:

0 1 2 0 0
1 0 0 2 1
0 1 0 0 0
return 2

5/13/2017

算法班,未经验证

注意,这里需要算需要多久,所以是层序遍历的问题,所以第39行开始是层序遍历的写法,普通BFS不需要,而且在放入queue之前就要先判断好是否是1,否则会无谓的监测一遍,day的最后值有出入

 1 public class Solution {
 2     /**
 3      * @param grid  a 2D integer grid
 4      * @return an integer
 5      */
 6
 7     class Point{
 8         int x;
 9         int y;
10         public Point(int x, int y) {
11             this.x = x;
12             this.y = y;
13         }
14     }
15     public int zombie(int[][] grid) {
16
17         // Write your code here
18         if (grid == null || grid.length == 0 || grid[0].length == 0) {
19             return -1;
20         }
21         int[] xDirection = new int[]{1, -1, 0, 0};
22         int[] yDirection = new int[]{0, 0, 1, -1};
23
24         // boolean[][] visited = new boolean[grid.length][grid[0].length];
25         Queue<Point> queue = new LinkedList<Point>();
26         int people = 0, day = 0;
27
28         for (int i = 0; i < grid.length; i++) {
29             for (int j = 0; j < grid[0].length; j++) {
30                 if (grid[i][j] == 1) {
31                     queue.offer(new Point(i, j));
32                 } else if (grid[i][j] == 0) {
33                     people++;
34                 }
35             }
36         }
37
38         while (!queue.isEmpty()) {
39             int size = queue.size();
40             for (int i = 0; i < size; i++) {
41                 Poing p = queue.poll();
42                 for (int j = 0; j < xDirection.length; i++) {
43                     int tmpX = p.x + xDirection[j];
44                     int tmpY = p.y + yDirection[j];
45                     if (inBoundary(tmpX, tmpY, grid.length, grid[0].length) && grid[tmpX][tmpY] == 0) {
46                         queue.offer(new Point(tmpX, tmpY));
47                         grid[tmpX][tmpY] = 1;
48                         people--;
49                     }
50                 }
51             }
52             day++;
53             if (people == 0) {
54                 return day;
55             }
56         }
57         return -1;
58     }
59
60     private boolean inBoundary(int x, int y, int xLength, int yLength) {
61         if (x < xLength && x >= 0 && y < yLength && y >= 0) return true;
62         return false;
63     }
64
65 }
时间: 2024-10-10 05:49:27

[LintCode] 598 Zombie in Matrix 解题报告的相关文章

[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

LeetCode: 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: Implement Queue by Stacks 解题报告

Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is

LeetCode: Spiral Matrix 解题报告

Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For 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]. Solution 1: 使

LeetCode 867 Transpose Matrix 解题报告

题目要求 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix. 题目分析及思路 题目要求得到矩阵的转置矩阵.可先得到一个行数与原矩阵列数相等.列数与原矩阵行数相等的矩阵,再对原矩阵进行遍历. python代码? c

[LintCode] 618 Search Graph Nodes 解题报告

DescriptionGiven a undirected graph, a node and a target, return the nearest node to given node which value of it is target, return NULL if you can't find. There is a mapping store the nodes' values in the given parameters. NoticeIt's guaranteed ther

leetCode解题报告5道题(九)

题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 分析: 题意给我们一个数字n, 和一个数字k,让我们求出从 1~~n中取出k个数所能得到的组合数 所

解题报告 之 2015蓝桥杯 垒骰子

解题报告 之 2015蓝桥杯 垒骰子 赌圣 atm 晚年迷恋上了垒骰子,就是把骰子一个垒在还有一个上边.不能歪歪扭扭,要垒成方柱体. 经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥! 我们先来规范一下骰子:1 的对面是 4.2 的对面是 5,3 的对面是 6. 如果有 m 组相互排斥现象,每组中的那两个数字的面紧贴在一起,骰子就不能稳定的垒起来. atm 想计算一下有多少种不同的可能的垒骰子方式. 两种垒骰子方式同样,当且仅当这两种方式中相应高度的骰子的相应数字的朝向都同

解题报告 之 POJ3686 The Windy&#39;s

解题报告 之 POJ3686 The Windy's Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The manager knows that every order will take different amount of hours in