僵尸矩阵 · Zombie in Matrix
给定一个 2D 网格,每个单元格要么是墙 2,要么是僵尸 1,要么是人 0(数字 0、1、2)。僵尸每天可以将最近的人(上/下/左/右)变成僵尸,但是 不能穿墙。 把所有人都变成僵尸需要多长时间? 如果不能将所有人变成僵尸,则返回 -1。
题解:https://www.jiuzhang.com/solutions/zombie-in-matrix/
多源点bfs。 从所有的源点出发,往外感染即可。
class Coordinate {int x, y;public Coordinate(int x, int y) {this.x = x;this.y = y;}
}public class Solution {public int PEOPLE = 0;public int ZOMBIE = 1;public int WALL = 2;public int[] deltaX = {1, 0, 0, -1};public int[] deltaY = {0, 1, -1, 0};/*** @param grid a 2D integer grid* @return an integer*/public int zombie(int[][] grid) {if (grid == null || grid.length == 0 || grid[0].length == 0) {return 0;}int n = grid.length;int m = grid[0].length;// initialize the queue & count peopleint people = 0;Queue<Coordinate> queue = new LinkedList<>();for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (grid[i][j] == PEOPLE) {people++;} else if (grid[i][j] == ZOMBIE) {queue.offer(new Coordinate(i, j));}}}// corner caseif (people == 0) {return 0;}// bfsint days = 0;while (!queue.isEmpty()) {days++;int size = queue.size();for (int i = 0; i < size; i++) {Coordinate zb = queue.poll();for (int direction = 0; direction < 4; direction++) {Coordinate adj = new Coordinate(zb.x + deltaX[direction],zb.y + deltaY[direction]);if (!isPeople(adj, grid)) {continue;}grid[adj.x][adj.y] = ZOMBIE;people--;if (people == 0) {return days;}queue.offer(adj);}}}return -1;}private boolean isPeople(Coordinate coor, int[][] grid) {int n = grid.length;int m = grid[0].length;if (coor.x < 0 || coor.x >= n) {return false;}if (coor.y < 0 || coor.y >= m) {return false;}return (grid[coor.x][coor.y] == PEOPLE);}
}
僵尸矩阵 · Zombie in Matrix相关推荐
- R语言使用psych包进行探索性因子分析EFA、使用cov2cor函数将原始数据的协方差矩阵将其转换为相关性矩阵( covariance matrix into correlation matrix)
R语言使用psych包进行探索性因子分析EFA.使用cov2cor函数将原始数据的协方差矩阵将其转换为相关性矩阵( covariance matrix transform into correlati ...
- R语言使用caret包的confusionMatrix函数计算混淆矩阵、使用编写的自定义函数可视化混淆矩阵(confusion matrix)
R语言使用caret包的confusionMatrix函数计算混淆矩阵.使用编写的自定义函数可视化混淆矩阵(confusion matrix) 目录
- sklearn使用投票器VotingClassifier算法构建多模型融合的软投票器分类器(soft voting)并计算融合模型的混淆矩阵、可视化混淆矩阵(confusion matrix)
sklearn使用投票器VotingClassifier算法构建多模型融合的软投票器分类器(soft voting)并计算融合模型的混淆矩阵.可视化混淆矩阵(confusion matrix) 目录
- sklearn使用投票器VotingClassifier算法构建多模型融合的硬投票器分类器(hard voting)并计算融合模型的混淆矩阵、可视化混淆矩阵(confusion matrix)
sklearn使用投票器VotingClassifier算法构建多模型融合的硬投票器分类器(hard voting)并计算融合模型的混淆矩阵.可视化混淆矩阵(confusion matrix) 目录
- R语言构建混淆矩阵(仿真数据)并基于混淆矩阵(confusion matrix)计算并计算Accuracy、Precision、Recall(sensitivity)、F1、Specificity指标
R语言构建混淆矩阵(仿真数据)并基于混淆矩阵(confusion matrix)计算并计算Accuracy.Precision.Recall(sensitivity).F1.Specificity指标 ...
- 混淆矩阵(confusion matrix)是什么?以及能从混淆矩阵中衍生出来的指标FPR、TPR、FDR、ACC、PPV、NPV分别是什么?对应的概念及公式是什么?
混淆矩阵(confusion matrix)是什么?以及能从混淆矩阵中衍生出来的指标FPR.TPR.FDR.ACC.PPV.NPV分别是什么?对应的概念及公式是什么? 目录
- R计算混淆矩阵(Confusion Matrix)
R计算混淆矩阵(Confusion Matrix) 目录 R计算混淆矩阵(Confusion Matrix) 拟合Logistic回归模型 创建混淆矩阵
- 程序员能力矩阵 Programmer Competency Matrix
[译文]程序员能力矩阵 Programmer Competency Matrix [译文]程序员能力矩阵 Programmer Competency Matrix 注意:每个层次的知识都是渐增的,位于 ...
- 拉普拉斯矩阵(Laplace Matrix)与瑞利熵(Rayleigh quotient)
作者:桂. 时间:2017-04-13 07:43:03 链接:http://www.cnblogs.com/xingshansi/p/6702188.html 声明:欢迎被转载,不过记得注明出处哦 ...
最新文章
- python过采样代码实现_过采样中用到的SMOTE算法
- 空函数有参函数调用参数的注意事项Swift 1.1语言
- python读取大文件的坑_python读取大文件踩过的坑——读取txt文件词向量
- splay/fhq-treap 问卷调查反馈—— [JSOI2008]火星人prefix(splay),Strange Queries(fhq-treap)
- 【Python】datetime内置模块处理日期和时间
- Less入门与安装(转)
- mysql装完是什么样儿的_Win7系统安装MySQL之后找不到指定文件与服务如何解决?...
- Codeforces Round #573 (Div. 2) C. Tokitsukaze and Discard Items
- 明华读卡器 java_明华读卡器Java例程
- WarDrive:使用Backtrack 4中的Kismet进行嗅探并使用GE绘制地图的简明攻略
- linux下socket编程实例
- 2009年中国直销业9大主旋律
- 什么是南桥芯片和北桥芯片?南桥芯片和北桥芯片区别
- 计算机桌面导航栏中删除内容,教你如何一键去除Windows桌面右击菜单多余选项-win7右键菜单管理...
- POJ 3295: Tautology
- 计算机进入pe快捷键,U盘启动快捷键大全(附U盘启动必杀技)
- ai技术对计算机的影响,人工智能技术的发展会对我们产生什么影响
- BugKu -- AWD --S1排位赛-4
- python笔记 - urllib模块(二十一)
- 数据结构第一次上机实验报告