题目描述

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

Example:

board =

[

['A','B','C','E'],

['S','F','C','S'],

['A','D','E','E']

]

Given word = "ABCCED", return true.

Given word = "SEE", return true.

Given word = "ABCB", return false.

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/word-search

代码实现

class Solution {
public:
bool existCore(vector<vector<char>>& board, string &word,int & Wordpos,int rows,int cols,int row,int col,vector<vector<int> > & visited){
if(Wordpos>=word.size())
return true;
if(col>=cols||row>=rows||col<0||row<0){
return false;
}
bool flag=false;
if(board[row][col]==word[Wordpos]&& visited[row][col]!=1){
++Wordpos;
visited[row][col]=1;
flag=existCore(board,word,Wordpos,rows,cols,row+1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col+1,visited)||existCore(board,word,Wordpos,rows,cols,row-1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col-1,visited);
if(!flag)
{
--Wordpos;
visited[row][col]=0;
}
}
return flag;
}
bool exist(vector<vector<char>>& board, string word) {
bool result=false;
if(board.size()==0||board[0].size()==0){
return false;
}
int rows=board.size();
int cols=board[0].size();
vector<vector<int> > visited;
for(int i=0;i<rows;++i){
vector<int> temp;
for(int j=0;j<cols;++j){
temp.push_back(0);
}
visited.push_back(temp);
}
int Wordpos=0;
for(int i=0;i<rows;++i){
for(int j=0;j<cols;++j)
{
if(existCore(board,word,Wordpos,rows,cols,i,j,visited))
return true;
}
}
return false;
}
};

总结

思路就是回溯法。中间纠结了一下,visited的数组是用二维数组还是用vector。后来发现如果用二维数组,传指针的地方较为麻烦,而且不能像vector一样直接visited[row][col]。

Leetcode79 Word Search的更多相关文章

  1. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

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

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

  4. Leetcode: word search

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

  5. Word Search I & II

    Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  6. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  7. Java for LeetCode 212 Word Search II

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

  8. 51. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  9. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...

随机推荐

  1. [RN] React Native 使用 realm 数据库

    React Native 使用 realm  数据库 realm 是一款专为移动 ​ 端开发的高性能数据库,其宣称自己是最快的 react-native 数据库. realm 整体的优点有这么四点: ...

  2. Linux性能优化实战学习笔记:第五十三讲

    一.上节回顾 在前面的内容中,我为你介绍了很多性能分析的原理.思路以及相关的工具.不过,在实际的性能分析中,一个很常见的现象是,明明发生了性能瓶颈,但当你登录到服务器中想要排查的时候,却发现瓶颈已经消 ...

  3. [LeetCode] 382. Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  4. x58平台 服务器电源配置 tdp

    我的机器配置: 处理器:    Intel(R) Xeon(R) CPU           X5660  @ 2.80GHz *2主板:    TYAN Tyan Tank GT20-B7002LN ...

  5. 【C/C++开发】C++静态库与动态库以及在Linux和Windows上的创建使用

    原文出处: 吴秦的博客    这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学 ...

  6. [Python学习笔记-008] 使用双向链表去掉重复的文本行

    用Python处理文本文件是极方便的,当文本文件中有较多的重复的行的时候,将那些重复的行数去掉并打印诸如"...<repeats X times>..."有助于更好的浏 ...

  7. docke网络之bridge、host、none

    一.bridge网络 1.创建一个测试容器 [root@localhost ~]# docker run -d -it --name busybox_1 busybox /bin/sh -c &quo ...

  8. 【java】Java多线程总结之线程安全队列Queue【转载】

    原文地址:https://www.cnblogs.com/java-jun-world2099/articles/10165949.html ============================= ...

  9. mini QQ(项目一)

    一个多人聊天工具(C/S结构),实现了如下功能: 一个可视化窗口,支持鼠标点击事件 注册功能,用户可以注册自己的聊天账号, 注册信息包括: 账号名(可以用姓名来替代账号,支持中文), 密码(聊天框输入 ...

  10. vb.net 多线程爬虫抓取免费代理IP

    Class Program Public Shared masterPorxyList As List(Of proxy) = New List(Of proxy)() Public Class pr ...