LeetCode OJ--Word Search **
https://oj.leetcode.com/problems/word-search/
类似于在棋盘上走步,走过的地方不能再走,每次都可以走当前位置的上、下、左、右,问能不能走出要求的形状来。
深搜:
依次搜它的上
下
左
右
在深搜中,容易超时,所以如果有复杂类型的数据传值,一般都用引用。当然,为了恢复每次引用的现场,需要把本次深搜中改变的值,再改回来。
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
if(board.size() == || word.size() == )
return false;
int row = board.size();
int col = board[].size();
vector<vector<bool> > flag;
//initialize
flag.resize(row);
for(int i = ; i < row; i++)
{
flag[i].resize(col);
for(int j = ; j < col; j++)
flag[i][j] = false;
}
bool ans = false;
for(int i = ; i < row; i++)
{
for(int j = ; j < col; j++)
{
if(board[i][j] == word[])
{
ans = find(board,word,i,j,flag,);
if(ans)
return true;
}
}
}
return false;
}
bool find(vector<vector<char> > &board, string &word, int i, int j,vector<vector<bool> > &flag, int match_index)
{
if(match_index == word.size())
return true;
//true means used
flag[i][j] = true;
bool ans;
//up
if(i!= && board[i-][j] == word[match_index] && flag[i-][j] == false)
{
ans = find(board,word,i-,j,flag,match_index + );
if(ans)
return true;
}
//right
if(j!= board[].size() - && board[i][j+] == word[match_index] && flag[i][j+] == false)
{
ans = find(board,word,i,j+,flag,match_index + );
if(ans)
return true;
}
//down
if(i!= board.size() - && board[i+][j] == word[match_index] && flag[i+][j] == false)
{
ans = find(board,word,i+,j,flag,match_index + );
if(ans)
return true;
}
//left
if(j!= && board[i][j-] == word[match_index] && flag[i][j-] == false)
{
ans = find(board,word,i,j-,flag,match_index + );
if(ans)
return true;
}
flag[i][j] = false;
return false;
}
};
LeetCode OJ--Word Search **的更多相关文章
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- 【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 ...
- LeetCode OJ——Word Ladder
http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...
- [LeetCode#212]Word Search II
Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
随机推荐
- SpringBoot-Security-用户权限分配-项目搭建
SpringBoot原则是约定优于配置,简化spring应用开发,去繁从简,产品级别的应用. SpringBoot有哪些优点1.快速创建独立运行的spring项目与主流框架集成 2.使用嵌入式的ser ...
- Farm Tour POJ - 2135 (最小费用流)
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= ...
- 栈及其DFS:B - Parentheses Balance
解题心得及总结: 总结: 1.递推:又1推出n,数列中的基本到通项,最终目标得出通项公式. 递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1: 2.判断是否可以由递推或递推得出, ...
- ListNode Java创建链表
用了一种自创的比较简洁的方式来创建链表 class ListNode { //为了方便,这两个变量都使用pub1ic, //存放数据的变量,直接为int型 public int data; //存放结 ...
- 网络流24题:P2762 太空飞行计划问题
P2762 太空飞行计划问题 题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,E ...
- STL学习笔记6 -- 栈stack 、队列queue 和优先级priority_queue 三者比较
栈stack .队列queue 和优先级priority_queue 三者比较 默认下stack 和queue 基于deque 容器实现,priority_queue 则基于vector 容器实现 ...
- Leetcode 493.翻转对
翻转对 给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对. 你需要返回给定数组中的重要翻转对的数量. 示例 ...
- tail /grep/more
1.tail -f 文件名 不断的刷新日志信息,实时的得到新追加到文件中的信息,常用来跟踪日志文件,如tail -f err.log Ctrl+C退出 2.grep '内容' 文件名 3.more 分 ...
- Callable、Future、FutureTask浅析
1.Callable<V>接口 Runnable接口 public interface Runnable { public abstract void run(); } Callable ...
- 环境说明与HelloWorld
本人采用的是ExtJs4.2版本,采用WebStorm作为IDE开发工具 目录说明 builds:压缩后的ExtJs代码 docs:文档 examples:官方示例 locale:多国语言的资源文件 ...