2014-05-06 07:11

题目链接

原题:

Find a shortest path in a N*N matrix maze from (,) to (N,N), assume  is passable,  is not,  is destination, use memorization to cache the result. Here is my code. I am not sure if I am caching it right.

题目:给定一个N * N的矩阵,找出从(0, 0)到(n - 1, n - 1)的最短路径。此人在后面还贴上了自己的代码,从代码水平上看此人实力基本不足以参加Google的面试,此人叫“Guy”。

解法:对于这种移动距离限于一格的最短路径问题,最直接的思想就是BFS了。而这位老兄居然还把自己四路DFS的代码贴出来,多余的也就没必要评论了。此题要求得到完整的最短路径,所以进行BFS的同时,需要记录每个点的回溯位置。这样就可以从目的地一直回溯到出发点,得到一条完整的最短路径。算法的整体时间复杂度是O(n^2)的。

代码:

 // http://www.careercup.com/question?id=5634470967246848
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; int main()
{
int n;
vector<vector<int> > v;
queue<int> q;
int i, j, k;
int x, y;
int dx, dy;
int d[][] = {
{-, },
{+, },
{, -},
{, +}
};
int back[] = {, , , };
vector<vector<int> > trace;
vector<int> path;
int tmp; while (scanf("%d", &n) == && n > ) {
v.resize(n);
trace.resize(n);
for (i = ; i < n; ++i) {
v[i].resize(n);
trace[i].resize(n);
} for (i = ; i < n; ++i) {
for (j = ; j < n; ++j) {
scanf("%d", &v[i][j]);
if (v[i][j] == ) {
dx = i;
dy = j;
}
}
} v[][] = -;
q.push();
while (v[dx][dy] > && !q.empty()) {
tmp = q.front();
q.pop();
i = tmp / n;
j = tmp % n;
for (k = ; k < ; ++k) {
x = i + d[k][];
y = j + d[k][];
if (x < || x > n - || y < || y > n - ) {
continue;
}
if (v[x][y] > ) {
v[x][y] = v[i][j] - ;
trace[x][y] = back[k];
q.push(x * n + y);
}
}
}
while (!q.empty()) {
q.pop();
} if (v[dx][dy] < ) {
x = dx;
y = dy;
while (x || y) {
path.push_back(x * n + y);
i = x + d[trace[x][y]][];
j = y + d[trace[x][y]][];
x = i;
y = j;
}
path.push_back();
while (!path.empty()) {
x = path.back() / n;
y = path.back() % n;
printf("(%d, %d)", x, y);
path.pop_back();
}
putchar('\n');
} else {
printf("Unreachable.\n");
} for (i = ; i < n; ++i) {
v[i].clear();
trace[i].clear();
}
v.clear();
trace.clear();
path.clear();
} return ;
}

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

  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面试题 - 5680330589601792

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

  6. Careercup - Google面试题 - 5424071030341632

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

  7. Careercup - Google面试题 - 5377673471721472

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

  8. Careercup - Google面试题 - 6331648220069888

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

  9. Careercup - Google面试题 - 5692127791022080

    2014-05-08 22:09 题目链接 原题: Implement a class to create timer object in OOP 题目:用OOP思想设计一个计时器类. 解法:我根据自 ...

随机推荐

  1. javaSE第十二天

    第十二天    64 1. Scanner的使用(了解)    64 (1)在JDK5以后出现的用于键盘录入数据的类.    64 (2)构造方法:    64 (4)要掌握的两个方法    65 ( ...

  2. (一)、NodeJS (转载)

    NodeJS基础                                                 ------ 转载自阿里巴巴 什么是NodeJS JS是脚本语言,脚本语言都需要一个解 ...

  3. 软件工程 speedsnail 冲刺1

    2015-5-5 完成任务:背景音乐的添加: 遇到问题:循环播放的设置: 明日任务:snail的移动功能.

  4. VS2008安装SP1补丁后智能提示从中文变为英文的解决办法

    如果你安装了中文的VS2008,打了SP1补丁之后出现问题,那是微软的Bug,请下载此补丁修正: VS90SP1-KB957507-CHS-x86.exe 点击下载

  5. c# await 关键字错误

    private void OnUnlockCommand(object parameter) {    StorageFile file = await Windows.Storage.Applica ...

  6. form表单无刷新提交文件(iframe)

    先看一段代码(PHP例子) 1.表单代码(form.html): <iframe name="testIframeName" style="display:none ...

  7. 两种会话状态之Session会话

    什么是Session 使用Cookie和附加URL参数都可以将上一次请求的状态信息传递到下一次请求中,但是如果传递的状态信息较多,将极大降低网络传输效率和增大服务器端程序处理的难度. Session技 ...

  8. Discuz X3.2 分区 gid 完美伪静态方法 Apache/Nginx

    Discuz 官方给出的伪静态规则并不完整,只实现了部分的伪静态设置及规则,分区 gid 仍然是 forum.php?gid=xxx 的形式,对于有强迫症的我是无法忍受的,下面给出分区 gid 的伪静 ...

  9. android属性

    一.布局 1.android:layout_gravity和android:gravity的区别 android:gravity 对齐方式,它是相对于控件本身对齐:android:layout_gra ...

  10. ARM-Linux S5PV210 UART驱动(3)----串口核心层、关键结构体、接口关系

    尽管一个特定的UART设备驱动完全可以按照tty驱动的设计方法来设计,即定义tty_driver并实现tty_operations其中的成员函数,但是Linux已经在文件serial_core.c中实 ...