Skip to content

Commit 7eea0a5

Browse files
authored
Merge pull request #241 from AlgorithmStudy-Allumbus/minjeong4
Minjeong / 7์›” 5์ฃผ์ฐจ / 2๋ฌธ์ œ
2 parents 7f34437 + 796dd87 commit 7eea0a5

6 files changed

+220
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def solution(places):
2+
def is_safe(place):
3+
for i in range(5):
4+
for j in range(5):
5+
if place[i][j] != 'P':
6+
continue
7+
8+
for dx, dy in dirs:
9+
ni, nj = i + dx, j + dy
10+
if not (0 <= ni < 5 and 0 <= nj < 5):
11+
continue
12+
if place[ni][nj] != 'P':
13+
continue
14+
15+
dist = abs(dx) + abs(dy)
16+
if dist == 1:
17+
return 0 # ๊ฑฐ๋ฆฌ 1์—์„œ ๋ฐ”๋กœ P๋ฉด ์œ„๋ฐ˜
18+
elif dist == 2:
19+
# ํŒŒํ‹ฐ์…˜ ์—ฌ๋ถ€ ํ™•์ธ
20+
if dx == 0: # ์ˆ˜ํ‰
21+
if place[i][j + dy // 2] != 'X':
22+
return 0
23+
elif dy == 0: # ์ˆ˜์ง
24+
if place[i + dx // 2][j] != 'X':
25+
return 0
26+
else: # ๋Œ€๊ฐ์„ 
27+
if place[i][nj] != 'X' or place[ni][j] != 'X':
28+
return 0
29+
return 1
30+
31+
dirs = [
32+
(-1, 0), (1, 0), (0, -1), (0, 1), # ๊ฑฐ๋ฆฌ 1
33+
(-2, 0), (2, 0), (0, -2), (0, 2), # ์ผ์ง์„  ๊ฑฐ๋ฆฌ 2
34+
(-1, -1), (-1, 1), (1, -1), (1, 1) # ๋Œ€๊ฐ์„  ๊ฑฐ๋ฆฌ 2
35+
]
36+
37+
answer = []
38+
for place in places:
39+
answer.append(is_safe(place))
40+
41+
return answer
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def solution(places):
2+
def is_safe(place):
3+
for i in range(5):
4+
for j in range(5):
5+
if place[i][j] != 'P':
6+
continue
7+
8+
for dx, dy in dirs:
9+
ni, nj = i + dx, j + dy
10+
if not (0 <= ni < 5 and 0 <= nj < 5):
11+
continue
12+
if place[ni][nj] != 'P':
13+
continue
14+
15+
dist = abs(dx) + abs(dy)
16+
if dist == 1:
17+
return 0 # ๊ฑฐ๋ฆฌ 1์—์„œ ๋ฐ”๋กœ P๋ฉด ์œ„๋ฐ˜
18+
elif dist == 2:
19+
# ํŒŒํ‹ฐ์…˜ ์—ฌ๋ถ€ ํ™•์ธ
20+
if dx == 0: # ์ˆ˜ํ‰
21+
if place[i][j + dy // 2] != 'X':
22+
return 0
23+
elif dy == 0: # ์ˆ˜์ง
24+
if place[i + dx // 2][j] != 'X':
25+
return 0
26+
else: # ๋Œ€๊ฐ์„ 
27+
if place[i][nj] != 'X' or place[ni][j] != 'X':
28+
return 0
29+
return 1
30+
31+
dirs = [
32+
(-1, 0), (1, 0), (0, -1), (0, 1), # ๊ฑฐ๋ฆฌ 1
33+
(-2, 0), (2, 0), (0, -2), (0, 2), # ์ผ์ง์„  ๊ฑฐ๋ฆฌ 2
34+
(-1, -1), (-1, 1), (1, -1), (1, 1) # ๋Œ€๊ฐ์„  ๊ฑฐ๋ฆฌ 2
35+
]
36+
37+
answer = []
38+
for place in places:
39+
answer.append(is_safe(place))
40+
41+
return answer
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
sys.setrecursionlimit(100000)
3+
4+
input = sys.stdin.readline
5+
6+
# ๋ฐฉํ–ฅ๋ฒกํ„ฐ
7+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
8+
9+
def dfs(x, y):
10+
# ๊นŠ์ด ํƒ์ƒ‰์„ ํ•˜๋ฉฐ ์•ˆ์ „ ์˜์—ญ ํƒ์ƒ‰
11+
visited[x][y] = True
12+
for dx, dy in directions:
13+
nx, ny = x + dx, y + dy
14+
if 0 <= nx < N and 0 <= ny < N and \
15+
not visited[nx][ny] and map[nx][ny] > h:
16+
dfs(nx, ny)
17+
18+
19+
N = int(input())
20+
map = [list(map(int, input().split())) for _ in range(N)]
21+
max_height = 0
22+
23+
# ๋งต ๋‚ด์˜ ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๊ธฐ
24+
for m in map:
25+
max_height = max(max_height, max(m))
26+
27+
answer = 0
28+
29+
for h in range(0, max_height + 1): # ๋ฌผ์ด ์ž ๊ธฐ์ง€ ์•Š๋Š” ์ƒํ™ฉ์„ ๊ณ ๋ คํ•˜์—ฌ 0๋ถ€ํ„ฐ ์‹œ์ž‘
30+
visited = [[False for _ in range(N)] for _ in range(N)]
31+
count = 0
32+
33+
for i in range(N):
34+
for j in range(N):
35+
# ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ณ  h(๋†’์ด) ์ด์ƒ์ธ ์ง€์ ์ด๋ผ๋ฉด DFS ํƒ์ƒ‰์„ ํ•œ๋‹ค.
36+
if not visited[i][j] and map[i][j] > h:
37+
dfs(i, j)
38+
count += 1 # DFS ํƒ์ƒ‰์„ ๋งˆ์น˜๋ฉด ํ•˜๋‚˜์˜ ์•ˆ์ „ ์˜์—ญ์ด๋ฏ€๋กœ count
39+
40+
answer = max(answer, count) # ์•ˆ์ „์˜์—ญ ์ตœ๋Œ€ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ
41+
42+
print(answer)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
def dfs(start, cnt):
5+
global chonsu
6+
if start == y:
7+
chonsu = cnt
8+
return
9+
10+
for i in graph[start]:
11+
if not visited[i]:
12+
visited[i] = True
13+
dfs(i, cnt + 1)
14+
visited[i] = False
15+
16+
17+
n = int(input()) # ์ „์ฒด ์‚ฌ๋žŒ ๊ฐœ์ˆ˜
18+
x, y = map(int, input().split())
19+
graph = [[] for _ in range(n+1)]
20+
visited = [False for _ in range(n + 1)]
21+
chonsu = -1 # ์ •๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ์ดŒ์ˆ˜
22+
23+
m = int(input()) # ๋ถ€๋ชจ ์ž์‹๋“ค๊ฐ„์˜ ๊ด€๊ณ„ ๊ฐœ์ˆ˜
24+
for _ in range(m):
25+
a, b = map(int, input().split())
26+
graph[a].append(b)
27+
graph[b].append(a)
28+
29+
visited[x] = True
30+
dfs(x, 0)
31+
print(chonsu)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
# ์ด ์ธต์ˆ˜ F, ํ˜„์žฌ ์ธต S, ๋ชฉํ‘œ ์ธต G, ์œ„ ๋ฒ„ํŠผ U, ์•„๋ž˜ ๋ฒ„ํŠผ D
7+
F, S, G, U, D = map(int, input().split())
8+
9+
10+
def bfs():
11+
if S == G: # ๊ฐ•ํ˜ธ๊ฐ€ ์ด๋ฏธ ๋„์ฐฉํ•ด์žˆ๋‹ค๋ฉด 0๋ฒˆ ํด๋ฆญ
12+
return 0
13+
14+
visited = [False for _ in range(F + 1)]
15+
queue = deque([(S, 1)]) # (ํ˜„์žฌ ์ธต, ๋ฒ„ํŠผ ํด๋ฆญ ํšŸ์ˆ˜)
16+
visited[S] = True
17+
18+
while queue:
19+
x, cnt = queue.popleft()
20+
21+
# ์œ„๋กœ ์ด๋™
22+
nx = x + U
23+
if nx <= F and not visited[nx]:
24+
if nx == G: # ๋ชฉํ‘œ์ธต ๋„์ฐฉ
25+
return cnt
26+
queue.append((nx, cnt + 1))
27+
visited[nx] = True # ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ํ‘œ์‹œ ๊ผญ ๋„ฃ์–ด์ฃผ๊ธฐ
28+
29+
# ์•„๋ž˜๋กœ ์ด๋™
30+
nx = x - D
31+
if nx > 0 and not visited[nx]:
32+
if nx == G: # ๋ชฉํ‘œ์ธต ๋„์ฐฉ
33+
return cnt
34+
queue.append((nx, cnt + 1))
35+
visited[nx] = True
36+
37+
# ๋ชฉํ‘œ์ธต์— ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ
38+
return -1
39+
40+
41+
# BFS ํƒ์ƒ‰
42+
answer = bfs()
43+
if answer == -1:
44+
print("use the stairs")
45+
else:
46+
print(answer)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
N, K = map(int, input().split())
6+
# 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ์‚ฌ๋žŒ์„ ํ์— ๋„ฃ๊ธฐ
7+
people = deque([i+1 for i in range(N)])
8+
answer = [] # ์ œ๊ฑฐ๋œ ์ˆœ์„œ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
9+
10+
while people:
11+
# K-1๋ฒˆ ์•ž์˜ ์›์†Œ๋ฅผ ๋นผ์„œ ๋’ค๋กœ ๋ณด๋ƒ„ (์›ํ˜• ์ด๋™)
12+
for _ in range(K-1):
13+
cur = people.popleft()
14+
people.append(cur)
15+
# K๋ฒˆ์งธ์—์„œ ์ œ๊ฑฐ
16+
answer.append(people.popleft())
17+
18+
# ์ •๋‹ต ์ถœ๋ ฅ
19+
print("<" + ", ".join(map(str, answer)) + ">")

0 commit comments

Comments
ย (0)