Careercup - Google面试题 - 4716965625069568
2014-05-06 00:17
原题:
Given a -D matrix represents the room, obstacle and guard like the following ( is room, B->obstacle, G-> Guard): B G G
B calculate the steps from a room to nearest Guard and set the matrix, like this B G G
B
Write the algorithm, with optimal solution.
题目:有一个二维矩阵表示的迷宫,其中G表示保安人员,B表示不可穿越的墙,其他位置均为空地。如果每次可以向东南西北四个方向移动一格,请计算出每个空格离最近的保安有多远。题目给出了一个示例。
解法:既然矩阵里可能有多个保安,我的思路是以各个保安为中心,进行广度优先搜索,搜索的过程中随时更新每个空格位置的最短距离,保证全部搜索完之后所有的结果都是最小的。单次BFS的时间代价是O(n^2)级别的。
代码:
// http://www.careercup.com/question?id=4716965625069568
#include <queue>
#include <vector>
using namespace std; class Solution {
public:
void solve(vector<vector<int> > &matrix) {
// -1 for guard
// -2 for blockade
// 0 for room
// > 0 for distance n = (int)matrix.size();
if (n == ) {
return;
}
m = (int)matrix[].size();
if (m == ) {
return;
} int i, j; for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
if (matrix[i][j] == -) {
doBFS(matrix, i, j);
}
}
}
};
private:
int n, m; bool inBound(int x, int y) {
return x >= && x <= n - && y >= && y <= m - ;
} void doBFS(vector<vector<int> > &matrix, int x, int y) {
queue<int> q;
static int dd[][] = {{-, }, {+, }, {, -}, {, +}};
int tmp;
int xx, yy;
int i;
int dist; q.push(x * m + y);
while (!q.empty()) {
tmp = q.front();
q.pop();
x = tmp / m;
y = tmp % m;
dist = matrix[x][y] > ? matrix[x][y] : ;
for (i = ; i < ; ++i) {
xx = x + dd[i][];
yy = y + dd[i][];
if (!inBound(xx, yy) || matrix[xx][yy] < ||
(matrix[xx][yy] > && matrix[xx][yy] <= dist + )) {
// out of boundary
// a guard or a blockade
// the distance is no shorter
continue;
}
matrix[xx][yy] = dist + ;
q.push(xx * m + yy);
}
}
}
};
Careercup - Google面试题 - 4716965625069568的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- disucz!NT 3.5.0 验证码点击不能变化只是样式变化
来博客园这么久了,第一次写博客啊!公司有个论坛10年没动了,是discuz!NT 3.5.0版本的,由于验证码不能变化老是被人刷.网上找了很多资料根本没有.可能有同行下次也会遇到这样的问题,我就把我的 ...
- Java 专业人士必备的书籍和网站列表
对于 Java™ 语言开发人员来说,信息过量是一个真正的问题.每个新入行的程序员都要面临一个令人畏缩的挑战:要进入的行业是一个具有海量知识的行业.要了解的东西简直 太多了.对于有经验的老手来说,情况只 ...
- C# 线程抛异常
异常抛出 异常抛出要在线程代码中抛出,否则捕获不到 using System; using System.Threading; namespace testthread_keyword_lock { ...
- WPF非UI线程获取修改控件属性值的方法
public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, ...
- 如何安装 JAVA 7 (JDK 7u75) 在 CentOS/RHEL 7/6/5 Fedora
先下载JDK For 64 Bit:- # cd /opt/ # wget --no-cookies --no-check-certificate --header "Cookie: gpw ...
- 获取一年时间的sql
select a.day, to_char(a.day, 'day') as dd, 1 as flag,to_char(a.day,'YYYY-MM-DD') from ( SELECT TO_DA ...
- jQuery自定义Web页面鼠标右键菜单
jQuery自定义Web页面鼠标右键菜单 右键菜单是固定的,很多时候,我们需要自定义web页面自定义菜单,指定相应的功能. 自定义的原理是:jQuery封装了鼠标右键的点击事件(“contextmen ...
- js 正则表达式 手机号
js--手机号验证 //注册验证 function RegsiterBtn() { var regphone = $("#regphone").val(); var regreal ...
- OC4_XML文件解析
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-// ...
- java synchronized关键字浅探
synchronized 是 java 多线程编程中用于使线程之间的操作串行化的关键字.这种措施类似于数据库中使用排他锁实现并发控制,但是有所不同的是,数据库中是对数据对象加锁,而 java 则是对将 ...