[leetcode ] word search

来源:互联网 发布:大话设计模式php 编辑:程序博客网 时间:2024/03/29 06:42

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:    struct Point{        int x;        int y;        Point(int x_pos, int y_pos): x(x_pos), y(y_pos){}        bool operator== (Point p) const          {              if (x == p.x && y == p.y)                  return true;              return false;          }      };        bool exist(vector<vector<char> > &board, string word) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if (board.size() == 0)            return false;                    if (word.size()== 0)            return true;                    int h = board.size();        int w = board[0].size();                for(int i = 0; i< h; i++)            for(int j = 0; j<w; j++){                if (board[i][j] == word[0]){                    vector<Point> searched;                    if( find (board, word, 0, searched, i, j))                         return true;                }            }           return false;    }        bool find(vector<vector<char> > &board, string& word, int idx, vector<Point> searched, int x, int y){        if (idx == word.size())            return true;                    if (x< 0 || y<0 || x>= board.size() || y >= board[0].size())            return false;                    Point point(x, y);        for(int i = 0; i < searched.size(); i++){            if (searched[i]==point)                return false;        }                if (board[x][y] == word[idx]){            searched.push_back(point);            return find (board, word, idx+1, searched, x-1, y)||                   find (board, word, idx+1, searched, x+1, y)||                   find (board, word, idx+1, searched, x, y-1)||                   find (board, word,idx+1, searched, x, y+1);        }         return false;                        }};

1. 注意如何写overload operator

2. 不能直接用algorithm里面的find,因为根本没有定义

class Solution {public:    bool find(vector< vector<char> > &board, int i, int j, string &word, int idx)    {        if (idx == word.size()) return true;        if (i < board.size() && j < board[i].size()        && i >= 0 && j >= 0 && board[i][j] == word[idx])        {            board[i][j] = '#';            ++idx;            return find(board, i+1, j, word, idx)            | find(board, i-1, j, word, idx)            | find(board, i, j-1, word, idx)            | find(board, i, j+1, word, idx);        }        return false;    }    bool exist(vector<vector<char> > &board, string word) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        for (int i = 0; i < board.size(); ++i)        {            for (int j = 0; j < board[i].size(); ++j)            {                if (board[i][j] == word[0])                {                    vector<vector<char> > tmp (board);                    bool result = find(tmp, i, j, word, 0);                    if (result == true)                        return true;                }            }        }    }};





原创粉丝点击