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 ...
随机推荐
- 开发mis系统用到的技术
1. b/s架构:就broser/server,浏览器/服务器的说法.服务器端要运行tomcat,提供链接数据库服务供java代码读写数据,这个可以在eclipse中配置运行.浏览器则解释jsp或ht ...
- How To Uninstall Software Using The Ubuntu Command Line
How To Uninstall Software Using The Ubuntu Command Line Uninstall Ubuntu Software Using The Terminal ...
- andriod&linux&c函数原型
1.dlopen 功能:打开一个动态链接库,并返回动态链接库的句柄 包含头文件: #include <dlfcn.h> 函数定义: void * dlopen( const char * ...
- BZOJ4358:permu
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- Linux基础命令-文本文件查看工具
文本文件查看工具 cat concatenate 文本文件查看工具 cat /etc/fstab cat [OPTION]... [FILE]... -n:给显示的文本行编行 -E:显示行结束符 ta ...
- clone对象的克隆
用一句简单的话来说就是浅拷贝,只是对指针的拷贝,拷贝后两个指针指向同一个内存空间,深拷贝不但对指针进行拷贝,而且对指针指向的内容进行拷贝,经深拷贝后的指针是指向两个不同地址的指针. 等多 http:/ ...
- codeforce 103B Cthulhu
B. Cthulhu time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- RVO和NRVO
返回值优化(Return Value Optimization,简称RVO),是这么一种优化机制:当函数需要返回一个对象的时候,如果自己创建一个临时对象用户返回,那么这个临时对象会消耗一个构造函数(C ...
- 【SymmetricDS】SymmetricDS是如何工作的
2018-04-20 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/8890785.html 本文翻译自SymmetricDS官方文档 ...
- delphi 从 TWebbrowse组件中获取图片
在 delphi 中使用 TWebbrowse 组件,虽然效率不如用(idhttp之类)模拟操作效率高.但其难度低,上手快,简单粗暴有效. 从网上搜到的处理此问题的文章大多是 ctrl + c 复制到 ...