Problem Description: http://oj.leetcode.com/problems/word-search/

Basic idea: recursively go forward, use char '#' to mark the char visited, so no extra memory is need, don't forget to recover the previous char if the program cannot go forward.

 class Solution {
public:
bool existSub(int i, int j, vector<vector<char> > &board, string word){
if(word.size() == )
return true;
if(i < || j < || i >= board.size() || j >= board[].size())
return false;
if(board[i][j] != word[])
return false; string sub_word = word.substr();
char ch = board[i][j];
board[i][j] = '#';
bool ret = existSub(i - , j, board, sub_word) ||
existSub(i + , j, board, sub_word) ||
existSub(i, j - , board, sub_word) ||
existSub(i, j + , board, sub_word);
if (ret)
return true; board[i][j] = ch;
return false;
} bool exist(vector<vector<char> > &board, string word) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(word.size() == )
return true; for(int i = ; i < board.size(); i++)
for(int j = ; j < board[i].size(); j++)
if(board[i][j] == word[])
if(existSub(i, j, board, word))
return true; return false;
}
};

Word Search [LeetCode]的更多相关文章

  1. Word Search leetcode java

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

  2. [LeetCode] Word Search II 词语搜索之二

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

  3. [LeetCode] Word Search 词语搜索

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

  4. 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 ...

  5. 【LeetCode】79. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  6. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  7. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  8. Leetcode之回溯法专题-79. 单词搜索(Word Search)

    Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...

  9. [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 ...

随机推荐

  1. 命令行运行R语言脚本(代码)

    1 Windows: 键入 cd C:\Program Files\R\R-3.2.0\bin   工作目录切换到R的核心程序目录 键入 R BATCH F:\Test.R 或 Rscript F:\ ...

  2. [CVE:2013-4810]Apache Tomcat/JBoss远程命令执行

    <?php $host=gethostbyname($argv[1]); $port=$argv[2]; $cmd=$argv[3]; //small jsp shell //change th ...

  3. HNOI2006-鬼谷子的钱袋

    鬼谷子的钱袋 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会,其中有一件宝物引起 ...

  4. WinForm 弹框确认后执行

    if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIc ...

  5. 播放列表文件用于HTTP实时流的使用

    广告播放清单(Discontinuities): 通常你会想要提供一系列的电影,在每个电影前面显示一些品牌(广告),让用户知道这些电影来自你的特定网站.一种方法是简单地将广告与每部电影合并.但是如果你 ...

  6. Python学习(15)文件/IO

    目录 Python 文件I/O 打印到屏幕 读取键盘输入 打开和关闭文件 File对象属性 文件定位 重命名和删除文件 Python的目录 Python 文件I/O 本章只讲述所有基本的的I/O函数, ...

  7. mysql /*! 50100 ... */ 条件编译

    1./*...*/ 是注释,mysql不会执行.2.mysql对标准sql进行了扩展,包含了一些自己的特性.3./*!...*/ 是一种特殊的注释,其他的数据库产品当然不会执行.mysql特殊处理,会 ...

  8. js图片跑马灯效果

    <style. type="text/css">body{margin:0px auto; padding:0px;}ul,li{margin:0px; padding ...

  9. maven的仓库、生命周期与插件

    一.仓库 统一存储所有Maven项目共享的构建的位置就是仓库. 仓库分为本地仓库和远程仓库.远程仓库又分为中央仓库(中央仓库是Maven核心自带的远程仓库),伺服(另一种特殊的远程仓库,为节省宽带和时 ...

  10. 正则的小效果:-------> 过滤敏感词

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...