728x90
๋ฌธ์
ํ์ด
์ผ์ชฝ ์ค๋ฅธ์ชฝ ์ค์
def turnLeft(now):
if now == 1: return 4 #๋ ๋ถ
if now == 2: return 3 #์ ๋จ
if now == 3: return 1 #๋จ ๋
if now == 4: return 2 #๋ถ ์
def turnRight(now):
if now == 1: return 3 #๋ ๋จ
if now == 2: return 4 #์ ๋ถ
if now == 3: return 2 #๋จ ์
if now == 4: return 1 #๋ถ ๋
์ ๋ ฅ
i, j, dir์ ๋ก๋ด์ ์์น
m, n = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(m)]
visited = [[[False] * 5 for _ in range(n)] for _ in range(m)]
i, j, dir = map(int, input().split())
destI, destJ, destDir = map(int, input().split())
bfs
1,2,3์นธ์ ์์ง์ด๋ ๋์ค ๋ฒฝ์ ๋ง๋๋ฉด ๋ฐ๋ก break๋ฅผ ํด์ฃผ์๋ค. (๋ฒฝ์ ํต๊ณผํ ์ ์์)
def bfs():
q = deque()
q.append((i - 1, j - 1, dir, 0))
visited[i - 1][j - 1][dir] = True
while q:
si, sj, sdir, cnt = q.popleft()
if si == destI - 1 and sj == destJ - 1 and sdir == destDir:
print(cnt)
return
left, right = turnLeft(sdir), turnRight(sdir)
if not visited[si][sj][left]:
visited[si][sj][left] = True
q.append((si, sj, left, cnt + 1))
if not visited[si][sj][right]:
visited[si][sj][right] = True
q.append((si, sj, right, cnt + 1))
if sdir in [1, 2]:
weight = 1
if sdir == 2:
weight = -1
for k in range(1, 4):
nj = sj + weight * k #1,2,3 ๋งํผ ์ด๋
if nj < 0 or nj >= n or visited[si][nj][sdir]:
continue
if graph[si][nj] == 1:
break
visited[si][nj][sdir] = True
q.append((si, nj, sdir, cnt + 1))
if sdir in [3, 4]:
weight = 1
if sdir == 4:
weight = -1
for k in range(1, 4):
ni = si + weight * k #1,2,3 ๋งํผ ์ด๋
if ni < 0 or ni >= m or visited[ni][sj][sdir]:
continue
if graph[ni][sj] == 1:
break
visited[ni][sj][sdir] = True
q.append((ni, sj, sdir, cnt + 1))
์ฝ๋
from collections import deque
input = __import__('sys').stdin.readline
def turnLeft(now):
if now == 1: return 4 #๋ ๋ถ
if now == 2: return 3 #์ ๋จ
if now == 3: return 1 #๋จ ๋
if now == 4: return 2 #๋ถ ์
def turnRight(now):
if now == 1: return 3 #๋ ๋จ
if now == 2: return 4 #์ ๋ถ
if now == 3: return 2 #๋จ ์
if now == 4: return 1 #๋ถ ๋
m, n = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(m)]
visited = [[[False] * 5 for _ in range(n)] for _ in range(m)]
i, j, dir = map(int, input().split())
destI, destJ, destDir = map(int, input().split())
def bfs():
q = deque()
q.append((i - 1, j - 1, dir, 0))
visited[i - 1][j - 1][dir] = True
while q:
si, sj, sdir, cnt = q.popleft()
if si == destI - 1 and sj == destJ - 1 and sdir == destDir:
print(cnt)
return
left, right = turnLeft(sdir), turnRight(sdir)
if not visited[si][sj][left]:
visited[si][sj][left] = True
q.append((si, sj, left, cnt + 1))
if not visited[si][sj][right]:
visited[si][sj][right] = True
q.append((si, sj, right, cnt + 1))
if sdir in [1, 2]:
weight = 1
if sdir == 2:
weight = -1
for k in range(1, 4):
nj = sj + weight * k #1,2,3 ๋งํผ ์ด๋
if nj < 0 or nj >= n or visited[si][nj][sdir]:
continue
if graph[si][nj] == 1:
break
visited[si][nj][sdir] = True
q.append((si, nj, sdir, cnt + 1))
if sdir in [3, 4]:
weight = 1
if sdir == 4:
weight = -1
for k in range(1, 4):
ni = si + weight * k #1,2,3 ๋งํผ ์ด๋
if ni < 0 or ni >= m or visited[ni][sj][sdir]:
continue
if graph[ni][sj] == 1:
break
visited[ni][sj][sdir] = True
q.append((ni, sj, sdir, cnt + 1))
bfs()
๋ง๋ฌด๋ฆฌ
์์์ ํ๋ฆ(?)๋๋ก ๋ฌธ์ ์์ ์ฃผ์ด์ง ๊ฒ์ ๊ตฌํํ๋ ๋ง์๋ค.
728x90