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 ...
随机推荐
- ASP.NET缓存全解析3:页面局部缓存 转自网络原文作者李天平
有时缓存整个页面是不现实的,因为页的某些部分可能在每次请求时都需要变化.在这些情况下,只能缓存页的一部分.顾名思义,页面部分缓存是将页面部分内容保存在内存中以便响应用户请求,而页面其他部分内容则为动态 ...
- jquery实现点击页面空白隐藏指定菜单
注意:dmenu是一个div的class名哦 代码如下 复制代码 $('html,body').click(function(e){ if(e.target.id.indexOf("dme ...
- 基于jquery的inputlimiter 实现字数限制功能
html代码: <td>内容摘要:</td> <td> <textarea id="content_summary" rows=5 nam ...
- Oracle数据库对象_视图
视图是一种非常重要的数据库对象,它的形式类似于普通表,我们可以从视图中查询数据. 实际上它是建立在表上的一种虚表,在视图中并不存储真正的数据,而是仅仅保存一条SELECT语句,对视图的访问将被转化为对 ...
- 苹果系统开发中的混合编程(2):Swift和C的相互调用
在进行Swift和C之间的相互调用时,有必要先了解一下两种语言之间的类型转换关系: C 类型 Swift 类型 bool CBool char, signed char CChar unsig ...
- Swift数据类型及数据类型转换
整型 Swift 提供 8.16.32.64 位形式的有符号及无符号整数.这些整数类型遵循 C 语言的命名规 约,如 8 位无符号整数的类型为 UInt8,32 位 有符号整数的类型为 Int32 ...
- iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/
http://zanderzhang.gitcafe.io/2015/09/19/iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/ 百度外卖选择送货地址: 饿了么选择送货地 ...
- 20150216—winform中的DataGridView
DataGridView的主要作用是用来按列表来显示信息,其信息的数据源可以是SQL数据库,也可以是一个列表式的集合. DataGridView的位置:工具箱--数据--DataGridView.如下 ...
- OpenGL第12-14讲小结
首先要为自己为什么没有写第10讲的控制3D场景和第11讲的红旗飘飘呢?因为没看啊~哈哈哈,而且我尝试着运行红旗飘飘的时候电脑蓝屏了(可能不是它的锅),暂时跳过了. 恩,12到14主要了解了这么些东西, ...
- centos6.5 安装mono
mono是一个在linux下兼容.net的软件.安装之前要把开发包装好 源码安装mono wget http://download.mono-project.com/sources/mono/mono ...