Word Search

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example, Given board =

[
["ABCE"],
["SFCS"],
["ADEE"]
]

word = "ABCCED", -> returns true, word = "SEE", -> returns true, word = "ABCB", -> returns false.

思路:深度优先搜索。注意每条路径搜索完之后,所有的 visited 重置为 false (未访问).

typedef pair<int, int> point;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
bool dfs(vector<vector<char> > &board, string& word, int id, point p, vector<vector<bool> > &visited) {
if(id == word.size()) return true;
visited[p.first][p.second] = true;
for(int i = 0; i < 4; ++i) {
int x = p.first + dx[i], y = p.second +dy[i];
if(x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || visited[x][y]) continue;
if(board[x][y] == word[id] && dfs(board, word, id+1, point(x, y), visited))
return true;
visited[x][y] = false;
}
return false;
}
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
if(word == "") return true;
if(board.size() == 0 || board[0].size() == 0) return false;
int row = board.size(), col = board[0].size();
vector<vector<bool> > visited(row, vector<bool>(col, 0));
for(int r = 0; r < row; ++r) {
for(int c = 0; c < col; ++c) {
if(board[r][c] == word[0] && dfs(board, word, 1, point(r,c), visited))
return true;
visited[r][c] = false;
}
}
return false;
}
};

51. Word Search的更多相关文章

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

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

  3. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  4. Word Search I & II

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

  5. 【leetcode】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. 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 ...

  7. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

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

  8. 212. Word Search II

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

  9. 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. BestCoder Round #43

    T1:pog loves szh I(hdu 5264) 题目大意: 给出把AB两个字符串交叉拼起来的结果,求出原串. 题解: 不解释..直接每次+2输出. T2:pog loves szh II(h ...

  2. android studio 开启genymotion 出现"failed to create framebuffer image"

    出现错误 Unable to start the virtul device To start virtual devices, make sure that your video card supp ...

  3. Java与数据库之间时间的处理

    Java与数据库之间时间的处理 在数据库中建表: DROP TABLE IF EXISTS `times`; CREATE TABLE `times` ( `id` int(11) NOT NULL ...

  4. Poisson Distribution——泊松分布

    老师留个小作业,用EXCEL做不同lambda(np)的泊松分布图,这里分别用EXCEL,Python,MATLAB和R简单画一下. 1. EXCEL 运用EXCEL统计学公式,POISSON,算出各 ...

  5. asp.net 动态添加自定义控件

    前两天一直纠结asp.net动态添加控件后,后台获取不到控件的问题,查看了网上很多的回答,可能自己的理解有误或者自己所掌握的知识有限,都没有解决我遇到的问题,经过两天的研究,终于把问题解决了. 我这里 ...

  6. day15_集合第一天

    1.集合体系 红色为今天所学 Collection                          (接口)|--List                       (接口) 元素有序,可以重复 ...

  7. DIV+CSS制作二级横向弹出菜单,略简单

    没有使用JavaScript控制二级菜单的显示,结果如上图所示. 代码如下: <!DOCTYPE html> <html> <head> <meta char ...

  8. springmvc学习笔记--支持文件上传和阿里云OSS API简介

    前言: Web开发中图片上传的功能很常见, 本篇博客来讲述下springmvc如何实现图片上传的功能. 主要讲述依赖包引入, 配置项, 本地存储和云存储方案(阿里云的OSS服务). 铺垫: 文件上传是 ...

  9. Node.js高级编程读书笔记Outline

    Motivation 世俗一把,看看前端的JavaScript究竟能做什么. 顺便检验一下自己的学习能力. Audience 想看偏后台的Java程序员关于前端JavaScript的认识的职业前端工程 ...

  10. bootstrap-5

    代码(一) 在bootstrap中主要提供了三种代码风格:详见688行-730行 1.使用<code></code>来显示单行内联代码 2.使用<pre></ ...