2014-05-06 00:17

题目链接

原题:

  1. Given a -D matrix represents the room, obstacle and guard like the following ( is room, B->obstacle, G-> Guard):
  2.  
  3. B G G
  4. B
  5.  
  6. calculate the steps from a room to nearest Guard and set the matrix, like this
  7.  
  8. B G G
  9. B
  10. Write the algorithm, with optimal solution.

题目:有一个二维矩阵表示的迷宫,其中G表示保安人员,B表示不可穿越的墙,其他位置均为空地。如果每次可以向东南西北四个方向移动一格,请计算出每个空格离最近的保安有多远。题目给出了一个示例。

解法:既然矩阵里可能有多个保安,我的思路是以各个保安为中心,进行广度优先搜索,搜索的过程中随时更新每个空格位置的最短距离,保证全部搜索完之后所有的结果都是最小的。单次BFS的时间代价是O(n^2)级别的。

代码:

  1. // http://www.careercup.com/question?id=4716965625069568
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Solution {
  7. public:
  8. void solve(vector<vector<int> > &matrix) {
  9. // -1 for guard
  10. // -2 for blockade
  11. // 0 for room
  12. // > 0 for distance
  13.  
  14. n = (int)matrix.size();
  15. if (n == ) {
  16. return;
  17. }
  18. m = (int)matrix[].size();
  19. if (m == ) {
  20. return;
  21. }
  22.  
  23. int i, j;
  24.  
  25. for (i = ; i < n; ++i) {
  26. for (j = ; j < m; ++j) {
  27. if (matrix[i][j] == -) {
  28. doBFS(matrix, i, j);
  29. }
  30. }
  31. }
  32. };
  33. private:
  34. int n, m;
  35.  
  36. bool inBound(int x, int y) {
  37. return x >= && x <= n - && y >= && y <= m - ;
  38. }
  39.  
  40. void doBFS(vector<vector<int> > &matrix, int x, int y) {
  41. queue<int> q;
  42. static int dd[][] = {{-, }, {+, }, {, -}, {, +}};
  43. int tmp;
  44. int xx, yy;
  45. int i;
  46. int dist;
  47.  
  48. q.push(x * m + y);
  49. while (!q.empty()) {
  50. tmp = q.front();
  51. q.pop();
  52. x = tmp / m;
  53. y = tmp % m;
  54. dist = matrix[x][y] > ? matrix[x][y] : ;
  55. for (i = ; i < ; ++i) {
  56. xx = x + dd[i][];
  57. yy = y + dd[i][];
  58. if (!inBound(xx, yy) || matrix[xx][yy] < ||
  59. (matrix[xx][yy] > && matrix[xx][yy] <= dist + )) {
  60. // out of boundary
  61. // a guard or a blockade
  62. // the distance is no shorter
  63. continue;
  64. }
  65. matrix[xx][yy] = dist + ;
  66. q.push(xx * m + yy);
  67. }
  68. }
  69. }
  70. };

Careercup - Google面试题 - 4716965625069568的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. ASP.NET缓存全解析3:页面局部缓存 转自网络原文作者李天平

    有时缓存整个页面是不现实的,因为页的某些部分可能在每次请求时都需要变化.在这些情况下,只能缓存页的一部分.顾名思义,页面部分缓存是将页面部分内容保存在内存中以便响应用户请求,而页面其他部分内容则为动态 ...

  2. jquery实现点击页面空白隐藏指定菜单

    注意:dmenu是一个div的class名哦 代码如下 复制代码 $('html,body').click(function(e){  if(e.target.id.indexOf("dme ...

  3. 基于jquery的inputlimiter 实现字数限制功能

    html代码: <td>内容摘要:</td> <td> <textarea id="content_summary" rows=5 nam ...

  4. Oracle数据库对象_视图

    视图是一种非常重要的数据库对象,它的形式类似于普通表,我们可以从视图中查询数据. 实际上它是建立在表上的一种虚表,在视图中并不存储真正的数据,而是仅仅保存一条SELECT语句,对视图的访问将被转化为对 ...

  5. 苹果系统开发中的混合编程(2):Swift和C的相互调用

      在进行Swift和C之间的相互调用时,有必要先了解一下两种语言之间的类型转换关系:   C 类型 Swift 类型 bool CBool char, signed char CChar unsig ...

  6. Swift数据类型及数据类型转换

    整型  Swift 提供 8.16.32.64 位形式的有符号及无符号整数.这些整数类型遵循 C 语言的命名规 约,如 8 位无符号整数的类型为 UInt8,32 位 有符号整数的类型为 Int32 ...

  7. iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/

    http://zanderzhang.gitcafe.io/2015/09/19/iOS-Andriod百度地图仿百度外卖-饿了么-选择我的地址-POI检索/ 百度外卖选择送货地址: 饿了么选择送货地 ...

  8. 20150216—winform中的DataGridView

    DataGridView的主要作用是用来按列表来显示信息,其信息的数据源可以是SQL数据库,也可以是一个列表式的集合. DataGridView的位置:工具箱--数据--DataGridView.如下 ...

  9. OpenGL第12-14讲小结

    首先要为自己为什么没有写第10讲的控制3D场景和第11讲的红旗飘飘呢?因为没看啊~哈哈哈,而且我尝试着运行红旗飘飘的时候电脑蓝屏了(可能不是它的锅),暂时跳过了. 恩,12到14主要了解了这么些东西, ...

  10. centos6.5 安装mono

    mono是一个在linux下兼容.net的软件.安装之前要把开发包装好 源码安装mono wget http://download.mono-project.com/sources/mono/mono ...