51. Word Search
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的更多相关文章
- [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 ...
- [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 ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
随机推荐
- androidd 程序默认安装位置和数据存储位置(公用和私用)
默认安装位置: android App 安装到外置SD卡中,缓解手机内置内存的压力: <manifest xmlns:android="http://schemas.android.c ...
- [转]Neural Networks, Manifolds, and Topology
colah's blog Blog About Contact Neural Networks, Manifolds, and Topology Posted on April 6, 2014 top ...
- 用meta-data配置参数
在接入第三方渠道SDK的时候,经常会看到其配置文件AndroidManifest.xml有类似如下的定义: <!-- appid --> <meta-data android:nam ...
- 网络编程-socket
本节内容: 一:TCP/IP:Transmission Control Protocol/Internet Protocol 传输控制协议/因特网互联协议.即通讯协议.是主机接入互联网以及互联网中两台 ...
- DAO接口及实现类
DAO接口中定义了所有的用户操作,如添加记录.删除记录及查询记录. package chapter13; import java.util.*; public interface UserDAO { ...
- .vimrc常用
vim 的環境設定參數 :set nu:set nonu 行号 :set tabstop=4 :set softtabstop=4 :set shiftwidth=4 tab :set hlsea ...
- [原创] 聊聊X-Forwared-For和关于他的几种非主流安全问题
关于这个X-FORWARED-FOR 有很多非主流漏洞都和他有关 之前我和我的基友misty以为关于这个标头的漏洞会有很多会被很多开发者忽视 会出现很多关于他的安全漏洞 可是由于我精力不足 就没继续 ...
- OS实验一实验报告
实验一.命令解释程序的编写实验 专业:商业软件工程 姓名:王泽锴 学号:201406114113 一.实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语 ...
- 【Unity3D基础教程】给初学者看的Unity教程(零):如何学习Unity3D
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D有什么优势 Unity3D是一个跨 ...
- Node.js热部署方式
1. supervisor: 2. hotnode: 3. node-dev: 4. nodemon: