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.

ABC151 D Maze Master

Posted at

from collections import deque

h,w = map(int,input().split())

s = []
for i in range(h):
  s.append(input())
  
q = deque()

steps = [0] * h*w 

# スタートの位置は決まってないので
# スタート位置を決める 
for i in range(h):
  for j in range(w):
    # i,jからスタートする
   
    # 訪れたところ
    visited = [[False for j in range(w)] for i in range(h)] 
    step = 0
    # スタート地点をqに追加する。
    q.append((i,j,step))

    # スタート地点が壁でないかを判別する。
    if s[i][j] == "#":
      continue
      
    while len(q) >= 1:
       # qの先頭を取り出す
      y,x,step = q.popleft()

      # 現在の地点が壁でないか判別
      if s[y][x] == "#":
        continue
      # 行ったことのある場所でないか判別
      if visited[y][x]:
        continue

      visited[y][x] = True
      # 最大歩数を更新する
      steps[i] = max(steps[i],step)

      # 移動さきが壁でないかを判別し
      # 上に移動
      if y-1>=0:
        q.append((y-1,x,step+1))
      # 下に移動
      if y+1<h:
        q.append((y+1,x,step+1))
      # 左に移動
      if x-1>=0:
        q.append((y,x-1,step+1))
      # 右に移動
      if x+1<w:
        q.append((y,x+1,step+1))
     
    
print(max(steps))

0
0
0

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?