终于搞定了这个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 待解决?的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  4. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  5. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...

  6. [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 ...

  7. [leetcode]Word Search @ Python

    原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 将非Maven管理的jar打包到Maven本地资源库

    这里有2个案例,需要手动发出Maven命令包括一个 jar 到 Maven 的本地资源库. 要使用的 jar 不存在于 Maven 的中心储存库中. 您创建了一个自定义的 jar ,而另一个 Mave ...

  2. electron 安装失败解决办法

    1.安装node https://nodejs.org/en/download/2.安装镜像工具npm install -g cnpm --registry=https://registry.npm. ...

  3. [转载]Linux驱动-SPI驱动-概述

    转载地址http://blog.csdn.net/droidphone SPI是"Serial Peripheral Interface" 的缩写,是一种四线制的同步串行通信接口, ...

  4. Mybatis多参数查询映射

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  5. c# 几种singleton 实现

    http://csharpindepth.com/Articles/General/Singleton.aspx#introduction 4th在线看 https://www.manning.com ...

  6. zedgraph控件的一些比较有用的属性

    (1)zedgraph控件属性具体解释: AxisChange()() ->> This performs an axis change command on the graphPane. ...

  7. 图解缓存淘汰算法一之LRU

    1.概念分析 LRU(Least Recently Used),即最近最少使用.怎么理解这个概念呢?我一开始见到这个概念的时候,以为"最近","最少"都是修饰使 ...

  8. javascript——对象的基础知识

    一.javascript作为脚本语言可以完成以下任务: 操纵浏览器对象,如窗口的打开与关闭: 操纵Dom树: 通过XMLHttpRequest对象与服务器端进行异步通信: XML编程,借助于Activ ...

  9. object类型对象 ref参数如何理解?

    class Program { static void Main(string[] args) { Student stu = new Student { Name = "老王" ...

  10. java.lang.Class.getDeclaredMethod()方法详解

    Java.lang.Class.getDeclaredMethod()方法用法 注:方法返回一个Method对象,它反映此Class对象所表示的类或接口的指定已声明方法. 描述 java.lang.C ...