leetcode-490. 迷宫-【DFS】【BFS】


在这里插入图片描述
在这里插入图片描述

输入:maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
输出:true
解释:一种可能的路径是 : 左 -> 下 -> 左 -> 下 -> 右 -> 下 -> 右。

解题思路

简单的dfs
特殊的是,一般的dfs只能走一步,而这个走一步就必须走到头
分别向四个方向搜索就行了
标记一下走过的位置vis[101][101],不然不同的路径会重复走该位置,时间复杂度会爆掉

代码

class Solution {
public:

    bool isok = false;
    bool vis[101][101];

    void dfs(vector<vector<int>>& maze,int i,int j,vector<int>& destination){
        int r = maze.size();
        int c = maze[0].size();
        int destR = destination[0];
        int destC = destination[1];
        

        if(isok) return; //剪枝,已经有路径走得通了,让其他还在栈里的元素,退出栈
        if(i < 0 || i >= r || j < 0 || j >= c) return; //边界,结束
        if(i == destR && j == destC){ //结束条件
            isok = true;
            return;
        }  

        vis[i][j] = 1;
        //向左边搜索(只能一下子到最左边)
        //找到最左边的下标 
        int i1 = i,j1 = j;
        while(j1 >= 0 &&  maze[i1][j1] == 0 ) j1--;
        j1++; //走到墙了,或者走到边界了,退回来一步
        if(!vis[i1][j1]) dfs(maze,i1,j1,destination);

        //向上搜索
        int i2 = i,j2 = j;
        while(i2 >= 0 &&  maze[i2][j2] == 0 )  i2--;  
        i2++; //走到墙了,或者走到边界了,退回来一步
        if(!vis[i2][j2]) dfs(maze,i2,j2,destination);

        //向右搜索
        int i3 = i,j3 = j;
        while( j3 <= c-1  && maze[i3][j3] == 0 )j3++;
        j3--; //走到墙了,或者走到边界了,退回来一步
        if(!vis[i3][j3]) dfs(maze,i3,j3,destination);

        //向下搜索
        int i4 = i,j4 = j;
        while(i4 <= r-1  && maze[i4][j4] == 0 )  i4++;
        i4--; //走到墙了,或者走到边界了,退回来一步
        if(!vis[i4][j4]) dfs(maze,i4,j4,destination);
    }
    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        memset(vis,0,sizeof(vis));
        int startR = start[0];
        int startC = start[1];
        dfs(maze,startR,startC,destination);
        return isok;
    }
};

java dfs

class Solution {

    boolean vis[][] = new boolean[101][101];
    boolean isok = false;
    void dfs(int[][] maze,int i,int j,int di,int dj){
        int r = maze.length;
        int c = maze[0].length;
        if(i < 0 || i >= r || j < 0 || j >= c) return;
        if(isok) return;
        if(i == di && j == dj){
            isok = true;
            return;
        }
        vis[i][j] = true;
        int i1 = i,i2 = i,i3 = i,i4 = i;
        int j1 = j,j2 = j,j3 = j,j4 = j;
        while(j1 >= 0 && maze[i1][j1] == 0) j1--;
        j1++; if(!vis[i1][j1]) dfs(maze,i1,j1,di,dj);
        while(j2 <= c-1 && maze[i2][j2] == 0) j2++;
        j2--; if(!vis[i2][j2]) dfs(maze,i2,j2,di,dj);
        while(i3 >= 0 && maze[i3][j3] == 0) i3--;
        i3++; if(!vis[i3][j3]) dfs(maze,i3,j3,di,dj);
        while(i4 <= r-1 && maze[i4][j4] == 0) i4++;
        i4--; if(!vis[i4][j4]) dfs(maze,i4,j4,di,dj);
    }

    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
       for(int i = 0; i < 101; i++)
        for(int j = 0; j < 101; j++) vis[i][j] = false;
        int si = start[0];
        int sj = start[1];
        int di = destination[0];
        int dj = destination[1];
        dfs(maze,si,sj,di,dj);
        return isok;
    }
}

c++ bfs

class Solution {
public:
    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        int r = maze.size();
        int c = maze[0].size();
        int si = start[0];
        int sj = start[1];
        int di = destination[0];
        int dj = destination[1];
        queue<pair<int,int>> q;
        bool vis[101][101];
        memset(vis,0,sizeof(vis));
        q.push(make_pair(si,sj));
        while(!q.empty()){
            int i = q.front().first;
            int j = q.front().second;
            if(i == di && j == dj) return true;
            vis[i][j] = 1;
            q.pop();
            int i1 = i,i2 = i,i3 = i,i4 = i;
            int j1 = j,j2 = j,j3 = j,j4 = j;
            while(j1 >= 0 && maze[i1][j1] == 0) j1--;
            j1++; if(!vis[i1][j1]) q.push(make_pair(i1,j1));
            while(j2 <= c-1 && maze[i2][j2] == 0) j2++;
            j2--; if(!vis[i2][j2]) q.push(make_pair(i2,j2));
            while(i3 >= 0 && maze[i3][j3] == 0) i3--;
            i3++; if(!vis[i3][j3]) q.push(make_pair(i3,j3));
            while(i4 <= r-1 && maze[i4][j4] == 0) i4++;
            i4--; if(!vis[i4][j4]) q.push(make_pair(i4,j4));
        }
        return false;

    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值