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思想设计一个计时器类. 解法:我根据自 ...
随机推荐
- .NET程序员吧需要知道的小知识——关于数据库
关于数据库 作为一个有“情怀的”(B格高一些的).NET开发工程师,需要多少知道一些这样的小故事. 哪怕仅仅当作一些扯淡的谈资. 1.文件型数据库(常见的) Access SQLite SQLSe ...
- MySQL查询昨天、今天、7天、近30天、本月、上一月数据
文章同步发表在博主网站朗度云,传输门:http://www.wolfbe.com/detail/201608/291.html 在开发或者统计时,我们可能需要统计某个表的数据.比如:查看今天新增的 ...
- docker开发_在basic image的基础上创建自定义的image
方法一:docker commit 1. 跑一个basic image,docker新建了一个容器 root@ubuntu:/home/thm/docker/test# docker run -i - ...
- MySQL深入利用Ameoba实现读写分离
3 ameoba安装配置 3.1 安装配置JDK [root@stu15 ~]# rpm -ivh jdk-7u67-linux-x64.rpm [root@stu15 ~]# cd /usr/j ...
- Mysql的ssl主从复制+半同步主从复制
Mysql的ssl主从复制+半同步主从复制 准备工作 1.主从服务器时间同步 [root@localhost ~]# crontab -e */30 * * * * /usr/sbin/ntpdate ...
- Curses library not found. Please install appropriate package
今天安装mysql-5.5.3-m3的时候,报下面的错误: -- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_ ...
- PHPExcel上传sae遇到: -1:fail to get xml content
在用PHPExcel1.8.0来处理excel时,本地测试时好使的,但是要把代码部署到SAE,在上传代码的时候就会遇到这个问题. 部署代码中遇到问题: -1:fail to get xml conte ...
- php 递归 适合刚刚接解递归的人看
递归,就是自己调用自己,当满足某条件时层层退出(后进先出). --------------------------------------------------------------------- ...
- PHP-You don’t have permissions to access xxx on this server!
问题如下图: 如果你是想要查看目录下的每一个文件,那么你需要修改一下httpd-conf配置文件,也就是apache的配置文件,以phpStudy2013为例,如下图打开: 然后找到如下部分,添加 ...
- Android四大组件一----Activity
最新面试需要复习一下Android基础. {所谓Activity} 通俗点:app上看到的窗口基本都是Activity Android 程序一般是由多个Activity组成,用户看到的能够交互的窗口通 ...