0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【ポートフォリオ】Pythonで迷路問題を解く

Posted at

!ポートフォリオ代わりのアウトプットです!

某所で公開されている迷路探索問題についてPythonで解いたもの。

条件

  • "S"がスタート位置、"."が通行可能な通路、"#"が壁である
  • 迷路のマップが1行毎に標準入力される
  • 迷路は二次元である
  • 脱出できる迷路である場合YESを出力、そうでない場合はNOを出力する
# プレイヤークラス
class Player():
    def __init__(self):
        self.now = [0, 0]
        self.move_log = ['move_right']
        self.pos_log = []
    
    def move_right(self):
        self.now = [self.now[0], self.now[1] + 1]
    
    def move_left(self):
        self.now = [self.now[0], self.now[1] - 1]
        
    def move_up(self):
        self.now = [self.now[0] - 1, self.now[1]]
    
    def move_down(self):
        self.now = [self.now[0] + 1, self.now[1]]
    
    @property
    def get_now(self):
        return self.now
    
    def set_now(self, position):
        self.now = position
    
    @property
    def get_move_log(self):
        return self.move_log
    
    def set_move_log(self, move):
        self.move_log.append(move)
    
    def get_allow_ahead(self, game_map):
        flg = False
        if self.move_log[-1] == 'move_right':
            if not game_map[now_positon[0]][now_positon[1] + 1] == '#':
                flg = True
        if self.move_log[-1] == 'move_left':
            if not game_map[now_positon[0]][now_positon[1] - 1] == '#':
                flg = True
        if self.move_log[-1] == 'move_up':
            if not game_map[now_positon[0] - 1][now_positon[1]] == '#':
                flg = True
        if self.move_log[-1] == 'move_down':
            if not game_map[now_positon[0] + 1][now_positon[1]] == '#':
                flg = True
        return flg
        
    def move_ahead(self):
        getattr(self, self.move_log[-1])()
        self.set_move_log(self.move_log[-1])
    
    
    @property
    def get_pos_log(self):
        return self.pos_log
    
    def set_pos_log(self, position):
        self.pos_log.append(position)
    
# 迷路の入力
i = input().split(' ')
row = int(i[0])
col = int(i[1])
game_map = []
for i in range(row):
    game_map.append(list(input()))

pl = Player()

# プレイヤーの初期位置を取得
for a, map_row in enumerate(game_map):
    for b, value in enumerate(map_row):
        if value == 'S':
            pl.set_now([a, b])
            break

# 迷路を解く
while True:
    pl.set_pos_log(pl.get_now)
    now_positon = pl.get_now
    
    if pl.get_now[0] == 0 or pl.get_now[1] == 0 or pl.get_now[0] == (row - 1) or pl.get_now[1] == (col - 1):
        print('YES')
        break
    
    if pl.get_allow_ahead(game_map):
        pl.move_ahead()
        
    elif not game_map[now_positon[0]][now_positon[1] + 1] == '#' and not [now_positon[0],now_positon[1] + 1] in pl.get_pos_log:
        pl.move_right()
        pl.set_move_log('move_right')
        
    elif not game_map[now_positon[0]][now_positon[1] - 1] == '#' and not [now_positon[0],now_positon[1] - 1] in pl.get_pos_log:
        pl.move_left()
        pl.set_move_log('move_left')
        
    elif not game_map[now_positon[0] - 1][now_positon[1]] == '#' and not [now_positon[0] - 1,now_positon[1]] in pl.get_pos_log:
        pl.move_up()
        pl.set_move_log('move_up')
        
    elif not game_map[now_positon[0] + 1][now_positon[1]] == '#' and not [now_positon[0] + 1,now_positon[1]] in pl.get_pos_log:
        pl.move_down()
        pl.set_move_log('move_down')
        
    else:
        print('NO')
        break




0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?