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 ...
随机推荐
- android 使用NinePatch图作Background,导致布局混乱
在Layout中使用NinePatch图片作为其布局的背景图片时,可能会导致Layout中的布局发生混乱,此时将此Layout设置一个属性:android:padding="0dp" ...
- 联系电话正则表达式(jquery表单验证)
一.实现的效果图: 二.CSS样式 /*验证样式*/ .onError{ vertical-align: middle; color: #ff0000; line-height: 22px; padd ...
- php删除数组中相同的元素,只保留一个相同元素
<?php// 删除数组中相同元素,只保留一个相同元素function formatArray($array){sort($array);$tem = ”;$temarray = array() ...
- QtPropertyBrowser+vs2010的安装与配置(转)
这一篇文章有些问题,后又写了一篇,地址是http://www.cnblogs.com/aminxu/p/4552410.html 转自http://blog.csdn.net/jingwenlai_s ...
- Ajax Array Json 示例
function functionName(){ var list=new Array(); $("td.classA").each(function(){ list.push($ ...
- [Android开发系列]IT博客应用V1.3
首先,感谢使用这款软件并给我意见的朋友们,有你们的意见,才有了这个版本. 其次,检索功能和分类筛选功能(如果是你提的意见,记得在下面mark哦,毕竟读代码你能发现,其实发意见这个就是用自己的邮箱给自己 ...
- iOS-RunLoop,为手机省电,节省CPU资源,程序离不开的机制
RunLoop是什么?基本操作是什么? 1.RunLoop的作用 RunLoop可以: 保持程序的持续运行 处理App中的各种事件(比如触摸事件.定时器事件.Selector事件) 节省CPU资源,提 ...
- 【Unity3D】Unity3D之 Resources.Load 动态加载资源
[Unity3D]Unity3D之 Resources.Load 动态加载资源 1.Resources.Load:使用这种方式加载资源,首先需要下Asset目录下创建一个名为Resources的文件夹 ...
- (转)RabbitMQ 集群与高可用配置
集群概述 环境 配置步骤 集群概述 通过 Erlang 的分布式特性(通过 magic cookie 认证节点)进行 RabbitMQ 集群,各 RabbitMQ 服务为对等节点,即每个节点都提供服务 ...
- ubuntu下安装git,sublime,nodejs
用的是VMware10.0版本的虚拟机,很早之前下载的今天就直接用了,安装挺简单记得需要一个序列号.在这里:http://mirrors.163.com/ubuntu-releases/15.04/u ...