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

推荐订阅源

NISL@THU
NISL@THU
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Cloudbric
Cloudbric
H
Heimdal Security Blog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
A
Arctic Wolf
W
WeLiveSecurity
SecWiki News
SecWiki News
S
Security Affairs
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
T
The Exploit Database - CXSecurity.com
V2EX - 技术
V2EX - 技术
AI
AI
Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
有赞技术团队
有赞技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
H
Hacker News: Front Page
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
月光博客
月光博客
博客园 - 【当耐特】
Recorded Future
Recorded Future
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
Scott Helme
Scott Helme
N
News and Events Feed by Topic
Help Net Security
Help Net Security
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
小众软件
小众软件

楚天乐的全栈之路

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