`

Word Search (C++)

 
阅读更多

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.

For example,
Given board =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

 

 

class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) {
        if(board.size()<1 || word.size()<1) return false;
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                vector<vector<char> > tmpBoard = vector<vector<char> >(board);
                if(match(tmpBoard, i, j, word, 0)) return true;
            }
        }
    }
    
    bool match(vector<vector<char> > &board, int i, int j, string word, int k) {
        if(i < 0 || i >= board.size() || j < 0 || j >= board[0].size()) return false;
        if(k < 0 || k > word.size()) return false;
        
        if(board[i][j] == word[k]) {
            if(k == word.size()-1) return true;
            
            board[i][j] = '*';
            ++k;
            return 
            match(board, i+1, j, word, k) ||
            match(board, i-1, j, word, k) ||
            match(board, i, j+1, word, k) ||
            match(board, i, j-1, word, k);
        }
        return false;
    }
};

 

欢迎关注微信公众号——计算机视觉:

0
5
分享到:
评论

相关推荐

    Word转PDF源码-C++开发

    #import "C:\\Program Files\\Microsoft Office\\Office14\\MSWORD.OLB" rename_namespace("MSWord") \ auto_search \ auto_rename \ no_auto_exclude \ rename("ExitWindows", "WordExitWindows") #import "C...

    数据结构常用算法c++实现

    数据结构常用算法c++实现,程序目录如下: Array shuffle Prime test(trial division) Prime test(Miller-Rabin's method) 2D Array Arbitary Integer Linear congruential generator Maximum subarray problem Bit...

    c++课程设计单词频率统计

    给定指定单词,统计其在选定文本中出现的频率 ...还需要用到显示每个单词频率的函数frequencydisplay(),在searchword()基础上使需要查找的单词是链表中的每一个不重复的单词;需要主函数来调用定义的函数。

    Bloodshed Dev-C++

    * Implemented search in help files for the word at cursor (context sensitive help) * Implemented "compiler sets" infrastructure to switch between different compilers easily (e.g. gcc-2.95 and gcc-3.2)...

    c++数据结构与算法实现

    WordLadder.cpp: Word Ladder Program and Word Changing Utilities SeparateChaining.h: Header file for separate chaining SeparateChaining.cpp: Implementation for separate chaining TestSeparateChaining...

    Word-Search-Generator:用 C++ 编写的单词搜索拼图生成器

    词搜索生成器 用 C++ 编写的单词搜索拼图生成器 #Usage ./generator &lt;words&gt; 在这里阅读我的文章: 我的小组合作伙伴项目的一部分:

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    Visual C++ 编程资源大全(英文源码 控件)

    (18KB)&lt;END&gt;&lt;br&gt;20,dumpsome.zip DUMPSOME is a project for Visual C++ 4.x that shows how to write a CGI server extension which uses MFC and DAO. (8KB)&lt;END&gt;&lt;br&gt;21,edpos.zip EDPOS is a MDI-based ...

    Dict-Word-Search-Project

    词典词搜索项目

    Visual C++ 编程资源大全(英文源码 其它)

    replaceinfiles.zip Search and Replace in multiple files Add-in for Visual C++ 6.0(83KB)&lt;END&gt;&lt;br&gt;76,RescueAgent.zip A Code Rescue Add-in for Visual C++ Developers(118KB)&lt;END&gt;&lt;br&gt;77,stripem.zip...

    cpp-算法精粹

    Word Search 总结 分治法 Pow(x,n) Sqrt(x) 贪心法 Jump Game Jump Game II Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock II Longest Substring Without Repeating Characters Container With ...

    数据结构Advanced-Data-Structures

    Directed acyclic word graph 374 Multiway trees 376 Ternary search tree 376 And–or tree 379 (a,b)-tree 380 Link/cut tree 381 SPQR tree 381 Spaghetti stack 384 Disjoint-set data structure 385 Space-...

    餐厅信息管理系统.rar

    int Pass_Word(void); void Observe(void); void ListMenu(void); void List_Management(void); void Main_Menu(void); void Get_Rank(void); void Menu_select(void); 。。。。。。。。。。。。。

    TF-IDF.zip_多国语言处理_Visual_C++_

    The tf–idf weight (term frequency–inverse document frequency) is a weight often used in ... Variations of the tf–idf weighting scheme are often used by search engines as a central tool in scorin

    编程珠玑全部源代码 分享

    sign.c -- Sign each word by its letters in sorted order. squash.c -- Put each anagram class on a single line. Column 5: Scaffolding for testing and timing search functions search.c -- Linear and ...

    通讯录的制作-数据结构课程设计

    可以在Microsoft Visual C++ 上运行没有错误包括论文word文档,答辩的ppt等 设计内容: 本系统应完成一下几方面的功能: ①输入信息(Enter()): 调用此函数用以输入数据到内存中,此过程包括建立相应的链表或相应...

    notepad2

    - Useful word, line and block editing shortcuts - Rectangular selection (Alt+Mouse) - Brace matching, auto indent, long line marker, zoom functions - Support for Unicode, UTF-8, Unix and ...

    clucene源码

    It is a high-performance, full-featured text search engine written in C++. CLucene is faster than lucene as it is written in C++. ------------------------------------------------------ CLucene has ...

    饭店就餐管理系统

    int search(void);//查找匹配的餐桌 ORDER* Dish_Order(ORDER *,ORDER *);//将节点插入到主链 void Display(int);//显示菜的信息; void saveInList(float);//将客户消费额写入账单 void Save_Inform(ORDER *,int);/...

Global site tag (gtag.js) - Google Analytics