给定一个二维网格和一个单词,找出该单词是否存在于网格中。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例:

board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 true. 给定 word = "ABCB", 返回 false.

class Solution {
public:
vector<vector<int> > visit;
int len;
int r;
int c;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool exist(vector<vector<char> >& board, string word)
{
len = word.size();
if(len == 0)
return true;
r = board.size();
if(r == 0)
return false;
c = board[0].size();
if(r * c < len)
return false;
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
visit.clear();
visit = vector<vector<int> >(r, vector<int>(c, 0));
if(board[i][j] == word[0])
{
visit[i][j] = 1;
if(DFS(board, 1, word, i, j))
return true;
}
}
}
return false;
} bool DFS(vector<vector<char> >& board, int pos, string cmp, int x, int y)
{
if(pos >= cmp.size())
return true;
for(int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx < 0 || xx >= r || yy < 0 || yy >= c)
continue;
if(visit[xx][yy] == 1)
continue;
if(board[xx][yy] != cmp[pos])
continue;
visit[xx][yy] = 1;
if(DFS(board, pos + 1, cmp, xx, yy))
return true;
visit[xx][yy] = 0;
}
return false; }
};

Leetcode79. Word Search单词搜索的更多相关文章

  1. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  2. LeetCode 79. Word Search单词搜索 (C++)

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  3. 079 Word Search 单词搜索

    给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...

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

  5. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

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

  7. [LeetCode OJ] Word Search 深度优先搜索DFS

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. Leetcode79 Word Search

    题目描述 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed f ...

  9. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. jquery 判断当前设备是PC端还是移动端

    $(function(){ var system = { win: false, mac: false, xll: false, ipad:false }; //检测平台 var p = naviga ...

  2. Java 使用JDBC连接MySQL

    //  这学期本来不打算深入学习Java的,课上的小项目也就随便写了一个简单计算器和扫雷游戏就糊弄过去.可是我们的Eliza老师偏偏什么都讲了,考虑到期末也会涉及到JDBC的内容,前些天试着学习一番. ...

  3. Django 连接MySQL的驱动设置

    对于在Django 中连接MySQL 的驱动,有以下三种: 1) mysqlclient 2) mysql-connector-python 3) pymysql (建议:这个包已经有一年未升级了,本 ...

  4. CheckBox自定义样式

    效果: xmal代码: <Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}"> & ...

  5. Lab 2 内存管理

    常见的操作系统 只使用了 0 和3 段选择子 DPL 是段描述符的内容 段的信息 中断和陷入的 大致特权级的展现 RPL 当前要访问数据段对饮给的特权级 CPL 当前代码段的特权级 DPL 全局的 通 ...

  6. CAS添加验证码功能

    1.  cas.war 下面的web-inf/web.xml  lib添加  kaptcha.jar kaptcha.jar通过maven获取 <dependency> <group ...

  7. DROOLS通过URL访问changset

    package droolsRule; import java.net.Authenticator; import java.net.PasswordAuthentication; import ka ...

  8. PKUOJ 区间内的真素数

    http://bailian.openjudge.cn/tm2018/A/ #include <iostream> #include <math.h> #include < ...

  9. PKU 百炼OJ 简单密码

    http://bailian.openjudge.cn/practice/2767/ #include<iostream> #include <cmath> #include ...

  10. Python学习笔记之常用函数及说明

    Python学习笔记之常用函数及说明 俗话说"好记性不如烂笔头",老祖宗们几千年总结出来的东西还是有些道理的,所以,常用的东西也要记下来,不记不知道,一记吓一跳,乖乖,函数咋这么多 ...