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的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. jquery简单的图片切换效果,支持pc端、移动端的banner图片切换开发

    详细内容请点击 无意中看见了两年前写的一个图片切换,那会儿刚刚学习网页制作,可以说是我的第一个处女座的jquery图片切换效果.无聊之余对它的宽度稍稍做了一下修改,变成了支持pc端.手机端全屏的ban ...

  2. Activity生命周期-Android

    Activity常见的三种生命周期: 1.完整生命周期 oncreate-->onstart-->onresume-->onpause-->onstop-->ondest ...

  3. JSP之错误信息提示

    MessageResource.properties配置文件: RegisterAction注册: package com.caiduping.action; import javax.servlet ...

  4. 使用script的src实现跨域和类似ajax效果

    在解决js的跨域问题的时候, 有多种方式, 其中有一种是利用script标签的src属性,因为这个属性是不受域名限制的,我们可以直接让src的这个链接指向跨域网站的一个接口, 这个接口返回的是js代码 ...

  5. 【Linux C中文函数手册】文件内容控制函数

    文件内容控制函数 1)clearerr 清除文件流的错误旗标 相关函数 feof表头文件 #include<stdio.h>定义函数 void clearerr(FILE * stream ...

  6. iOS定位 (一) 地图定位

    带地图的定位方法#import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> <MKMapViewDel ...

  7. 10款精美的web前端源码的特效

    1.HTML5侧滑聊天面板 很酷的聊天界面 这是一款基于HTML5和SVG的侧滑聊天面板,初始化的时候聊天面板是锁定的,当你拖动白色区域时,即可解锁展开聊天面板,显示所有好友.点击面板中的好友即可切换 ...

  8. 使用FlashFXP V3.8烈火汉化绿色版软件连接Linux

    使用FlashFXP V3.8烈火汉化绿色版软件连接Linux 单击右上角的小闪电图标: 特别注意:出于安全考虑,FTP默认禁止使用root账号登陆Linux主机,必须使用除root用户以外的其他用户 ...

  9. HashSet 读后感

    HashSet实现Set,是一个不能重复元素的集合,内部使用HashMap实现.因此具有HashMap的特性,如不保证元素插入的顺序,线程不安全,允许null.HashSet的元素就是内部HashMa ...

  10. sizeWithFont方法被弃用了,该怎么办?

    之前使用了NSString类的sizeWithFont:constrainedToSize:lineBreakMode:方法,但是该方法已经被iOS7 Deprecated了,而iOS7新出了一个bo ...