Careercup - Facebook面试题 - 6685828805820416
2014-05-02 02:33
原题:
Given the following by grid where the (first row, first column) is represented by (,): , , ,
, , ,
, , null we need to find if we can get to each cell in the table by following the cell locations at the current cell we are at. We can only start at cell (,) and follow the cell locations from that cell, to the cell it indicates and keep on doing the same for every cell.
题目:有一个3乘3的矩阵,从每个方格可以跳到其它方格,也可能无法继续跳。如果规定从(0, 0)出发,请判断能否走完所有方格?
解法:一边跳一边标记即可,其实作为任何规模的矩阵,解法都是一样:用一个counter来统计剩余的方格数,同时用O(n^2)的空间来标记每个方格是否被走到了。有时候可以想办法在原来的矩阵上做标记,避免额外的空间开销。
代码:
// http://www.careercup.com/question?id=6685828805820416
#include <cstdio>
#include <vector>
using namespace std; struct Point {
int x;
int y;
Point(int _x = , int _y = ): x(_x), y(_y) {};
}; class Solution {
public:
bool canReachAll(vector<vector<Point> > &grid) {
int n, m; n = (int)grid.size();
if (n == ) {
return false;
}
m = (int)grid[].size();
if (m == ) {
return false;
} int cc = n * m;
Point p(, );
Point next_p; while (true) {
next_p = grid[p.x][p.y];
grid[p.x][p.y].x = n;
grid[p.x][p.y].y = m;
--cc;
p = next_p;
if (p.x < && p.y < ) {
// null terminated
break;
}
if (grid[p.x][p.y].x == n && grid[p.x][p.y].y == m) {
// already visited
break;
}
} return cc == ;
};
}; int main()
{
vector<vector<Point> > grid;
int n, m;
int i, j;
Solution sol; while (scanf("%d%d", &n, &m) == && (n > && m > )) {
grid.resize(n);
for (i = ; i < n; ++i) {
grid[i].resize(m);
}
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
scanf("%d%d", &grid[i][j].x, &grid[i][j].y);
}
}
printf(sol.canReachAll(grid) ? "Yes\n" : "No\n");
} return ;
}
Careercup - Facebook面试题 - 6685828805820416的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- HTML_创建易用的Web表单
首先创建一个表单域集合fieldset fieldset元素允许Web开发者将主题相关的表单组合在一起 <fieldset></fieldset> 要说明的是本例子中每个表单都 ...
- 九、Android学习笔记_ Android开发中使用软引用和弱引用防止内存溢出
在<Effective Java 2nd Edition>中,第6条“消除过期的对象引用”提到,虽然Java有 垃圾回收机制,但是只要是自己管理的内存,就应该警惕内存泄露的问题,例如的对象 ...
- K3Cloud单据转换获取源单数据
private DynamicObject sourceBill = null; object fid; //获取源单数据(选单) public overri ...
- 放弃SCOPE_Identity,使用OUTPUT代替
最近项目中使用了SCOPE_IDENTITY()来获取新增数据的自动递增ID号. 在运行过程中会不时的发生无法通过SCOPE_IDENTITY()来获取ID号的情况. 尝试着测试又发现不了问题. 今天 ...
- 教-----------有时候就是那么纠结,教的时候不提问题,好像很懂,最后又来纠缠你!真是ctmb
A热心满满教导B 几分钟后...B”都懂了“ B几分钟后.又把你叫来.(走过去,您好,我还有个问题,能帮我回答下吗?不耽误你多少时间,) A已经走一段距离 , 思维已经在别的事情上了,变得好没有耐 ...
- JAVA JDBC 元数据分析小结
纯干货: 获取数据库名称: /** * 获取数据库的名称 */ public void getDataBaseName() throws Exception { Connection con = DS ...
- netstat命令[转]
原文地址:http://www.cnblogs.com/peida/archive/2013/03/08/2949194.html netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的 ...
- linux 系统运行级别及修改[转]
Linux运行级别从0-6,共7个. 0:关机.不能将系统缺省运行级别设置为0,否则无法启动. 1:单用户模式,只允许root用户对系统进行维护. 2:多用户模式,但不能使用NFS(相当于Window ...
- 杭电2034——人见人爱A-B
#include <stdio.h> #include <algorithm> using namespace std; int main () { int a[110],b[ ...
- 汇编中PTR常见的几种用法
汇编中PTR的用法确实是令人比较头疼的,我特意搜集了一些PTR的应用实例,可以从例子中揣摩出规律: 1.MOV WORD PTR [DI],OFFSET BUF1 2.SUB BYTE ...