原题地址

依次枚举起始点,DFS+回溯

代码:

 bool dfs(vector<vector<char> > &board, int r, int c, string word) {
int m = board.size();
int n = board[].size();
int dir[][] = {{-, }, {, }, {, -}, {, }}; if (word.empty())
return true; if (r >= && r < m && c >= && c < n && board[r][c] == word[]) {
for (int i = ; i < ; i++) {
char tmp = board[r][c];
board[r][c] = ;
if (dfs(board, r + dir[i][], c + dir[i][], word.substr()))
return true;
board[r][c] = tmp;
}
} return false;
} bool exist(vector<vector<char> > &board, string word) {
if (board.empty() || board[].empty()) return false; int m = board.size();
int n = board[].size(); for (int i = ; i < m; i++)
for (int j = ; j < n; j++)
if (dfs(board, i, j, word))
return true; return false;
}

Leetcode#79 Word Search的更多相关文章

  1. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  2. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  3. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  4. LeetCode 79. Word Search(单词搜索)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  5. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  6. LeetCode 79. Word Search单词搜索 (C++)

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  7. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

随机推荐

  1. Cocos2d-JS自定义粒子系统

    除了使用Cocos2d-JS的11种内置粒子系统外,我们还可以通过创建ParticleSystem对象,并设置属性实现自定义粒子系统,通过这种方式完全可以实现我们说需要的各种效果的粒子系统.使用Par ...

  2. jQuery异步分页插件

    学校软件工程让写课程设计(其实就是自选语言做个项目),感觉都是重复的东西就没有很认真的去写内容,更加注意写一些之前没有用过的东西. 因为一直都使用TP框架来写PHP,TP又自带分页类,想到这里就想试试 ...

  3. 20141009---Visual Studio 2012 预定义数据类型

    预定义数据类型 一.值类型 整型:(整数) 有符号整型和无符号整形,区别是有符号的有负数无符号的都是正数, 2x+1 常用int 有符号:              带有正负数,范围为按所写依次增大 ...

  4. ARM你必须知道的事儿——为啥“PC = PC + 8”?

    为啥是“PC = PC + 8”: “PC = PC + 8”其实这样写容易让人蒙了.“PC = PC + 8”真正含义应该是: 执行处代码地址 = PC - 8: 也就是说,”PC指向的地址“领先“ ...

  5. 8个超炫酷的纯CSS3动画及源码分享

    在现代网页中,我们已经越来越习惯使用大量的CSS3元素,而现在的浏览器也基本都支持CSS3,所以很多时候我们不妨思考一下是否可以用纯CSS3制作一些有趣或者实用的网页.本文要分享8个超炫酷的纯CSS3 ...

  6. NOIP 2015复赛提高组Day2 T1==Codevs 4768 跳石头

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold  题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中 ...

  7. Linux 软中断

    本文转载自: http://blog.chinaunix.net/uid-9620812-id-3833377.html,如有需要,请移步访问. Technorati 标签: Linux 软中断 -- ...

  8. USB时钟为什么是48MHz

        在学习2440的USB配置时钟中,发现它的时钟需要设置成48MHz固定的,这个我就来兴趣了,为什么这里面USB的时钟一定要是48M呢?在网上找了众多文章,都是讲解如何配置2440的MPLL和U ...

  9. Debian--changelog

    以前就研究过debian安装包的问题,当时也没有做相关方面的记录,当时也没有完全研究明白,现在重新研究下,现在写下我的一些笔记,等我研究明白了,我会整理出来,出个系列博客,有兴趣的同学,也可以去看E文 ...

  10. javascript框架库API入口

    underscorejs : http://learning.github.io/underscore/