A museum was represented by a square matrix that was filled with O, G, and W where O represented open space, G represented guards, and W represented walls. Write a function that accepts the square matrix and returns another square matrix where all of the O's in the matrix are replaced with the number of how many spaces they are from a guard, without being able to go through any walls.

思路:

用BFS,先找到第一个guard,然后做bfs,找到所有的guard路径中最短的,其中如果遇到墙或者路径长度大于最小的路径就可以break返回了。循环查找,直到结束。

Java:

class Solution {
class Position {
int i;
int j;
int distance; public Position(int i, int j, int dist) {
this.i = i;
this.j = j;
this.distance = dist;
} public Position() {
this.i = -1;
this.j = -1;
this.distance = -1;
}
} public static void main(String[] args) {
char[][] matrix = { { 'o', 'o', 'o', 'g', 'o' }, { 'o', 'o', 'w', 'o', 'o' }, { 'o', 'g', 'o', 'o', 'w' },
{ 'o', 'w', 'g', 'o', 'o' }, { 'w', 'o', 'o', 'o', 'g' } };
Solution sol = new Solution(); int[][] result = sol.findDistance(matrix); if (result == null) {
System.out.println("Invalid input Matrix");
} for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
} } public int[][] findDistance(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return null;
}
int[][] result = new int[matrix.length][matrix[0].length]; Queue<Position> q = new LinkedList<Position>();
// finding Guards location and adding into queue
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
result[i][j] = -1;
if (matrix[i][j] == 'g') {
q.offer(new Position(i, j, 0));
result[i][j] = 0;
}
}
} while (!q.isEmpty()) {
Position p = q.poll();
// result[p.i][p.j] = p.distance;
updateNeighbors(p, matrix, q, result);
}
return result;
} public void updateNeighbors(Position p, char[][] matrix, Queue<Position> q, int[][] result) {
int[][] indexArray = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; for (int[] index : indexArray) {
if (isValid(p.i + index[0], p.j + index[1], matrix, result)) {
result[p.i + index[0]][p.j + index[1]] = p.distance + 1;
q.offer(new Position(p.i + index[0], p.j + index[1], p.distance + 1));
}
}
} public boolean isValid(int i, int j, char[][] matrix, int[][] result) {
if ((i < 0 || i > matrix.length - 1) || (j < 0 || j > matrix[0].length - 1) || matrix[i][j] == 'w'
|| matrix[i][j] == 'g' || result[i][j] != -1) {
return false;
}
return true;
}
}

  

[CareerCup] Guards in a museum 博物馆的警卫的更多相关文章

  1. CareerCup Questions List 职业杯题目列表

    网站 www.careercup.com 上的题库列表 # Title Difficulty Company 1 Guards in a museum Hard F, G  2 Bomberman H ...

  2. McNay Art Museum【McNay艺术博物馆】

    McNay Art Museum When I was 17, I read a magazine artice about a museum called the McNay, once the h ...

  3. BZOJ 3270 博物馆 && CodeForces 113D. Museum 期望概率dp 高斯消元

    大前提,把两个点的组合看成一种状态 x 两种思路 O(n^7) f[x]表示在某一个点的前提下,这个状态经过那个点的概率,用相邻的点转移状态,高斯一波就好了 O(n^6) 想象成臭气弹,这个和那个的区 ...

  4. UVA 11080 - Place the Guards(二分图判定)

    UVA 11080 - Place the Guards 题目链接 题意:一些城市.之间有道路相连,如今要安放警卫,警卫能看守到当前点周围的边,一条边仅仅能有一个警卫看守,问是否有方案,假设有最少放几 ...

  5. less新手入门(四)—— Mixin Guards

    八.Mixin Guards 有条件的 mixin 当您想要匹配表达式时,相对于简单的值或特性,Guards是有用的.如果您熟悉函数式编程,您可能已经遇到过它们. 为了尽可能地保持CSS的声明性质,在 ...

  6. 【CodeForces - 598D】Igor In the Museum(bfs)

    Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...

  7. 【BZOJ-3270】博物馆 高斯消元 + 概率期望

    3270: 博物馆 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 292  Solved: 158[Submit][Status][Discuss] ...

  8. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  9. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

随机推荐

  1. Cookie、Session、Token那点事儿和前后端分离之JWT用户认证

    (两篇文章转自:https://www.jianshu.com/p/bd1be47a16c1:https://www.jianshu.com/p/180a870a308a) 什么是Cookie? Co ...

  2. [NOI2013]快餐店 / CF835F Roads in the Kingdom (基环树)

    题意 一颗基环树,选一对点使得这两个点的最短距离最大. 题解 相当于找基环树的直径,但是这个直径不是最长链,是基环树上的最短距离. 然后不会做. 然后看了ljh_2000的博客. 然后会了. 这道题最 ...

  3. PHP 连接MySQL

    连接MySQL 在我们访问MySQL数据库前,我们需要先连接到数据库服务器: 实例(MySQLi - 面向对象) <?php $servername = "localhost" ...

  4. [MUTC2013]idiots

    嘟嘟嘟 首先\(O(n ^ 2)\)大家都会,枚举最长边,然后找两条短边满足两边之大于第三边即可. 然后估计就没法优化了. 正难则反,如果枚举的两条短边小于等于第三边会怎么样呢?发现\(a_i \le ...

  5. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  6. Other-Website-Contents.md

    title: 本站目录 categories: Other sticky: 10 toc: true keywords: 机器学习基础 深度学习基础 人工智能数学知识 机器学习入门 date: 999 ...

  7. 【原创】go语言学习(十四)IO操作1

    目录: 格式化输入 格式化输出 终端输入输出背后的原理理 bufio包的使用 命令行参数处理理和urfave/cli使用 格式化输入 1.从终端获取⽤用户的输入 格式化输入 fmt.Scan(a …i ...

  8. 在vscode里面光标跳跃的问题

    https://tieba.baidu.com/p/5242680781?red_tag=2439355674 卸载 JS-CSS-HTML Formatter就可以了.卸载完还要加载一下哦

  9. AHOI2012树屋阶梯

    这玩意不是卡特兰的经典模型吗…… 我们设方案数为f(i),则f(0)=1,f(1)=1,f(2)=2,f(3)=5,(其实到这里你再手模一个就出来了)我们把左上角的矩形删掉,则会变成下方和右方两个更小 ...

  10. nodejs配置QQ企业邮箱

    安装模块 npm install -g nodemailer npm install -g nodemailer-smtp-transport 代码示例 var nodemailer = requir ...