题解 | 迷宫寻路

迷宫寻路

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

package main

import (
	"fmt"
)

func main() {
	var x, y int
	fmt.Scan(&x)
	fmt.Scan(&y)
	data := make([][]string, x+1)
	for i := 1; i <= x; i++ {
        var temp string
        fmt.Scan(&temp)
        yItem := make([]string, len(temp)+1)
        for j := 1; j<=len(temp);j++{
            yItem[j] = string(temp[j-1])
        }
		data[i] = yItem
	}
    // fmt.Println(data)

    var dfs func(x, y int) bool

    pointRule := make([][]int, 0)
    dfs = func(pointX, pointY int) bool {
        if (x < pointX || y < pointY || pointX < 1 || pointY < 1 || data[pointX][pointY] != ".") {
            return false
        }

        data[pointX][pointY] = "#"
        pointRule = append(pointRule, []int{pointX, pointY})
        // fmt.Println(pointRule, pointX, pointY)
        if dfs(pointX, pointY+1) {
            return true
        }
        if dfs(pointX+1, pointY) {
            return true
        }
        if dfs(pointX, pointY-1) {
            return true
        }
        if dfs(pointX-1, pointY) {
            return true
        }
        if (pointX == x && pointY == y) {
            return true
        }
        pointRule = pointRule[0:len(pointRule)-1]        
        return false
    }


    if dfs(1, 1) {
        fmt.Printf("Yes")
    } else {
        fmt.Printf("No")
    }
}














全部评论

相关推荐

11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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