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的更多相关文章

  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. 结合setTimeout和clearTimeout,实现“返回顶部”的功能

    结合setTimeout和clearTimeout,当页面停止滚动时,“返回顶部”按钮淡隐淡出.点击“返回顶部”页面以动画形式返回顶部.完美兼容ie6-11,firefox,chrome等. html ...

  2. 判断checked是否选中

    if($('#checkbox-id').is(':checked')) {     // do something }

  3. Table of Contents - 设计模式

    设计原则 OCP - 开闭原则 SRP - 单一职责原则 DIP - 依赖倒置原则 ISP - 接口隔离原则 LSP - 里氏替换原则 LoD - 迪米特法则 创建型模式 工厂方法模式 抽象工厂模式 ...

  4. Java之阶乘数的计算

    说起“阶乘数”,我们应该都不会感到陌生.当老师布置了这样的作业,我们大多数人是一贯用笔算,还有的同学会用计算机去计算.数学是讲究原理和方法的,我们知其然,也要知其所以然.下面我们就用编程来计算阶乘数. ...

  5. Struts2基本包作用详解

    asm-3.3.jar作用:操作java字节码的类库包路径及主要类:未提供 asm-commons-3.3.jar作用:提供了基于事件的表现形式包路径及主要类:未提供 asm-tree-3.3.jar ...

  6. 【Linux C中文函数手册】之 内存和字符串函数

    内存和字符串函数 1) bcmp 比较内存内容 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp表头文件 #include<stri ...

  7. 第六十七篇、OC_UITableView head下拉图片放大的效果

    (一) 布置UITableview 我们首先要通过设置UITableview的内容偏移 self.tableView.contentInset 来为图片视图留出位置,这里我们的图片高度暂定为280 ; ...

  8. 【学习笔记】【C语言】指针

    一.指针变量的定义 1. 格式:变量类型 *指针变量名; 2. 举例:int *p;   char *p2; 3. 注意:定义变量时的*仅仅是指针变量的象征 二.利用指针变量简单修改其他变量的值 1. ...

  9. 用实体框架搭建MVC程序框架(全部)

    第一步:1.新建项目 2.新建domain类库 3.新建Data类库 4.为上面的1.2.3添加实体框架nuget包.(可以右键管理nuget包来查找entityframework,当然也可以通过程序 ...

  10. WAS缓存导致修改文件不生效问题

    当用ftp上传部署增量文件,was应用不生效问题解决方法:一. 修改web.xml文件,需要修改以下三个目录下的文件:1. /opt/IBM/WebSphere/AppServer/profiles/ ...