题目描述
https://leetcode-cn.com/problems/word-search
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
给定 word = "ABCCED", 返回 true
给定 word = "SEE", 返回 true
给定 word = "ABCB", 返回 false
提示:
board 和 word 中只包含大写和小写英文字母。
1 <= board.length <= 200
1 <= board[i].length <= 200
1 <= word.length <= 10^3
思路:在一个解空间中寻找有效解,首先想到DFS和回溯法,本题首先找到首字母的位置,再依次判断其水平和垂直方向(可能部分没有需判断)相邻字符是否符合条件,无论是否满足,递归调用完都需要回溯到最初的位置,因为本题不可重复调用,故用一个bollean型数组存放是否被用过防止重复
还有一点需要注意的是,本题只要找到一个有效解就应直接返回true,因此DFS要设置为boolean型,如果用flag的方式记录遍历过程中是否出现了有效解则会在数据较大时超时。
class Solution {
boolean[][] num;//存放当前位置是否用过
public boolean exist(char[][] board, String word) {
if(board==null||board.length==0||board[0].length==0){
return false;
}
num=new boolean[board.length][board[0].length];
if(word==null||word.length()==0)
return true;
if(dfs(board,word,0,0,0)){
return true;
}
else{
return false;
}
}
//start为当前的字符串第几个字符,x为二维矩阵中当前位置的行坐标,y为列坐标
public boolean dfs(char[][] board,String word,int start,int x,int y){
if(start==word.length()){//当所有字符串字符都有有效解则返回
return true;
}
char temp=word.charAt(start);
if(start==0){//start=0时要找到第一个字符的解
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
if(board[i][j]==temp){
num[i][j]=true;
if(dfs(board,word,start+1,i,j))
return true;
num[i][j]=false;
}
}
}
return false;
}
else{
if(y-1>=0){//左边
if(board[x][y-1]==temp&&!num[x][y-1]){
num[x][y-1]=true;
if(dfs(board,word,start+1,x,y-1))
return true;
num[x][y-1]=false;
}
}
if(x-1>=0){//上边
if(board[x-1][y]==temp&&!num[x-1][y]){
num[x-1][y]=true;
if(dfs(board,word,start+1,x-1,y))
return true;
num[x-1][y]=false;
}
}
if(y+1<board[0].length){//右边
if(board[x][y+1]==temp&&!num[x][y+1]){
num[x][y+1]=true;
if(dfs(board,word,start+1,x,y+1))
return true;
num[x][y+1]=false;
}
}
if(x+1<board.length){//下边
if(board[x+1][y]==temp&&!num[x+1][y]){
num[x+1][y]=true;
if(dfs(board,word,start+1,x+1,y))
return true;
num[x+1][y]=false;
}
}
return false;
}
}
}
题目描述
https://leetcode-cn.com/problems/word-search-ii
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
示例:
输入:
words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
输出: ["eat","oath"]
说明:
你可以假设所有输入都由小写字母 a-z 组成。
提示:
你需要优化回溯算法以通过更大数据量的测试。你能否早点停止回溯?
如果当前单词不存在于所有单词的前缀中,则可以立即停止回溯。什么样的数据结构可以有效地执行这样的操作?散列表是否可行?为什么? 前缀树如何?如果你想学习如何实现一个基本的前缀树,请先查看这个问题: 实现Trie(前缀树)。
思路:和前一题步骤类似,先对 words数组排序,再依次判断是否有解,有则将其加入List,DFS同样还是设置为boolean型,一旦出现true即返回输出。
import java.util.List;
class Solution {
List<String> res=new ArrayList<String>();
boolean[][] num;
public List<String> findWords(char[][] board, String[] words) {
if(board==null||board.length==0||board[0].length==0||words==null||words.length==0)
return res;
Arrays.sort(words);
for(int i=0;i<words.length;i++){
num=new boolean[board.length][board[0].length];
String s=words[i];
if(find(board,s,0,0,0)){
res.add(s);
}
}
return res;
}
public boolean find(char[][] board,String s,int start,int x,int y){
if(start==s.length()){
return true;
}
char temp=s.charAt(start);
if(start==0){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
if(board[i][j]==temp){
num[i][j]=true;
if(find(board,s,start+1,i,j)){
return true;
}
num[i][j]=false;
}
}
}
return false;
}
if(y-1>=0){
if(board[x][y-1]==temp&&!num[x][y-1]){
num[x][y-1]=true;
if(find(board,s,start+1,x,y-1))
return true;
num[x][y-1]=false;
}
}
if(x-1>=0){
if(board[x-1][y]==temp&&!num[x-1][y]){
num[x-1][y]=true;
if(find(board,s,start+1,x-1,y))
return true;
num[x-1][y]=false;
}
}
if(y+1<board[0].length){
if(board[x][y+1]==temp&&!num[x][y+1]){
num[x][y+1]=true;
if(find(board,s,start+1,x,y+1))
return true;
num[x][y+1]=false;
}
}
if(x+1<board.length){
if(board[x+1][y]==temp&&!num[x+1][y]){
num[x+1][y]=true;
if(find(board,s,start+1,x+1,y))
return true;
num[x+1][y]=false;
}
}
return false;
}
}