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

推荐订阅源

L
LINUX DO - 最新话题
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Vercel News
Vercel News
Cloudbric
Cloudbric
Recent Announcements
Recent Announcements
O
OpenAI News
Y
Y Combinator Blog
Hacker News: Ask HN
Hacker News: Ask HN
I
InfoQ
WordPress大学
WordPress大学
S
Secure Thoughts
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
小众软件
小众软件
I
Intezer
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
GRAHAM CLULEY
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
美团技术团队
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
C
Check Point Blog
H
Help Net Security
博客园 - Franky
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Project Zero
Project Zero
N
Netflix TechBlog - Medium
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V2EX - 技术
V2EX - 技术
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
V
V2EX
C
Cybersecurity and Infrastructure Security Agency CISA
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
Apple Machine Learning Research
Apple Machine Learning Research

楚天乐的全栈之路

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-19 · 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