~๋ชฉ์ฐจ~
๋ฌธ์
https://softeer.ai/practice/6246
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
- '์์๊ฐ ์๋ ๋ฐฉ๋ฌธ ๋ฃจํธ ๊ตฌํ๊ธฐ'๋ผ๊ณ ์ธ์งํ์๋ง์ DFS(๊น์ด ์ฐ์ ํ์)์์ ์ธ์งํ๋ค.
- ๋ฐฉ๋ฌธ ํ์๋ฅผ ์ํ visited ๋ฐฐ์ด์ด ํ์ํจ์ ๋ ์ฌ๋ ธ๋ค.
- ํ์ฌ ์์น๋ฅผ ๊ธฐ์ค์ผ๋ก ์ํ์ข์ฐ๋ฅผ ๋ณด์์ ๋
1) ์ ํจํ ๊ณต๊ฐ( 0 ~ n-1 )์ ๋ฒ์ด๋์ง ์๊ณ
2) ๋ฐฉ๋ฌธํ์ง ์์๊ณ
3) ๋ฒฝ์ด ์๋
- ์์๋ฅผ ์งํค๊ณ ์๋๊ฐ?
์ฒซ์๋) ๋ฐฉ๋ฌธํ ์์น๊ฐ ์ง๋์ผํ ์์น ๋ฆฌ์คํธ์ ํฌํจ๋๋๋ฐ, ์์๊ฐ ๋ง์ง ์์ ๋ ํด๋น ๋ฃจํธ๋ ๋์ด์ ํ์ํ์ง ์๋๋ค
=> ์ฒซ์๋์์ ๋ฌธ์ ๊ฐ ๋์๋ ๊ฒ์ ์๋ชป ๋ ๋ฌธ๋ฒ์ ์ฌ์ฉ๊ณผ ๋ณต์กํ๊ณ ๊ตฌ๋ฉ ๋ซ๋ฆฐ ์กฐ๊ฑด๋ฌธ ๋๋ฌธ์ด๋ค.
if (row, col) in points:
if not points[depth-1] != (row, col):
continue
else:
visited[row][col] = True
dfs(row, col, depth+1)
1. `(row, col) in points`:
- ์ด ์กฐ๊ฑด์ ํ์ฌ ํ์ ์ค์ธ (row, col) ์ขํ๊ฐ points ๋ฆฌ์คํธ์ ํฌํจ๋์ด ์๋์ง๋ฅผ ํ์ธํฉ๋๋ค.
- points ๋ฆฌ์คํธ๋ ๋ฐฉ๋ฌธํด์ผ ํ ์ฒดํฌํฌ์ธํธ๋ค์ ์ขํ๋ฅผ ์ ์ฅํ ๋ฆฌ์คํธ์ ๋๋ค.
- ์ด ์กฐ๊ฑด์ด ์ฐธ์ด๋ฉด, row์ col์ด ์ฒดํฌํฌ์ธํธ ์ค ํ๋๋ผ๋ ์๋ฏธ
2. `if not points[depth-1] != (row, col)`:
- depth๊ฐ ๋ํ๋ด๋ ๊ฒ์ ๋ค์ ๋ฐฉ๋ฌธํด์ผํ ์ฒดํฌํฌ์ธํธ ์์๋ฅผ ์๋ฏธํ๋ค.
- ๊ทธ๋ฌ๋ฏ๋ก depth-1๋ก ์ด์ ๋ฐฉ๋ฌธํ ์ฒดํฌํฌ์ธํธ๋ฅผ ํ์ธํ ๊ฒ์ด ์๋๋ผ depth์ ์ฒดํฌํฌ์ธํธ ์์น๋ฅผ ํ์ธํด์ผํ๋ค.
์ฌ์๋) ๋ฐฉ๋ฌธํ ์์น๊ฐ ๋ค์ ์ง๋์ผํ ์์์ ๋ถํฉํ๋ค๋ฉด depth+1๋ก dfs, ๋ถํฉํ์ง ์๋๋ค๋ฉด depth๋ก dfs
- ๋ฐ์ฑ : ๊ดํ ๊ฐ์ง์น๊ธฐ๋ฅผ ์๋ํ์๋ค๊ฐ ๊ตฌ๋ฉ ๋ซ๋ฆฐ ์กฐ๊ฑด๋ฌธ์ ์คํํ์ง ๋ง๊ณ ์ํ์ ์ํํ๊ณ ์ถ๊ฐ ์กฐ๊ฑด์ ๋ฌ์๋๊ฐ๋ ๋ฐฉ์์ ์ ํํ์.
์์ฑ ์ฝ๋
์คํจ ์ฝ๋
import sys
input = sys.stdin.readline
# ๊ฒฉ์์ ํฌ๊ธฐ, ๋ฐฉ๋ฌธ ํฌ์ธํธ ๊ฐ์
n, m = map(int, input().split())
# n๊ฐ์ ์
board = []
for i in range(n):
board.append(list(map(int, input().split())))
# ๋ฐฉ๋ฌธํด์ผ ํ m๊ฐ์ ์นธ์ ์์น(x, y)
points = []
for i in range(m):
x, y = map(int, input().split())
points.append((x-1, y-1))
dr = [0, 1, 0, -1]
dc = [1, 0, -1, 0]
visited = [[False]*n for _ in range(n)]
answer = 0
def dfs(r, c, depth):
global answer
if depth == m:
answer += 1
return
for d in range(4):
row = r + dr[d]
col = c + dc[d]
if -1 < row < n and -1 < col < n and not board[row][col] and not visited[row][col]:
if (row, col) in points:
if not points[depth-1] != (row, col):
continue
else:
visited[row][col] = True
dfs(row, col, depth+1)
else:
visited[row][col] = True
dfs(row, col, depth)
visited[row][col] = False
visited[points[0][0]][points[0][1]] = True
dfs(points[0][0], points[0][1], 1)
print(answer)
์ฑ๊ณต ์ฝ๋
import sys
input = sys.stdin.readline
# ๊ฒฉ์์ ํฌ๊ธฐ, ๋ฐฉ๋ฌธ ํฌ์ธํธ ๊ฐ์
n, m = map(int, input().split())
# n๊ฐ์ ์
board = []
for i in range(n):
board.append(list(map(int, input().split())))
# ๋ฐฉ๋ฌธํด์ผ ํ m๊ฐ์ ์นธ์ ์์น(x, y)
points = []
for i in range(m):
x, y = map(int, input().split())
points.append((x-1, y-1))
dr = [0, 1, 0, -1]
dc = [1, 0, -1, 0]
visited = [[False]*n for _ in range(n)]
answer = 0
def dfs(r, c, depth):
global answer
if depth == m:
answer += 1
return
elif (r, c) == points[-1]:
return
for d in range(4):
row = r + dr[d]
col = c + dc[d]
if -1 < row < n and -1 < col < n and not board[row][col] and not visited[row][col]:
visited[row][col] = True
if points[depth] == (row, col):
dfs(row, col, depth+1)
else:
dfs(row, col, depth)
visited[row][col] = False
visited[points[0][0]][points[0][1]] = True
dfs(points[0][0], points[0][1], 1)
print(answer)
๋์์ด ๋์ จ๋ค๋ฉด ์ข์์ ๋๋ฌ์ฃผ์ธ์๐
'๐๋ฌธ์ ํ์ด > ๐งฉSofteer' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] Softeer ์๋์ฐจ ํ ์คํธ - ์ ๋ ฌ (2) | 2024.08.31 |
---|---|
[Python] ์ํํฐ์ด Lv.2 ์ ๊ดํ (0) | 2024.06.27 |