This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-10-09] sumin #313 #327
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
풀이시간: 40분 | ||
|
||
<input> | ||
n: 격자의 크기(2 ≤ n ≤ 4) | ||
m: 순서대로 방문해야 하는 칸의 수(2 ≤ m ≤ n^2) | ||
|
||
n개의 수(0 or 1) | ||
- 0: 빈 칸 | ||
- 1: 벽 | ||
|
||
방문해야할 m개의 칸 (x, y)가 주어진다 | ||
- 주어진 칸에 벽이 있는 경우능 없고, 중복도 없다. | ||
|
||
<solution> | ||
n과 m이 매우 작기 때문에 DFS를 통해 모든 경우의 수를 확인한다.(브루트 포스) | ||
이 때, 더 이상 다음 칸으로 이동할 수 없다면 탐색을 포기한다.(백트래킹) | ||
|
||
<시간복잡도> | ||
O(N^2) | ||
""" | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
|
||
def dfs(cur_x: int, cur_y: int, nxt_idx: int): | ||
""" | ||
cur_x: 시작 칸의 x좌표 | ||
cur_y: 시작 칸의 y좌표 | ||
nxt_idx: 다음 칸의 인덱스 | ||
""" | ||
global cnt | ||
# 다음 칸에 도달한 경우 | ||
if cur_x == go[nxt_idx][0] and cur_y == go[nxt_idx][1]: | ||
# 1) 마지막 칸까지 도달했다면 경우의 수를 1 증가시키고 종료 | ||
if nxt_idx == m-1: | ||
cnt += 1 | ||
return | ||
# 2) 이동한 칸이 마지막 칸이 아니라면 방문해야할 칸의 idx 증가 | ||
else: | ||
nxt_idx += 1 | ||
|
||
# 현재 위치 방문처리 | ||
visited[cur_x][cur_y] = True | ||
|
||
for k in range(4): | ||
nx, ny = cur_x+dx[k], cur_y+dy[k] | ||
if (0 <= nx < n and 0 <= ny < n) and board[nx][ny] == 0 and not visited[nx][ny]: | ||
dfs(nx, ny, nxt_idx) | ||
|
||
visited[cur_x][cur_y] = False | ||
|
||
|
||
# n: 격자의 크기, m: 순서대로 방무해야 하는 칸의 수 | ||
n, m = map(int, input().split()) | ||
# 격자판(0: 빈 칸, 1: 벽) | ||
board = [list(map(int, input().split())) for _ in range(n)] | ||
# 방문처리 배열 | ||
visited = [[False] * n for _ in range(n)] | ||
|
||
# 방문해야할 칸의 좌표 | ||
go = [] | ||
for _ in range(m): | ||
x, y = map(int, input().split()) | ||
go.append((x-1, y-1)) | ||
|
||
# 상, 하, 좌, 우 방향으로 이동 | ||
dx = [1, -1, 0, 0] | ||
dy = [0, 0, 1, -1] | ||
|
||
# 경우의 수 | ||
cnt = 0 | ||
# 순차적으로 방문 | ||
dfs(go[0][0], go[0][1], 1) | ||
print(cnt) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
방문처리를 다음에 탐색할 영역이 아닌 현재 방문한 위치에 대해서 방문 처리 및 방문 처리 해제를 해주는 과정이 인상 깊네요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 사실 ny와 nx를 다룰 때 방문 처리를 하는게 익숙해서 잘 와닿지 않았던 것 같아요! 하지만 이렇게 훨씬 깔끔하게 구성할 수 있다는 것을 알았습니다!