[CareerCup] Guards in a museum 博物馆的警卫
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 博物馆的警卫的更多相关文章
- CareerCup Questions List 职业杯题目列表
网站 www.careercup.com 上的题库列表 # Title Difficulty Company 1 Guards in a museum Hard F, G 2 Bomberman H ...
- 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 ...
- BZOJ 3270 博物馆 && CodeForces 113D. Museum 期望概率dp 高斯消元
大前提,把两个点的组合看成一种状态 x 两种思路 O(n^7) f[x]表示在某一个点的前提下,这个状态经过那个点的概率,用相邻的点转移状态,高斯一波就好了 O(n^6) 想象成臭气弹,这个和那个的区 ...
- UVA 11080 - Place the Guards(二分图判定)
UVA 11080 - Place the Guards 题目链接 题意:一些城市.之间有道路相连,如今要安放警卫,警卫能看守到当前点周围的边,一条边仅仅能有一个警卫看守,问是否有方案,假设有最少放几 ...
- less新手入门(四)—— Mixin Guards
八.Mixin Guards 有条件的 mixin 当您想要匹配表达式时,相对于简单的值或特性,Guards是有用的.如果您熟悉函数式编程,您可能已经遇到过它们. 为了尽可能地保持CSS的声明性质,在 ...
- 【CodeForces - 598D】Igor In the Museum(bfs)
Igor In the Museum Descriptions 给你一个n*m的方格图表示一个博物馆的分布图.每个方格上用'*'表示墙,用'.'表示空位.每一个空格和相邻的墙之间都有一幅画.(相邻指的 ...
- 【BZOJ-3270】博物馆 高斯消元 + 概率期望
3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 292 Solved: 158[Submit][Status][Discuss] ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
随机推荐
- JavaScript常用的方法
indexOf() 功能:indexOf() 方法返回调用 String 对象中第一次出现的指定值的索引. 语法:indexOf(searchValue, fromIndex) searchValue ...
- docker学习(五)
一.实战案例介绍一些典型的应用场景和案例. 1.使用Supervisor来管理进程Docker 容器在启动的时候开启单个进程,比如,一个 ssh 或者 apache 的 daemon 服务.但我们经常 ...
- guava字符串工具 Splitter 主要功能是拆分字符串为集合 Map
public class SplitterTest { public static void main(String args[]){ //1.拆分字符串为List集合 String str=&quo ...
- 「CF375E」Red and Black Tree「树形DP」
题意 给定一个结点颜色红或黑的树,问最少进行多少次交换黑.红结点使得每个红结点离最近的黑结点距离\(\leq x\). \(1\leq n \leq 500, 1 \leq x \leq 10^9\) ...
- 国外最受欢迎的BT-磁力网站
1.海盗湾 The Pirate Bay 2.KickAssTorrents 3.Torrentz 4.zooqle 5.SumoTorrent 6.TorrentDownloads 7.Rarbg ...
- git 常用命令使用,git bash通用命令
git 常用命令 1.强制推送(慎用,除非你认为其他冲突等可以丢弃 或者不是很重要) git push -- force 2.创建文件等小命令 touch a // 创建一个a文件 >> ...
- 用Python匹配HTML tag的时候,<.>和<.?>有什么区别?
答:术语叫贪婪匹配( <.> )和非贪婪匹配(<.?> ) 例如: test <.*> : test <.*?> :
- 三十一、Gawk基础入门
AWK:Aho Weinberger Kernighan awk :报告生成器.格式化文本输出 一.gawk - pattern scanning and processing language 基本 ...
- Java中BigDecimal的equals与compareTo的区别
有个是否为零的判断[BigDecimal.ZERO.equals(ratio)]我用了BigDecimal的equals方法,结果,判断失败,因此特地分析一下equals与compareT ...
- 模糊查询(附上源码和jquery-1.12.1.js,jquery-ui.js,jquery-ui.css)
直接上源码: <!doctype html> <html lang="en"> <head> <meta charset="ut ...