forked from satnaing/astro-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Affected files: .obsidian/workspace.json src/content/blog/boj-2564-경비원.md
- Loading branch information
Showing
2 changed files
with
141 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -4,16 +4,16 @@ | |
"type": "split", | ||
"children": [ | ||
{ | ||
"id": "c93cbacdb763ad00", | ||
"id": "2676f6981de71d52", | ||
"type": "tabs", | ||
"children": [ | ||
{ | ||
"id": "4077864ed2ae2ddd", | ||
"id": "a33d3c979b188989", | ||
"type": "leaf", | ||
"state": { | ||
"type": "markdown", | ||
"state": { | ||
"file": "src/content/blog/boj-2529-부등호.md", | ||
"file": "src/content/blog/boj-15661-링크와-스타트.md", | ||
"mode": "source", | ||
"source": false | ||
} | ||
|
@@ -22,16 +22,16 @@ | |
] | ||
}, | ||
{ | ||
"id": "2676f6981de71d52", | ||
"id": "5618ed33c29b957d", | ||
"type": "tabs", | ||
"children": [ | ||
{ | ||
"id": "a33d3c979b188989", | ||
"id": "72d69f1c38fd96fe", | ||
"type": "leaf", | ||
"state": { | ||
"type": "markdown", | ||
"state": { | ||
"file": "src/content/blog/boj-15661-링크와-스타트.md", | ||
"file": "src/content/blog/boj-2564-경비원.md", | ||
"mode": "source", | ||
"source": false | ||
} | ||
|
@@ -103,7 +103,7 @@ | |
"state": { | ||
"type": "backlink", | ||
"state": { | ||
"file": "src/content/blog/boj-15661-링크와-스타트.md", | ||
"file": "src/content/blog/boj-2564-경비원.md", | ||
"collapseAll": false, | ||
"extraContext": false, | ||
"sortOrder": "alphabetical", | ||
|
@@ -120,7 +120,7 @@ | |
"state": { | ||
"type": "outgoing-link", | ||
"state": { | ||
"file": "src/content/blog/boj-15661-링크와-스타트.md", | ||
"file": "src/content/blog/boj-2564-경비원.md", | ||
"linksCollapsed": false, | ||
"unlinkedCollapsed": true | ||
} | ||
|
@@ -143,7 +143,7 @@ | |
"state": { | ||
"type": "outline", | ||
"state": { | ||
"file": "src/content/blog/boj-15661-링크와-스타트.md" | ||
"file": "src/content/blog/boj-2564-경비원.md" | ||
} | ||
} | ||
} | ||
|
@@ -166,10 +166,11 @@ | |
"table-editor-obsidian:Advanced Tables Toolbar": false | ||
} | ||
}, | ||
"active": "a33d3c979b188989", | ||
"active": "72d69f1c38fd96fe", | ||
"lastOpenFiles": [ | ||
"src/content/blog/boj-2529-부등호.md", | ||
"src/content/blog/boj-2564-경비원.md", | ||
"src/content/blog/boj-15661-링크와-스타트.md", | ||
"src/content/blog/boj-2529-부등호.md", | ||
"src/content/blog/leet-code-919-complete-binary tree-inserter.md", | ||
"src/content/blog/leet-code-207-course-schedule.md", | ||
"src/content/blog/leet-code-74-search-a-2d-matrix.md", | ||
|
@@ -195,7 +196,6 @@ | |
"src/content/blog/install-neovim-using-aqua-on-ubuntu.md", | ||
"src/content/blog/how-to-connect-wifi-on-ubuntu-server.md", | ||
"dist/posts/aqua를-이용해-wsl-ubuntu에-neovim을-설치하자.png", | ||
"src/content/blog/manage-cli-program-version-using-aqua-proj-on-windows.md", | ||
"dist/assets/[email protected]", | ||
"dist/assets/forrest-gump-quote.webp.jpg", | ||
"dist/_astro/[email protected]", | ||
|
This file contains 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,129 @@ | ||
--- | ||
author: Gyunseo Lee | ||
title: "BOJ 백준 2564: 경비원" | ||
pubDatetime: 2024-07-19T01:53:00+09:00 | ||
modDatetime: 2024-07-19T01:53:00+09:00 | ||
featured: false | ||
draft: false | ||
tags: | ||
- PS | ||
- BOJ | ||
- Algorithms | ||
- 실랜디 | ||
- 골랜디 | ||
- BFS | ||
description: BFS 템플릿은 자다 깨도 5분만에 칠 수 있게 외우자 | ||
ogImage: "" | ||
--- | ||
|
||
## Table of contents | ||
|
||
## 들어가며 | ||
|
||
걸린 시간: 27분 | ||
|
||
문제를 읽고, 경우의 수를 나눠서 좌표 간의 거리를 계산하면 되지 않을까 싶었습니다. | ||
그런데 웬걸 좌표의 범위, 타겟 좌표 개수의 범위 모두 100이하여서 이건 그냥 BFS로 돌리면 되겠다 싶어서, BFS로 돌렸습니다. | ||
|
||
## 접근 | ||
|
||
![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1721321745/image_kdvjz5.png) | ||
저 박스에 해당하는 board를 생성해서 그 안에서 BFS를 돌리게 했습니다. | ||
시간 복잡도는 O(상점 개수 \* 박스에 해당하는 보드의 크기) 최대값 넣고 계산해도 백만정도... | ||
|
||
## 구현 | ||
|
||
```python | ||
import sys | ||
from collections import deque | ||
|
||
|
||
input = sys.stdin.readline | ||
|
||
def processPos(direction, pos): | ||
ret_i, ret_j = -1, -1 | ||
# i = 0 | ||
if direction == 1: | ||
ret_i, ret_j = (0, pos) | ||
# i = height | ||
elif direction == 2: | ||
ret_i, ret_j = (height, pos) | ||
# j = 0 | ||
elif direction == 3: | ||
ret_i, ret_j = (pos, 0) | ||
# j = width | ||
elif direction == 4: | ||
ret_i, ret_j = (pos, width) | ||
|
||
return ret_i, ret_j | ||
|
||
def OOB(i, j): | ||
if i < 0 or i > height: | ||
return True | ||
if j < 0 or j > width: | ||
return True | ||
return False | ||
|
||
def BFS(target_pos): | ||
distance = [[0 for __ in range(width + 1)] for _ in range(height + 1)] | ||
q = deque() | ||
q.append(startPos) | ||
distance[startPos[0]][startPos[1]] = 1 | ||
|
||
while q: | ||
cur_i, cur_j = q.popleft() | ||
|
||
for delta_i, delta_j in zip(deltaI, deltaJ): | ||
next_i, next_j = cur_i + delta_i, cur_j + delta_j | ||
if OOB(next_i, next_j): | ||
continue | ||
# 벽으로 막혀 있다면 | ||
if board[next_i][next_j] == 1: | ||
continue | ||
# 이미 방문한 곳이라면 | ||
if distance[next_i][next_j] > 0: | ||
continue | ||
|
||
distance[next_i][next_j] = distance[cur_i][cur_j] + 1 | ||
q.append((next_i, next_j)) | ||
|
||
return distance[target_pos[0]][target_pos[1]] | ||
|
||
if __name__ == "__main__": | ||
|
||
width, height = map(int, input().strip().split()) | ||
numTargets = int(input().strip()) | ||
targetPosList = [] | ||
deltaI = (-1, 1, 0, 0) | ||
deltaJ = (0, 0, -1, 1) | ||
for _ in range(numTargets): | ||
direction, pos = map(int, input().strip().split()) | ||
|
||
targetPosList.append(processPos(direction, pos)) | ||
|
||
direction, pos = map(int, input().strip().split()) | ||
startPos = processPos(direction, pos) | ||
|
||
board = [[0 for __ in range(width + 1)] for _ in range(height + 1)] | ||
ans = 0 | ||
# fill wall inner board | ||
for i in range(height + 1): | ||
for j in range(width + 1): | ||
if i == 0 or i == height: | ||
continue | ||
if j == 0 or j == width: | ||
continue | ||
board[i][j] = 1 | ||
|
||
# print(startPos) | ||
# print(targetPosList) | ||
|
||
for target_pos in targetPosList: | ||
min_dist = BFS(target_pos) | ||
# print(min_dist) | ||
ans += min_dist - 1 | ||
print(ans) | ||
|
||
``` | ||
|
||
너무 전형적인 BFS 문제였습니다. |