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 ...
随机推荐
- C#异步编程(五)异步的同步构造
异步的同步构造 任何使用了内核模式的线程同步构造,我都不是特别喜欢.因为所有这些基元都会阻塞一个线程的运行.创建线程的代价很大.创建了不用,这于情于理说不通. 创建了reader-writer锁的情况 ...
- C#面向对象(四):其他面向对象知识
前文链接: C#面向对象(一):明确几个简单的概念作为开胃菜 C#面向对象(二):封装和继承 C#面向对象(三):多态 今天是这个系列的收尾文章了,来谈谈其他面向对象知识. 1.嵌套类 1.1概念 在 ...
- python之 python 起源、语言特点
一. 1.1 什么是 PythonPython 是一门优雅而健壮的编程语言,它继承了传统编译语言的强大性和通用性,同时也借鉴了简单脚本和解释语言的易用性.它可以帮你完成工作,而且一段时间以后,你还能 ...
- 第9章 DOM对象,控制HTML元素
学习地址:http://www.imooc.com/learn/10
- Asp.net工作流workflow实战之工作流持久化(五)
直接看msdn https://msdn.microsoft.com/zh-cn/library/ee395773(v=vs.100).aspx
- SQL SERVER存储过程的几种示例
1.常用系统存储过程及使用语法:exec sp_databases; --查看数据库exec sp_tables; --查看表exec sp_columns student;--查看列exec sp_ ...
- laravel count的使用
rt 学习源头: https://blog.csdn.net/chajinglong/article/details/51954010 四.聚合 查询构建器还提供了各种聚合方法,如统计,马克斯,min ...
- extjs控制器调用其他视图的函数实现控件赋值。
- maven jetty 配置
对于jdk8增加如下配置: <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jett ...
- SpringMVC—对Ajax的处理(含 JSON 类型)(2)
这里编写了一个通用的类型转换器: 用来转换形如: firstName=jack&lastName=lily&gender=1&foods=Steak&foods=Piz ...