惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

楚天乐的全栈之路

procrastinated单词终极指南:发音/中文解释/英文解释/词源/例句全解 | myxtea学英语 - 楚天乐的全栈之路 Vocabulary: Prolific - 楚天乐的全栈之路 DCT离散余弦变换和JPEG 压缩算法 - 楚天乐的全栈之路 c语言float和bytes array转换 - 楚天乐的全栈之路 使用Vault管理服务器各种密码 - 楚天乐的全栈之路 windows环境pip无法安装dlib库的终极解决 - 楚天乐的全栈之路 Windows WLS2使用本机ss代理访问github - 楚天乐的全栈之路 写个dockerfile自动部署hugo - 楚天乐的全栈之路 php中__METHOD__和_FUNCTION__的区别 - 楚天乐的全栈之路
地图找出口算法python实现 - 楚天乐的全栈之路
2024-03-20 · via 楚天乐的全栈之路

算法描述

代码实现

import copy

m = [
    [0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, ],
    [1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ],
    [0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, ],
    [0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, ],
    [0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, ],
    [0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, ],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, ],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, ],
    [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, ],
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, ],
    [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, ],
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, ],
    [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, ],
    [0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, ],
    [0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, ],
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, ],
]

LEFT = "Left"
RIGHT = "Right"
UP = "Up"
DOWN = "Down"

class Map:
    def __init__(self, m):
        self.m = m
        self.result = copy.deepcopy(m)
        self.current = [0, 0]
        self.direction = RIGHT
        self.height = len(m)
        self.width = len(m[0])
        self.left_translations = dict(Right=UP, Up=LEFT, Left=DOWN, Down=RIGHT)
        self.right_translations = dict(Right=DOWN, Up=RIGHT, Left=UP, Down=LEFT)

    def get_next_position(self, direction):
        if direction == UP:
            return [self.current[0], self.current[1]-1]
        elif direction == RIGHT:
            return [self.current[0]+1, self.current[1]]
        elif direction == DOWN:
            return [self.current[0], self.current[1]+1]
        elif direction == LEFT:
            return [self.current[0]-1, self.current[1]]
        else:
            raise "invalid direction"

    def is_available(self, position) -> bool:
        if position[0] < 0 or position[0] >= self.width or position[1] < 0 or position[1] >= self.height:
            return False
        return self.m[position[1]][position[0]] == 0

    def execute(self):
        step = 1
        self.show(step)
        while True:
            new_direction = self.right_translations[self.direction]
            next_position = self.get_next_position(new_direction)
            if self.is_available(next_position):
                self.direction = new_direction
                if self.has_change(next_position):
                    self.current = next_position
                    step += 1
                    self.show(step)
                    print("-----------------")
            else:
                self.direction = self.left_translations[self.direction]

            if self.finished():
                break
        print("final result")
        self.print_matrix(self.result)

    def finished(self):
        if self.current[0] == len(self.m[1])-1 and self.current[1] == len(self.m)-1:
            return True
        return False

    def has_change(self, new_position):
        return self.current[0] != new_position[0] or self.current[1] != new_position[1]

    def show(self, step):
        print("\n------------------ step : ", step)
        #self.print_matrix(self.m)
        for i in range(len(self.m)):
            for j in range(len(self.m[i])):
                if i == self.current[1] and j == self.current[0]:
                    print("X", end="   ")
                else:
                    print(m[i][j], end="   ")
            print()
        self.result[self.current[1]][self.current[0]] = "X"

    def print_matrix(self, matrix):
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if i == self.current[1] and j == self.current[0]:
                    print("X", end="   ")
                else:
                    print(matrix[i][j], end="   ")
            print()
inst = Map(m)
inst.execute()

执行结果和经过路径
Picture1.png