leetcode word_search

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”, -> returnstrue,
word =“SEE”, -> returnstrue,
word =“ABCB”, -> returnsfalse.

回朔

class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) 
    {
        vector<vector<bool> > flag(board.size(), vector<bool> (board[0].size(), false));
        for (int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[i].size();j++)
            {
                if (board[i][j] == word[0])
                {
                    flag[i][j] = true;
                    if (find(board, flag, word.substr(1), i, j))
                        return true;
                    flag[i][j] = false;
                }
            }
        }
        return false;
    }
    
    bool find(vector<vector<char> > &board, vector<vector<bool> > &flag, string word, int lastx, int lasty)
    {
        if (word.size() == 0)
            return true;
        bool ret = false;
        if(lastx > 0 && board[lastx-1][lasty] == word[0]&&!flag[lastx-1][lasty])
        {
            flag[lastx-1][lasty] = true;
            if(find(board,flag, word.substr(1), lastx-1,lasty))
                return true;
            flag[lastx-1][lasty] = false;//就算flag不是引用  这里也要回复,不然会对下面有影响
        }
        if(lastx < board.size()-1 && board[lastx+1][lasty] == word[0]&&!flag[lastx+1][lasty])
        {
            flag[lastx+1][lasty] = true;
            if (find(board,flag, word.substr(1), lastx+1,lasty))
                return true;
            flag[lastx+1][lasty] = false;
        }
        if(lasty > 0 && board[lastx][lasty-1] == word[0]&&!flag[lastx][lasty-1])
        {
            flag[lastx][lasty-1] = true;
            if (find(board,flag, word.substr(1), lastx,lasty-1))
                return true;
            flag[lastx][lasty-1] = false;
        }
        if(lasty < board[0].size()-1 && board[lastx][lasty+1] == word[0]&&!flag[lastx][lasty+1])
        {
            flag[lastx][lasty+1] = true;
            if (find(board,flag, word.substr(1), lastx,lasty+1))
                return true;
            flag[lastx][lasty+1] = false;
        }
        return false;
    }
};

//word改成引用  节省空间
class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) 
    {
        vector<vector<bool> > flag(board.size(), vector<bool> (board[0].size(), false));
        for (int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[i].size();j++)
            {
                if (board[i][j] == word[0])
                {
                    flag[i][j] = true;
                    if (find(board, flag, word, i, j, 1))
                        return true;
                    flag[i][j] = false;
                }
            }
        }
        return false;
    }
    
    bool find(vector<vector<char> > &board, vector<vector<bool> > &flag, string &word, int lastx, int lasty, int sequence)
    {
        if (word.size() == sequence)
            return true;
        bool ret = false;
        if(lastx > 0 && board[lastx-1][lasty] == word[sequence]&&!flag[lastx-1][lasty])
        {
            flag[lastx-1][lasty] = true;
            if(find(board,flag, word, lastx-1,lasty,sequence+1 ))
                return true;
            flag[lastx-1][lasty] = false;//就算flag不是引用  这里也要恢复,不然会对下面有影响
        }
        if(lastx < board.size()-1 && board[lastx+1][lasty] == word[sequence]&&!flag[lastx+1][lasty])
        {
            flag[lastx+1][lasty] = true;
            if (find(board,flag, word, lastx+1,lasty,sequence+1))
                return true;
            flag[lastx+1][lasty] = false;
        }
        if(lasty > 0 && board[lastx][lasty-1] == word[sequence]&&!flag[lastx][lasty-1])
        {
            flag[lastx][lasty-1] = true;
            if (find(board,flag, word, lastx,lasty-1,sequence+1))
                return true;
            flag[lastx][lasty-1] = false;
        }
        if(lasty < board[0].size()-1 && board[lastx][lasty+1] == word[sequence]&&!flag[lastx][lasty+1])
        {
            flag[lastx][lasty+1] = true;
            if (find(board,flag, word, lastx,lasty+1,sequence+1))
                return true;
            flag[lastx][lasty+1] = false;
        }
        return false;
    }
};

//宏简化
#define fun(x,y) do \
{\
    flag[x][y] = true;\
    if(find(board,flag, word, x, y,sequence+1))\
        return true;\
    flag[x][y] = false;\
}while(0)

class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) 
    {
        vector<vector<bool> > flag(board.size(), vector<bool> (board[0].size(), false));
        for (int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[i].size();j++)
            {
                if (board[i][j] == word[0])
                {
                    flag[i][j] = true;
                    if (find(board, flag, word, i, j, 1))
                        return true;
                    flag[i][j] = false;
                }
            }
        }
        return false;
    }
    
    bool find(vector<vector<char> > &board, vector<vector<bool> > &flag, string &word, int lastx, int lasty, int sequence)
    {
        if (word.size() == sequence)
            return true;
        bool ret = false;
        if(lastx > 0 && board[lastx-1][lasty] == word[sequence]&&!flag[lastx-1][lasty])
            fun(lastx-1, lasty);
        if(lastx < board.size()-1 && board[lastx+1][lasty] == word[sequence]&&!flag[lastx+1][lasty])
           fun(lastx+1, lasty);
        if(lasty > 0 && board[lastx][lasty-1] == word[sequence]&&!flag[lastx][lasty-1])
            fun(lastx, lasty-1);
        if(lasty < board[0].size()-1 && board[lastx][lasty+1] == word[sequence]&&!flag[lastx][lasty+1])
            fun(lastx, lasty+1);
        return false;
    }
};

