2014-05-01 02:30

题目链接

原题:

Given a matrix of letters and a word, check if the word is present in the matrix. E,g., suppose matrix is:
a b c d e f
z n a b c f
f g f a b c
and given word is fnz, it is present. However, gng is not since you would be repeating g twice.
You can move in all the directions around an element.

题目:给定一个字母矩阵,给定一个单词,请判断从矩阵某一点出发,能否走出一条路径组成这个单词。每一步可以走8邻接的方向。

解法:这基本就是Leetcode的原题Word Search,DFS搞定。如果矩阵太大的话,可以用BFS防止递归过深造成的栈溢出,不过效率方面就更慢了。

代码:

 // http://www.careercup.com/question?id=5890898499993600
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
n = (int)board.size();
if (n == ) {
return false;
}
m = (int)board[].size();
word_len = (int)word.length(); if (word_len == ) {
return true;
} int i, j;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
if(dfs(board, word, i, j, )) {
return true;
}
}
}
return false;
}
private:
int n, m;
int word_len; bool dfs(vector<vector<char> > &board, string &word, int x, int y, int idx) {
static const int d[][] = {
{-, -},
{-, },
{-, +},
{ , -},
{ , +},
{+, -},
{+, },
{+, +}
}; if (x < || x > n - || y < || y > m - ) {
return false;
} if (board[x][y] < 'A' || board[x][y] != word[idx]) {
// already searched here
// letter mismatch here
return false;
} bool res;
if (idx == word_len - ) {
// reach the end of word, success
return true;
} for (int i = ; i < ; ++i) {
board[x][y] -= 'a';
res = dfs(board, word, x + d[i][], y + d[i][], idx + );
board[x][y] += 'a';
if (res) {
return true;
}
}
// all letters will be within [a-z], thus I marked a position as 'searched' by setting them to an invalid value.
// we have to restore the value when the DFS is done, so their values must still be distiguishable.
// therefore, I used an offset value of 'a'.
// this tricky way is to save the extra O(n * m) space needed as marker array. return false;
}
};

Careercup - Facebook面试题 - 5890898499993600的更多相关文章

  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. html5 Canvas API

    详细内容请点击 1.HTML Canvas API有两方面优势可以弥补:首先,不需要将所绘制图像中的每个图元当做对象存储,因此执行性能非常好:其次,在其他编程语言现有的优秀二维绘图API的基础上实现C ...

  2. sql server 中 syscolumns 各个字段的意义

    列名 数据类型 描述 name sysname 列名或过程参数的名称. id int 该列所属的表对象 ID,或与该参数关联的存储过程 ID. xtype tinyint systypes 中的物理存 ...

  3. Top 10 Programming Fonts

    Top 10 Programming Fonts Sunday, 17 May 2009 • Permalink Update: This post was written back in 2009, ...

  4. PAT1014——福尔摩斯的约会

    大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”.大侦探很快就明白了,字条 ...

  5. 1 ubuntu下装setuptools

    setuptools可以让程序员更方便的创建和发布 Python 包,特别是那些对其它包具有依赖性的状况,分享以下我在ubuntu下装setuptools的过程 系统:ubuntu 语言:python ...

  6. ping命令的用法大全!

    1)如何查看本机所开端口: 用netstat -an命令查看!再stat下面有一些英文,我来简单说一下这些英文具体都代表什么- LISTEN:侦听来自远方的TCP端口的连接请求 SYN-SENT:再发 ...

  7. 集合类学习之HashMap经典储存 分拣存储与面向对象组合

    HashMap:键值对(key-value) 通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value. 默认是1:1关系(一对一) 存在则覆盖,当key已经存在,则利用新的va ...

  8. WCF之并发,吞吐量和限流

    并发 Single重入模式.对于每一个服务实例,同一时刻只能处理一个请求,其他对该实例的请求被排队. PerCall,每一线程会分配一个新的服务实例上.不会有并发性问题.不影响吞吐量. PerSess ...

  9. [Bootstrap]组件(三)

    输入框组 添加额外元素.input-group-addon 外包元素.input-group>input-group-addon+form-control <div class=" ...

  10. xmlspy注册后打开报错的解决办法

    XMLSpy 2011中文版破解补丁使用方法 1.如果你下载的版本是r2sp1的话(r2不用此步骤),先用补丁主程序(altova.xmlspy.v2011r2sp1b-patch.exe).2.XM ...