leetcode Word Search 待解决?
终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞定。
人品啊TLE:
class Solution {
public:
bool legal(int i, int j, vector<vector<char>> board)
{
if (i >= && i < board.size() && j >= && j < board[i].size())
return true;
return false;
} bool dfs(vector<vector<char>>& board, string word, int t, int i, int j, vector<vector<bool>> visited)
{
if (t == word.length())
return true;
if (board[i][j] == word[t])
{
visited[i][j] = true;
if (legal(i - , j, board) && !visited[i - ][j] && dfs(board, word, t + , i - , j, visited))
return true;
if (legal(i, j + , board) && !visited[i][j + ] && dfs(board, word, t + , i, j + , visited))
return true;
if (legal(i + , j, board) && !visited[i + ][j] && dfs(board, word, t + , i + , j, visited))
return true;
if (legal(i, j - , board) && !visited[i][j - ] && dfs(board, word, t + , i, j - , visited))
return true;
}
visited[i][j] = false;
return false;
} bool exist(vector<vector<char>>& board, string word)
{
int m = board.size();
int n = board[].size();
vector<vector<bool>> visited(m, vector<bool>(n, false));
for (int i = ; i < board.size(); i++)
{
for (int j = ; j < board[].size(); j++)
{
if (dfs(board, word, , i, j, visited))
return true;
}
}
return false;
}
};
为什么?
下面的依然是超时的, 无论怎么尝试都是超时的,觉得和提交的AC代码并没有什么区别啊?
#include<iostream>
#include<vector> using namespace std; bool isLegal(int i, int j, vector<vector<char>> board)
{
int H = board.size();
int L = board[].size();
if (i >= && i < H&&j >= && j < L)
return true;
return false;
} bool searchWord(vector<vector<char>> board, vector<vector<bool>> visited, string word, int i, int j, int index)
{
if (index >= word.length())
{
return true;
}
/*if (word[index] == board[i][j])
{*/
visited[i][j] = true;
int dx[] = { , , -, };
int dy[] = { , , , - }; for (int x = ; x < ; ++x)
{
int ii = i + dx[x];
int jj = j + dy[x];
if (isLegal(ii, jj, board) && !visited[ii][jj] && board[ii][jj] == word[index] && searchWord(board, visited, word, ii, jj, index + ))
return true;
} /*if (isLegal(i - 1, j, board) && !visited[i - 1][j] && board[i - 1][j]==word[index] && searchWord(board, visited, word, i - 1, j, index + 1))
return true;
if (isLegal(i, j - 1, board) && !visited[i][j - 1] && board[i][j - 1] == word[index] && searchWord(board, visited, word, i, j - 1, index + 1))
return true;
if (isLegal(i + 1, j, board) && !visited[i + 1][j] && board[i + 1][j] == word[index] && searchWord(board, visited, word, i + 1, j, index + 1))
return true;
if (isLegal(i, j + 1, board) && !visited[i][j + 1] && board[i][j + 1] == word[index] && searchWord(board, visited, word, i, j + 1, index + 1))
return true;*/
/*}*/
visited[i][j] = false;
return false;
} bool exist(vector<vector<char>>& board, string word) {
if (word.empty()) return false;
int H = board.size();
int L = board[].size();
int i, j;
vector<vector<bool>> visited(H, vector<bool>(L, false));
for (i = ; i < H; i++)
{
for (j = ; j < L; j++)
{
if (word[]==board[i][j]&&searchWord(board, visited, word, i, j, ))
{
return true;
}
}
}
return false;
} int main()
{
char boardArray[][] =
{
{ 'A', 'B', 'C', 'E' },
{ 'S', 'F', 'C', 'S' },
{ 'A', 'D', 'E', 'E' }
};
vector<vector<char>> board1();
for (int i = ; i < ; i++)
{
vector<char> temp();
temp[] = 'A';
board1[i] = temp;
for (int j = ; j < ; j++)
{
board1[i][j] = boardArray[i][j];
}
} if (exist(board1, "ABCCED"))
cout << <<endl;
else
cout << << endl;
}
leetcode 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 ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- LeetCode() Word Search II
超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...
- [LeetCode] Word Search [37]
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- [leetcode]Word Search @ Python
原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...
- [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 回溯
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
随机推荐
- 命令行启动MySQL
1 首先打开CMD,即命令行 输入mysqld ,如出现 则说明MySQL安装路径下的bin不在系统的环境变量中,你需要进入它的安装路径的bin目录下启动. ps:如果你不知道路径可以去开始菜单,找到 ...
- BZOJ4605:崂山白花蛇草水
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
- AllowsTransparency和WebBrowser兼容性问题解决方案
AllowsTransparency和System.Windows.Controls.WebBrowser兼容性问题,能看这篇文章,所以原因也不用多说:最根本的就是因为MS对win32底层的WebBr ...
- win 7命令行大全
一.win+(X) 其中win不会不知道吧,X为代码! Win+L 锁定当前用户. Win+E 资源管理器. Win+R 运行. Win+G (Gadgets)顺序切换边栏小工具. Win+U ...
- C语言生成程序问题
问题: 我用VS2013写好C语言程序调试运行后就在debug文件夹下生成了EXE文件,可以在本机运行.但是这个EXE文件在别的没装过VS2013的电脑上就不能直接运行,说丢失MSVCR120D.dl ...
- jhipster初接触
在Windows7部署之前把几个依赖下了 jdk:1.80 Maven :3.3.9 git:2.14.1 npm:唯一要注意的就是配置一个阿里的镜像,不然慢的你崩溃 Yeoman: npm inst ...
- apache http 跳到https
RewriteEngine OnRewriteCond %{HTTPS} !=onRewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
- Tomcat 不能正常启动
启动过程提示: Stopping ProtocolHandler ["http-bio-8080"] the JRE_HOME environment variable is no ...
- python+requests+excel 接口测试
1.EXCEL文件接口保存方式,如图. 2.然后就是读取EXCEL文件中的数据方法,如下: import xlrd class readExcel(object): def __init__(self ...