[LeetCode]Word Search

本文介绍了一种使用C++实现的迷宫寻路算法,通过深度优先搜索(DFS)来查找给定字符矩阵中是否存在指定字符串路径。文章详细阐述了如何遍历二维字符数组并检查是否可以找到目标单词,同时提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int Dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
class Solution {
//DFS Find the path
public:
	
	void DFS(int curX, int curY, int curPos, vector<vector<bool>>& visit, 
		bool& OK, vector<vector<char>> &board, string& word )
	{
		if(OK) return;
		if(word[curPos] != board[curX][curY]) return;
		if(curPos == word.size()-1)
		{
			OK = true;
			return;
		}
		for (int d = 0; d < 4; ++d)
		{
			int nextX = curX+Dir[d][0];
			int nextY = curY+Dir[d][1];
			if(nextX >= 0 && nextX < board.size() && nextY >= 0 && nextY < board[0].size())
			{
				if(!visit[nextX][nextY])
				{
					visit[nextX][nextY] = true;
					DFS(nextX, nextY, curPos+1, visit, OK, board, word);
					visit[nextX][nextY] = false;
				}
			}

		}
	}
public:
	bool exist(vector<vector<char> > &board, string word) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		if(word.size() == 0) return false;
		if(board.size() == 0 || board[0].size() == 0) return false;
		bool OK = false;
		vector<vector<bool>> visit(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)
			{
				visit[i][j] = true;
				DFS(i, j, 0, visit, OK, board, word);
				visit[i][j] = false;
				if(OK) return true;
			}
		}
		
		return false;
	}
};

second time

class Solution {
public:
    int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    bool findPath(vector<vector<char> > &board, string& word, vector<vector<bool> >& used, int curX, int curY, int curIdx)
    {
        int n = board.size();
        int m = board[curX].size();
        if(curIdx == word.size()) return true;
        for(int k = 0; k < 4; ++k)
        {
            int newX = curX+dir[k][0];
            int newY = curY+dir[k][1];
            if(newX >= 0 && newX < n && newY >= 0 && newY < m && used[newX][newY] == false && board[newX][newY] == word[curIdx])
            {
                used[newX][newY] = true;
                if(findPath(board, word, used, newX, newY, curIdx+1)) return true;
                used[newX][newY] = false;
            }
        }
        return false;
    }
    bool exist(vector<vector<char> > &board, string word) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(word.size() == 0) return true;
        int n = board.size();
        if(n == 0) return false;
        int m = board[0].size();
        
        vector<vector<bool> > used(n, vector<bool>(n, false));
        for(int i = 0; i < board.size(); ++i)
        {
            for(int j = 0; j < board[i].size(); ++j)
            {
                if(board[i][j] == word[0])
                {
                    used[i][j] = true;
                    if(findPath(board, word, used, i, j, 1)) return true;
                    used[i][j] = false;
                }
            }
        }
        return false;
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值