Zombie in Matrix
来源:互联网 发布:python sys.argc 编辑:程序博客网 时间:2024/06/11 09:20
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.
java
class corr { int x; int y; public corr(int x, int y) { this.x = x; this.y = y; }}public class Solution { /* * @param grid: a 2D integer grid * @return: an integer */ int WALL = 2; int PEOPLE = 0; int ZOMBIE = 1; public int zombie(int[][] grid) { // write your code here if (grid == null || grid.length == 0 || grid[0].length == 0) { return -1; } int row = grid.length; int column = grid[0].length; Queue<corr> queue = new LinkedList<>(); int people = 0; int count = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { if (grid[i][j] == PEOPLE) { people++; } if (grid[i][j] == ZOMBIE) { queue.offer(new corr(i, j)); } } } if (people == 0) { return 0; } int[] dir_x = {0, 1, -1, 0}; int[] dir_y = {1, 0, 0, -1}; while (!queue.isEmpty()) { if (people == 0) { break; } int n = queue.size(); count++; for (int i = 0; i < n; i++) { corr node = queue.poll(); for (int j = 0; j < 4; j++) { int n_x = node.x + dir_x[j]; int n_y = node.y + dir_y[j]; if (isPeople(grid, n_x, n_y)) { grid[n_x][n_y] = ZOMBIE; people--; queue.offer(new corr(n_x, n_y)); } } } } if (people != 0) { return -1; } return count; } private boolean isPeople(int[][] grid, int x, int y) { int row = grid.length; int column = grid[0].length; return (x >= 0 && x < row && y >= 0 && y < column && grid[x][y] == PEOPLE); }}python
from Queue import Queueclass Solution: """ @param: grid: a 2D integer grid @return: an integer """ PEOPLE = 0 ZOMBIE = 1 WALL = 2 def zombie(self, grid): # write your code here count, people = 0, 0 queue = Queue() row, column = len(grid), len(grid[0]) for i in xrange(row): for j in xrange(column): if grid[i][j] == self.PEOPLE: people += 1 if grid[i][j] == self.ZOMBIE: queue.put([i, j]) if people == 0: return 0 dir_x = [1, 0, 0, -1] dir_y = [0, -1, 1, 0] while not queue.empty(): if people == 0: break count += 1 n = queue.qsize() for i in xrange(n): node = queue.get() for j in xrange(4): n_x = node[0] + dir_x[j] n_y = node[1] + dir_y[j] if self.isPeople(grid, n_x, n_y): grid[n_x][n_y] = self.ZOMBIE people -= 1 queue.put([n_x, n_y]) if people != 0: return -1 return count def isPeople(self, grid, x, y): row, column = len(grid), len(grid[0]) return x >= 0 and x < row and y >= 0 and y < column and grid[x][y] == self.PEOPLE
阅读全文
0 0
- Zombie in Matrix
- Zombie in Matrix
- Zombie in Matrix
- zombie
- How To Catch The Memory Zombie In Xcode
- zombie and orphan process in Linux-Unix systems
- Killing zombie process & emacs Copy paste in putty
- Matrix structure in XNA
- view Matrix in opengl
- Matrix multiplication in OpenCV
- CCI: Find in Matrix
- Matrix In OpenGL
- matrix inversion in boost
- B. OR in Matrix
- B. OR in Matrix
- B. OR in Matrix
- 【COderForces】OR in Matrix
- Missing number in matrix
- JS的三种消息提示框alert confirm prompt
- python进程和线程学习笔记
- java控制随机数出现的概率
- 数据结构-二叉树
- ORACLE存储过程范例
- Zombie in Matrix
- myeclipse 导入web项目报红叉解决方案
- 100元买鸡算法题 php程序员面试题
- Java的单例
- AngularJS 面试题集锦
- springboot常用注解集锦
- 基于开源 Openfire 聊天服务器
- HDU2519 新生晚会
- 实验楼linux学习(1-2节)