测试

#include<iostream>
#include<string>
#include<vector>
using namespace std;
#define fun(x,y) do \
{\
    flag[x][y] = true;\
    if(find(board,flag, word, x, y,sequence+1))\
        return true;\
    flag[x][y] = false;\
}while(0)
    


class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) 
    {
        vector<vector<bool> > flag(board.size(), vector<bool> (board[0].size(), false));
        for (int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[i].size();j++)
            {
                if (board[i][j] == word[0])
                {
                    flag[i][j] = true;
                    if (find(board, flag, word.substr(1), i, j))
                        return true;
                    flag[i][j] = false;
                }
            }
        }
        return false;
    }
    
    bool find(vector<vector<char> > &board, vector<vector<bool> > &flag, string word, int lastx, int lasty)
    {
        if (word.size() == 0)
            return true;
        //cout<<word<<" "<<lastx<<" "<<lasty<<" "<<endl;
        if(lastx > 0 && board[lastx-1][lasty] == word[0]&&!flag[lastx-1][lasty])
        {
            flag[lastx-1][lasty] = true;
            if(find(board,flag, word.substr(1), lastx-1,lasty))
                return true;
            flag[lastx-1][lasty] = false;
        }
        if(lastx < board.size()-1 && board[lastx+1][lasty] == word[0]&&!flag[lastx+1][lasty])
        {
            flag[lastx+1][lasty] = true;
            if (find(board,flag, word.substr(1), lastx+1,lasty))
                return true;
            flag[lastx+1][lasty] = false;
        }
        if(lasty > 0 && board[lastx][lasty-1] == word[0]&&!flag[lastx][lasty-1])
        {
            flag[lastx][lasty-1] = true;
            if (find(board,flag, word.substr(1), lastx,lasty-1))
                return true;
            flag[lastx][lasty-1] = false;
        }
        if(lasty < board[0].size()-1 && board[lastx][lasty+1] == word[0]&&!flag[lastx][lasty+1])
        {
            flag[lastx][lasty+1] = true;
            if (find(board,flag, word.substr(1), lastx,lasty+1))
                return true;
            flag[lastx][lasty+1] = false;
        }
        return false;
    }
};

class Solution1 {
public:
    bool exist(vector<vector<char> > &board, string word) 
    {
        vector<vector<bool> > flag(board.size(), vector<bool> (board[0].size(), false));
        for (int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[i].size();j++)
            {
                if (board[i][j] == word[0])
                {
                    flag[i][j] = true;
                    if (find(board, flag, word, i, j, 1))
                        return true;
                    flag[i][j] = false;
                }
            }
        }
        return false;
    }
    
    bool find(vector<vector<char> > &board, vector<vector<bool> > &flag, string &word, int lastx, int lasty, int sequence)
    {
        if (word.size() == sequence)
            return true;
        bool ret = false;
        if(lastx > 0 && board[lastx-1][lasty] == word[sequence]&&!flag[lastx-1][lasty])
            fun(lastx-1, lasty);
        if(lastx < board.size()-1 && board[lastx+1][lasty] == word[sequence]&&!flag[lastx+1][lasty])
           fun(lastx+1, lasty);
        if(lasty > 0 && board[lastx][lasty-1] == word[sequence]&&!flag[lastx][lasty-1])
            fun(lastx, lasty-1);
        if(lasty < board[0].size()-1 && board[lastx][lasty+1] == word[sequence]&&!flag[lastx][lasty+1])
            fun(lastx, lasty+1);
        return false;
    }
};
/*
Given board =
[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]
word ="ABCCED", -> returnstrue,
word ="SEE", -> returnstrue,
word ="ABCB", -> returnsfalse.
*/
int main()
{
    vector<vector<char> >  board;
    char a1[] = {'A', 'B', 'C', 'E'};
    vector<char> vec1(a1,a1+4);
    board.push_back(vec1);
    char a2[] = {'S', 'F', 'E', 'S'};
    vector<char> vec2(a2,a2+4);
    board.push_back(vec2);
    char a3[] = {'A', 'D', 'E', 'E'};
    vector<char> vec3(a3,a3+4);
    board.push_back(vec3);
    for (int i=0;i<board.size();i++)
    {
        for(int j=0;j<board[i].size();j++)
        {
            cout<<board[i][j]<<" ";
        }
        cout<<endl;
    }
    
    Solution1 s;
    
    if (s.exist(board, "ABCESEEEFS"))
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
    
    if (s.exist(board, "SEE"))
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
    
    if (s.exist(board, "ABCB"))
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值