Careercup - Google面试题 - 5634470967246848
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的更多相关文章
- 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面试题 - 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 ...
- Careercup - Google面试题 - 5692127791022080
2014-05-08 22:09 题目链接 原题: Implement a class to create timer object in OOP 题目:用OOP思想设计一个计时器类. 解法:我根据自 ...
随机推荐
- 第1部分: 游戏引擎介绍, 渲染和构造3D世界
原文作者:Jake Simpson译者: 向海Email:GameWorldChina@myway.com ---------------------------------------------- ...
- 一款安卓ShowcaseView视图源码效果
该源码是从源码天堂那边转载过来的,大家可以看看一下吧啊,一款安卓ShowcaseView视图源码效果,非常不错的,特别是在做引导时使用. 源码下载地址:http://code.662p.com/vie ...
- 【风马一族_mysql】MySQL免安装版环境配置图文教程
mysql存放在某一个磁盘中(笔者使用E盘) 配置系统变量 打开 电脑的属性 点击 高级系统设置 选择 高级 点击 环境变量 选择 系统变量 点击 变量Path,追加 值 E:\mysql-5.6.2 ...
- [转]CentOS开机启动脚本
转载自http://www.2cto.com/os/201306/220559.html 我的一个Centos开机自启动脚本的制作 一.切换到/etc/init.d/ 二.制作sh脚本 v ...
- 软件工程 speedsnail 冲刺3
2015-5-7 完成任务:环境崩溃,重新配置环境,学习了黑马android教学视频前6集: 遇到问题: 问题1 Error: Error parsing C:\Users\sam\.android\ ...
- 掌握iOS自动布局
1,自动布局是一种基于约束的布局(constraint-based layout)引擎,它可以根据开发者在对象上的约束自动调整大小与位置. 2.在iOS 6之前使用的布局模型是“spring& ...
- 反编译工具(.NET Reflector )
1.下载安装原程序,然后运行 Reflector.exe 2.运行注册机,点击 Generate 获得序列号 3.把序列号填写到 Reflector 软件上4.断开网络,点击 Activate 激活( ...
- 重拾C,一天一点点_12
连续两天没写了,今天继续! sizeof 对象 或 sizeof (类型名) 返回一个整型值,等于指定对象或类型占用的存储空间字节数.(返回值是无符号整型值,其类型为size_t,在头文件<st ...
- PHP面向对象之旅:static变量与方法
static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或“类方法”. 如果访问控制权限允许,可不必创建该类对象而直接使用类名加两个冒号“ ...
- LotusPhp入口文件解析
LotusPhp也是单入口的框架,可以根据需要开启多个应用实例 例如前台页面可以用index.php作为入口文件,后台可以用admin.php作为入口文件,多个应用实例可以共享应用配置和类库或者根本每 ...