【Lintcode】1367. Police Distance
题目地址:
https://www.lintcode.com/problem/1367/
给定一个m×nm\times nm×n的矩阵,只含1,0,−11,0,-11,0,−1,111表示警察,000表示空地,−1-1−1表示障碍物。求一个m×nm\times nm×n的矩阵AAA使得A[i][j]A[i][j]A[i][j]表示(i,j)(i,j)(i,j)这个位置离其最近的警察与其的距离,距离取曼哈顿距离。障碍物的位置直接填−1-1−1。
从所有的警察开始做BFS即可。代码如下:
import java.util.LinkedList;
import java.util.Queue;public class Solution {/*** @param matrix : the martix* @return: the distance of grid to the police*/public int[][] policeDistance(int[][] matrix) {// Write your code hereint m = matrix.length, n = matrix[0].length;int[][] res = new int[m][n];boolean[][] vis = new boolean[m][n];Queue<int[]> queue = new LinkedList<>();for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (matrix[i][j] == 1) {queue.offer(new int[]{i, j});vis[i][j] = true;} else if (matrix[i][j] == -1) {res[i][j] = -1;}}}int dis = 0;int[] d = {1, 0, -1, 0, 1};while (!queue.isEmpty()) {dis++;for (int i = queue.size(); i > 0; i--) {int[] cur = queue.poll();int x = cur[0], y = cur[1];for (int j = 0; j < 4; j++) {int nextX = x + d[j], nextY = y + d[j + 1];if (0 <= nextX && nextX < m && 0 <= nextY && nextY < n && matrix[nextX][nextY] == 0 && !vis[nextX][nextY]) {res[nextX][nextY] = dis;vis[nextX][nextY] = true;queue.offer(new int[]{nextX, nextY});}}}}return res;}
}
时空复杂度O(mn)O(mn)O(mn)。
【Lintcode】1367. Police Distance相关推荐
- 【LeetCode】849. Maximize Distance to Closest Person
[LeetCode]849. Maximize Distance to Closest Person 849. Maximize Distance to Closest Person Easy In ...
- 【LeetCode】849. Maximize Distance to Closest Person 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LintCode】算法题 1443. 最长AB子串
描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...
- 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连
解析 max ksubarray sum: 最大和 of 连续子序列 => 最大和 of k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff: 无queu ...
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character 参考 1. Leetcode_easy_821. Shortest Distance to a Charac ...
- 【LeetCode】461. Hamming Distance (java实现)
2019独角兽企业重金招聘Python工程师标准>>> 原题链接 https://leetcode.com/problems/hamming-distance/ 原题 The Ham ...
- 【Lintcode】1413. Tree
题目地址: https://www.lintcode.com/problem/tree/description 给定一个无向图,其有树性质,并且顶点编号为111的为树根(也就是这个图是个有根树).要求 ...
- 【Lintcode】1645. Least Subsequences
题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...
- 【Lintcode】1375. Substring With At Least K Distinct Characters
题目地址: https://www.lintcode.com/problem/substring-with-at-least-k-distinct-characters/description 给定一 ...
最新文章
- 使用ViewBag传送数据从控制器至视图
- 使用SAE和Gitcafe开发网站应用
- sklearn之pipeline:pipeline的简介、使用技巧、案例应用之详细攻略
- vba quit此文件正由另一个应用程序或用户使用_VBA|如何添加外部对象库(或控件)引用来扩展VBA功能...
- codesys com库_CODESYS在线库,酷德网镜像站启用
- jqgrid为什么表头和数据之间有间隙_面试官:你看过Redis数据结构底层实现吗?...
- 自学python3 最好的入门书籍-清华学霸整理,Python入门到精通这几本书帮你快速入行...
- spring-boot 整合redis作为数据缓存
- OFFICE文档转换到PDF的几种方法与转换效率和性能的简单比较
- 23.1 智能DNS
- java根据word书签模板填充数据生成pdf输出
- Gateway GPRS support node (GGSN) 相关资料
- PTA 7-47 打印选课学生名单 (25 point(s))
- 【程序员进阶之路】吴恩达Deeplearning.ai课程学习全体验:深度学习必备课程
- CVE-2018-4878 flash漏洞复现
- Redis学习(二)之 Redis Cluster集群
- 微信公众号怎么集赞服务器,【微信点赞】_微信怎么点赞如何集赞微信点赞活动新手必备点赞教程...
- CookieSession会话技术
- 常用数学符号的 LaTeX 表示方法
- 11、struct 结构