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 ...
随机推荐
- 【学习笔记】【C语言】字符串数组
1.使用场合 * 一维字符数组中存放一个字符串,比如一个名字char name[20] = "mj" * 如果要存储多个字符串,比如一个班所有学生的名字,则需要二维字符数组,cha ...
- C# Winform里面用Console.WriteLine输出到哪了
C# Winform里面用Console.WriteLine输出也不会报错 显示在 VS IDE 的视图→输出窗口,且只在 Debug 环境下此语句执行. 如果是 Release 环境,在 Win32 ...
- TreeView控件的CheckBox级联选中或取消
背景: 在一个项目开发中遇到这样的要求:当选中树中一个节点时,需要同时选中其父节点,直至根节点.在取消一个节点的选中时,需要将其所有子节点取消选中,直至叶子节点.由于项目用户体验暂时可以不用考虑,直接 ...
- C语言清除输入缓存方法记录[转]
原文:http://www.tomytime.com/archives/218/ fflush()函数是标准的作法. setbuf(stdin,NULL)是GCC下可用的一种方法. scanf(&qu ...
- Android开发之切换activity动画overridePendingTransition
原文地址:http://blog.sina.com.cn/s/blog_706c449f01011s3v.html overridePendingTransition 在startActivity() ...
- 7款经典炫酷的HTML5/jQuery动画应用示例及源码
jQuery是一款普遍受前端开发者欢迎的Javascript框架,但是开发者貌似更关注jQuery开发的插件,海量的jQuery插件让前端开发者非常方便.HTML5的加入让jQuery这个家族更加丰富 ...
- 7款外观迷人的HTML5/CSS3 3D按钮特效
1.CSS3超酷3D弹性按钮 按钮实现非常简单 今天我又要向大家分享一款实现超级简单的CSS3 3D弹性按钮,它在鼠标按下时不仅从视觉上感受到3D立体的效果,而且更有弹性的动画特效,非常可爱. 在线演 ...
- 常见web服务器错误
参考地址:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5 10 Status Code Definitions ...
- J2EE5(Servlet2.5)对EL表达式的支持
JAVA EE5默认 支持EL表达式. 办法一:在每个jsp文件的最上方加入以下代码: <%@ page language="java" import="java. ...
- OpenGL第6、7讲小结
因为内容比较多,所以只看了两讲(强行解释). 一讲讲了如何给各个面贴纹理,一讲讲了加光照和按键控制. 现在讲的都是给规则的面贴纹理,像正方形,刚好纹理图也是正方形,那像人物模型的衣服贴起来用代码控制得 ...