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 ...
随机推荐
- [SP839]Optimal Marks
luogu 题意 给你一个无向图\(G(V,E)\). 每个顶点都有一个int范围内的整数的标记. 不同的顶点可能有相同的标记. 对于边\((u,v)\),我们定义\(Cost(u,v)=\rm ma ...
- webpack 插件
插件可以完成更多 loader 不能完成的功能. 插件的使用一般是在 webpack 的配置信息 plugins 选项中指定. Webpack 本身内置了一些常用的插件,还可以通过 npm 安装第三方 ...
- word 2007,以不同颜色突出显示文本的快捷键,highlight命令
命令:highlight 默认快捷键:Ctrl+Alt+H 查询或自定义快捷键的方法: 打开一个文档→单击左上角的office图标→word选项 左边的列表中选择自定义→在右边的窗口中,底部有个“ ...
- win8.1系统相关
win8.1系统相关 信息时代,系统更新速度非常快,十一月初,同事在网上花5元买了一个win8.1系统激活码,之后两周,我电脑由于系统故障,准备重装系统,借助他的系统,但无法激活,借用他购买的账号也不 ...
- 使用Spring AMQP开发消费者应用
前一篇中我们介绍了使用RabbitMQ Java Client访问RabbitMQ的方法.但是使用这种方式访问RabbitMQ,开发者在程序中需要自己管理Connection,Channel对象,Co ...
- Xcode的Refactor使用
最近在看<重构>的书,想到Xcode有一个Refactor的功能,不知道您用的多不多,用这个功能在我们开发过程中,可以提高开发效率. Refactor 右键显示 Refactor 一.Re ...
- Oracle 在约束中使用正则表达式
ALTER TABLE mytest ADD CONSTRAINT CK_REG CHECK(REGEXP_LIKE(TEST, '^[0-9]{1,3}(\.[0-9]){0,1}$'));
- python读取配置文件 ConfigParser
Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...
- leetcode645
vector<int> findErrorNums(vector<int>& nums) { ; int S[N]; int n = nums.size(); ; i ...
- Shell编程进阶 2.2 shell数组
给一个字符指定一个数组 怎么显示数组 a= echo $a a=( ) echo $a echo ${a[@]} echo ${a[*]} 指定显示数组中第几个数字 echo ${a[]} echo ...