Skip to content

Commit 79fa57a

Browse files
authored
Merge pull request #215 from AlgorithmStudy-Allumbus/hongjoo
HONGJOO/ 5์›” 4์ฃผ/ 4๊ฐœ
2 parents 237e364 + 6a25f66 commit 79fa57a

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
answer = 0
2+
def solution(n):
3+
4+
def backtracking(path,lv):
5+
# ์žฌ๊ท€ ์ข…๋ฃŒ
6+
global answer
7+
if lv >= len(path):
8+
if sum(path)==n :
9+
answer += 1
10+
# print(f"{lv} : {path} >{answer}")
11+
return 0
12+
#์ž์‹ ๋…ธ๋“œ ์ด๋™
13+
for x in [1,2] :
14+
if sum(path) + x <= n :
15+
path[lv] = x
16+
backtracking(path , lv+1)
17+
# backtracking
18+
path[lv] = 0
19+
20+
for m in range(1,n+1) :
21+
backtracking([0]*m , 0)
22+
return answer%1234567
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
20.19
3+
https://www.acmicpc.net/problem/11559
4+
BOJ.#11559_Gold4
5+
6+
#problem
7+
- goal) ์—ฐ์‡„ "์—ฐ์†" ํšŸ์ˆ˜ ๊ตฌํ•˜๊ธฐ
8+
[condition]
9+
- 1์—ฐ์‡„ : ํ•ด๋‹น turn ์—์„œ ์ƒํ•˜์ขŒ์šฐ 4๊ฐœ ์—ฐ๊ฒฐ -> ์‚ญ์ œ
10+
- ์‚ญ์ œ ํ›„ ์œ„์— ์žˆ๋Š” element ๋Š” ํ•˜๊ฐ•
11+
12+
- ์ž…๋ ฅ: ํ˜„ filed ์ƒํ™ฉ(12x6)
13+
- (๋นˆ๊ณต๊ฐ„) / R, G, B, P, Y
14+
- ๋นˆ๊ณต๊ฐ„ : 0
15+
- ์ƒ‰์ƒ : R,G, B, P, Y = 1,2,3,4,5
16+
[flow] # BFS
17+
1. ์ƒํ•˜์ขŒ์šฐ ์—ฐ์‡„ ํ™•์ธ
18+
- ์—ฐ์‡„ ํ™•์ธ
19+
- ์‚ญ์ œ
20+
- ์—ฐ์‡„ ํšŸ์ˆ˜ ์ฆ๊ฐ€
21+
22+
"""
23+
import sys
24+
from collections import deque
25+
input = sys.stdin.readline
26+
#1. field ํ˜„ํ™ฉ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ธฐ
27+
field = [list(input())[:-1] for _ in range(12)]
28+
29+
#2. bfs
30+
dy = [-1,1,0,0]
31+
dx =[0,0,-1,1]
32+
33+
34+
def refine_field(x) : # ์ค‘๊ฐ„ ๋นˆ์ž๋ฆฌ
35+
stack = deque()
36+
#1.์•„๋ ˆ=> ์œ„๋กœ ์Šคํƒ์— ๋ฟŒ์š”๋ฟŒ์š” ์ˆœ์„œ๋Œ€๋กœ ์ถ•์ฒ™ํ•˜๊ธฐ
37+
for ny in range(11, -1,-1):
38+
if field[ny][x] != ".":
39+
stack.append(field[ny][x])
40+
for ny in range(11, -1, -1) :
41+
if stack :
42+
field[ny][x] = stack.popleft()
43+
else :
44+
field[ny][x] = "."
45+
46+
# ์—ฐ์‡„ ํ™•์ธ ๋ฐ ํ„ฐ์ง
47+
def bfs(sy,sx):
48+
q = deque()
49+
q.append([sy,sx])
50+
pop_positions = [[sy,sx]]
51+
cur_color = field[sy][sx]
52+
visited.append([sy,sx])
53+
while q :
54+
cy,cx = q.popleft()
55+
for d in range(4):
56+
ny ,nx = cy + dy[d] ,cx + dx[d]
57+
# ๊ฐ™์€ ์ƒ‰์ƒ -> ์‚ญ์ œ ๋“ฑ๋กํ•˜๊ธฐ
58+
if 0<= ny<12 and 0<= nx < 6 and [ny,nx] not in visited and field[ny][nx] == cur_color :
59+
q.append([ny,nx])
60+
pop_positions.append([ny,nx])
61+
visited.append([ny,nx])
62+
63+
#2) ํ„ฐ์ง ํ™•์ธ
64+
if len(pop_positions) >= 4 :
65+
for y,x in pop_positions :
66+
field[y][x] = "."
67+
return True
68+
return False# ์•ˆ ํ„ฐ์ง
69+
70+
answer = 0
71+
flag = True
72+
k = 0
73+
while flag :
74+
visited = []
75+
flag =False
76+
#1. ์ „์ฒด field์—์„œ ๋ฟŒ์š”๋ฟŒ์š” ํƒ์ƒ‰
77+
for i in range(12):
78+
for j in range(6):
79+
if field[i][j]!="." and [i,j] not in visited:
80+
now_f = bfs(i,j) # 2.ํ•ด๋‹น ์œ„์น˜์—์„œ ํ„ฐ์ง ์—ฌ๋ถ€ ํ™•์ธ
81+
flag = flag or now_f
82+
#2. ์—ฐ์‡„ ๊ณ„์ˆ˜ ์ถ”๊ฐ€
83+
if not flag : # False - ์•ˆํ„ฐ์ง
84+
break
85+
else :
86+
for row in range(6):
87+
refine_field(row)
88+
# ํ˜„์žฌ turn ์—์„œ 1๋ฒˆ ์ด์ƒ ํ„ฐ์ง
89+
answer += 1
90+
print(answer)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
import sys
3+
from collections import deque
4+
INF = 1e9
5+
# ์ƒํ•˜์ขŒ์šฐ
6+
dy = [-1,1,0,0]
7+
dx = [0,0,-1,1]
8+
9+
dl= [[-1,1],[1,1],[1,-1],[-1,-1]] # ์šฐ์ƒ . ์šฐํ•˜ / ์ขŒํ•˜ / ์ขŒ์ƒ
10+
dl_combined = [0,3,1,2,2,3,0,1]
11+
sy ,sx= map(int,sys.stdin.readline().split())
12+
ty,tx = map(int, sys.stdin.readline().split())
13+
14+
field = [[INF]*9 for _ in range(10)]
15+
field[sy][sx] = 0
16+
field[ty][tx] = -1
17+
18+
q = deque()
19+
q.append([sy,sx])
20+
21+
def check_bound(y,x) :
22+
if 0<= y <=9 and 0<= x <=8 and field[y][x]!=-1 : # ๊ธฐ๋ฌผ x , field ์•ˆ์— , ๋ฐฉ๋ฌธ ์ƒ๊ด€x => ์™• ์œ„์น˜๋งŒ ์•ˆ๋จx
23+
return True
24+
return False
25+
def check_bound2(y,x) :
26+
if 0<= y <=9 and 0<= x <=8 : # ๊ธฐ๋ฌผ ๊ฐ€๋Šฅ
27+
return True
28+
return False
29+
30+
while q :
31+
cy,cx = q.popleft()
32+
for i in range(8):
33+
ny1, nx1 = cy +dy[i//2] , cx+dx[i//2]
34+
ny2, nx2 = ny1 + dl[dl_combined[i]][0] , nx1 + dl[dl_combined[i]][1]
35+
ny3 , nx3 = ny2 + dl[dl_combined[i]][0] , nx2 + dl[dl_combined[i]][1]
36+
37+
if check_bound(ny1, nx1) and check_bound(ny2, nx2) and check_bound2(ny3, nx3) :
38+
if ny3 == ty and nx3 == tx :
39+
# print(f"succes====")
40+
print(field[cy][cx] +1)
41+
42+
exit()
43+
elif field[cy][cx] +1 <= field[ny3][nx3] :
44+
field[ny3][nx3] =field[cy][cx] +1 # ์—…๋ฐ์ดํŠธ
45+
# print(f"{cy}{cx} -> {i}: {ny1}{nx1} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")
46+
47+
q.append([ny3,nx3])
48+
# print(f"{cy}{cx} -> {i}: {ny1}{nx2} / {ny2}{nx2} / {ny3}, {nx3} => field{field[ny3][nx3]} ")
49+
50+
print(field[ty][tx])
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
https://www.acmicpc.net/problem/7576
3+
# BFS, DFS
4+
(1) filed ๊ฐ’ ์ค‘ 1์ธ ๊ฐ’ ์ฐพ๊ธฐ
5+
(2) ์ƒํ•˜์ขŒ์šฐ๋กœ -1 ๋นผ๊ณ  ํ™•์‚ฐ
6+
(3) flag๋กœ ํ•ด๋‹น ์ด์ž” turn์—์„œ ํ™•์‚ฐ ์œ ๋ฌด๋ฅผ ํŒ๋‹จ -> ์•ˆ๋˜๋ฉด turn ์ข…๋ฃŒ
7+
8+
"""
9+
# ์ž…๋ ฅ
10+
import sys
11+
from collections import deque
12+
13+
input = sys.stdin.readline
14+
N,M = map(int, input().split())
15+
16+
field = [list(map(int,input().split())) for _ in range(M) ]
17+
# print(field)
18+
dy = [-1,1,0,0]
19+
dx = [-0,0,-1,1]
20+
# BFS
21+
22+
23+
lv = 0
24+
burn = []
25+
q = deque()
26+
for m in range(M) :
27+
for n in range(N) :
28+
# print(m , n)
29+
if field[m][n] == 1:
30+
# ์•ˆ ์ต์€ ํ† ๋งˆํ†  ์ตํžˆ๊ธฐ
31+
q.append([m,n])
32+
33+
while q :
34+
# ๊ฐ turn ๋งˆ๋‹ค ํ† ๋งˆ๋„ ์ „์—ผ์‹œํ‚ค๊ธฐ
35+
36+
# print(f"{t}ํ„ด - {m}{n}")
37+
#[2] ์ต์€ ํ† ๋งˆํ†  ํ™•์‚ฐ์‹œํ‚ค๊ธฐ
38+
# t+= 1
39+
y,x = q.popleft()
40+
for d in range(4):
41+
ny = y + dy[d] ; nx = x + dx[d]
42+
if 0<= ny <M and 0 <= nx < N and field[ny][nx] == 0:
43+
field[ny][nx] = field[y][x] +1
44+
q.append([ny,nx])
45+
46+
max_num = 0
47+
for i in range(M) :
48+
for j in range(N):
49+
if field[i][j]== 0 :
50+
print(-1)
51+
exit()
52+
max_num = max(max_num , field[i][j])
53+
print(max_num-1)
54+
55+
# burn.append([n,m])
56+
# q = deque()
57+
# for sy,sx in burn :
58+
# q.append([sy,sx])
59+
# while q :
60+

0 commit comments

Comments
ย (0)