题解 | 迷宫寻路

迷宫寻路

https://www.nowcoder.com/practice/0c8930e517444d04b426e9703d483ed4

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    const graph = [];
    const read = await readline();
    let ans = 'No'
    const [n,m] =read.split(' ').map(Number);
    while(line = await readline()){
        let tokens = line.split('')
        graph.push(tokens);
    }
    function dfs(graph){
        if(!graph||graph.length === 0)return;
        const visited = new Set();
        const stack = [];
        function helper(r,c){
            if(r<0||c<0||r>n-1||c>m-1)return;
            if(graph[r][c]!=='.'){
                return ;
            }
            if(r===n-1&&c===m-1){
                ans = 'Yes';
            }
            visited.add([r,c]);
            graph[r][c] = '#';
            //想四个方向搜索道路
            helper(r-1,c);
            helper(r+1,c);
            helper(r,c-1);
            helper(r,c+1);
        }
        helper(0,0)
        console.log(ans)
    }
    dfs(graph);
}()

全部评论

相关推荐

11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
迷茫的大四🐶:那你问他上班之后老实了没
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务