โ 7576 ํ ๋งํ โ
import sys
input=sys.stdin.readline
from collections import deque
M,N=map(int,input().split())
graph=[]
starts=[]
depths=[[0]*M for _ in range(N)]
visited=[[False]*M for _ in range(N)]
def BFS(graph, depths, starts, visited):
queue = deque(starts)
dx,dy=[0,0,-1,1],[-1,1,0,0]
while queue:
v = queue.popleft()
for i in range(4):
nx,ny=v[0]+dx[i],v[1]+dy[i]
if 0<=nx<N and 0<=ny<M:
if not visited[nx][ny] and graph[nx][ny] == 0:
graph[nx][ny] = 1
visited[nx][ny] = True
depths[nx][ny] = depths[v[0]][v[1]] + 1
queue.append((nx,ny))
for x1 in range(N):
l=list(map(int,input().split()))
graph.append(l)
x2=0
for y in l:
if y == 1:
starts.append((x1,x2))
visited[x1][x2] = True
x2+=1
BFS(graph, depths, starts, visited)
for x in range(N):
for y in range(M):
if graph[x][y] == 0:
print(-1)
sys.exit()
max_depth=-10_000_000
for x in range(N):
max_depth=max(max_depth,max(depths[x]))
print(max_depth)
โด๏ธ ์์ ๋ด์ ์ต์ ํ ๋งํ ๋ค์ด ๋์์ ์ํ์ข์ฐ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋ฏ๋ก ์ค๋ฒ ์ต๋จ๊ฑฐ๋ฆฌ ๊ธฐ์กด ๋ฌธ์ (์์์ 1๊ฐ)์ ๋์ด๋ ์ฝ๊ฐ ์์น. ์์์ ์ด ์ฌ๋ฌ๊ฐ์ธ BFS ์ต๋จ๊ฑฐ๋ฆฌ ์ ํ: 1์ผ ๋์ (x,y)๋ฅผ starts list์ ๋ฃ์ด queue์ ๋ฃ๊ณ ์์
โด๏ธ ๊ทธ ์ธ) 0์ด ์๋ค๋ฉด -1 ์ถ๋ ฅํ๊ณ ๋ฐ๋ก ์ข ๋ฃ / ๊ทธ๊ฒ ์๋๋ผ๋ฉด max ๊ฐ 2์ฐจ์ ๋ฐฐ์ด ๋๋ ค์ ํ์ธ
โ 7569 ํ ๋งํ โ
import sys
input=sys.stdin.readline
from collections import deque
def BFS(graph, visited, depths, starts):
queue = deque(starts)
dx,dy,dz=[0,0,0,0,1,-1],[0,0,1,-1,0,0],[1,-1,0,0,0,0]
while queue:
v = queue.popleft()
x,y,z=v[0],v[1],v[2]
visited[x][y][z] = True
for i in range(6):
nx,ny,nz=x+dx[i],y+dy[i],z+dz[i]
if 0<=nx<H and 0<=ny<N and 0<=nz<M:
if visited[nx][ny][nz] == False and graph[nx][ny][nz] == 0:
queue.append((nx,ny,nz))
depths[nx][ny][nz] = depths[x][y][z] + 1
graph[nx][ny][nz] = 1 #riped
M,N,H=map(int,input().split())
graph=[[] for _ in range(H)]
starts=[]
for i in range(H):
for j in range(N):
l = list(map(int,input().split()))
graph[i].append(l)
for k in range(len(l)):
if l[k] == 1:
starts.append((i,j,k))
visited=[[[False]*M for _ in range(N)] for _ in range(H)]
depths=[[[0]*M for _ in range(N)] for _ in range(H)]
BFS(graph, visited, depths, starts)
for h in range(H):
for x in range(N):
for y in range(M):
if graph[h][x][y] == 0:
print(-1) #not all has ripened
sys.exit()
max_depth=-2
for h in range(H):
for x in range(N):
max_depth=max(max_depth,max(depths[h][x]))
print(max_depth)
โด๏ธ ๋์ด ๋ถ๋ถ ์/ํ ๋ฐฉํฅ์ผ๋ก๋ ์์์ผ ํ๋ค๋ ์ ๋ง ์ฃผ์! ๋ฐฉํฅ ๋ฒกํฐ dx, dy, dz = [0,0,0,0,-1,1], [0,0,-1,1,0,0], [-1,1,0,0,0,0] ์ด๋ ๊ฒ ์ด 6๊ฐ์ง์ ๋ฐฉํฅ๋ฒกํฐ ์กด์ฌ
โด๏ธ ์์์ ๋์์ ์งํ / depths BFS ์ต๋๊ฐ / ๊ทธ ์ธ ๋ฌธ์ ์ ๋ง๊ฒ ์งํ (์ฆ ๋๋จธ์ง๋ ์์ 7569 ํ ๋งํ ๋ฌธ์ ํ์ด์ ๋์ผ)
โ 14502 ์ฐ๊ตฌ์ โ
import sys, copy
input=sys.stdin.readline
from collections import deque
from itertools import combinations
N,M=map(int,input().split())
graph_init=[]
virus=[]
pos=[]
def get_zero(graph, N, M, pos):
for i in range(N):
for j in range(M):
if graph[i][j] == 0:
pos.append((i,j))
return pos
def BFS(graph, virus, N, M, visited):
queue=deque(virus)
for v in virus:
visited[v[0]][v[1]] = True
dx,dy=[1,-1,0,0],[0,0,1,-1]
while queue:
v=queue.popleft()
for i in range(4):
nx,ny=v[0]+dx[i],v[1]+dy[i]
if 0<=nx<N and 0<=ny<M:
if not visited[nx][ny] and graph[nx][ny]==0:
visited[nx][ny]=True
graph[nx][ny] = 2
queue.append((nx,ny))
for x in range(N):
l,y=list(map(int,input().split())),0
graph_init.append(l)
for i in l:
if i == 2:
virus.append((x,y))
y+=1
pos=get_zero(graph_init,N,M,pos) #to find the potential spots to install a wall
ans=0
for pos_pair in combinations(pos,3):
graph=copy.deepcopy(graph_init)
for (x,y) in pos_pair:
graph[x][y] = 1 #make a wall
visited=[[False]*M for _ in range(N)]
BFS(graph, virus, N, M, visited) #make virus spread
areas=0
for i in range(N):
for j in range(M):
if graph[i][j] == 0:
areas+=1
ans=max(ans,areas)
print(ans)
โด๏ธ
(1) ๋ฒฝ์ ์ธ์ธ ์ ์๋ 0 ์์น pos list์ ๋ชจ์ผ๊ธฐ
(2) virus ์์น ๋ชจ์ผ๊ธฐ
(3) combinations(pos,3) ๋๋ฆด ๋๋ง๋ค brute-force๋ก ๋งค case ์์ ์์ญ์ ํฌ๊ธฐ ๊ตฌํด์ผ ํ๋ฏ๋ก deepcopy() ๊ณ์ ์ฌ์ฉ → ์ธ ๊ตฐ๋ฐ ๋ฒฝ ์ธ์ฐ๊ณ (1๋ก ๋ง๋ค๊ธฐ) → ์ฌ๋ฌ virus ๋์์ ํผ์ง๊ฒ BFS ๋๋ฆฌ๊ธฐ → 0์ธ ๊ฐ์ areas๋ก ๊ตฌํด์ ans update
โ 10026 ์ ๋ก์์ฝ โ
import sys, copy
input=sys.stdin.readline
from collections import deque
N=int(input())
arr=[]
ans1,ans2=0,0
def BFS1(arr,visited,N,color,start):
queue=deque([start])
dx,dy=[0,0,1,-1],[1,-1,0,0]
while queue:
v=queue.popleft()
for i in range(4):
nx,ny=v[0]+dx[i],v[1]+dy[i]
if 0<=nx<N and 0<=ny<N:
if visited[nx][ny] == False and arr[nx][ny] == color:
visited[nx][ny]=True
queue.append((nx,ny))
def BFS2(arr,visited,N,color,start):
queue=deque([start])
dx,dy=[0,0,1,-1],[1,-1,0,0]
while queue:
v=queue.popleft()
for i in range(4):
nx,ny=v[0]+dx[i],v[1]+dy[i]
if 0<=nx<N and 0<=ny<N:
if visited[nx][ny] == False:
if color=='B':
if arr[nx][ny] == 'B':
visited[nx][ny]=True
queue.append((nx,ny))
else: #'R''G'
if arr[nx][ny] in ['R','G']:
visited[nx][ny]=True
queue.append((nx,ny))
for _ in range(N):
arr.append(list(input().rstrip()))
visited1=[[False]*N for _ in range(N)]
visited2=[[False]*N for _ in range(N)]
for i in range(N):
for j in range(N):
color=arr[i][j]
if not visited1[i][j]:
visited1[i][j] = True
BFS1(arr,visited1,N,color,(i,j))
ans1+=1
if not visited2[i][j]:
visited2[i][j] = True
BFS2(arr,visited2,N,color,(i,j))
ans2+=1
print(ans1,ans2)
โด๏ธ ์ ๋ก์์ฝ์ด ์๋ ๊ฒฝ์ฐ ๋จ์ํ ์ฐ๊ฒฐ์์ญ์ ๊ฐ์ + ์ ๋ก์์ฝ์ผ ๊ฒฝ์ฐ R/G/B if๋ฌธ ๊ตฌ๋ถ๋ง ํด์ฃผ๋ฉด ๋จ
โ 1240 ๋ ธ๋ ์ฌ์ด์ ๊ฑฐ๋ฆฌ โ
import sys
input=sys.stdin.readline
from collections import deque
def BFS(tree, visited, x, y, d):
visited[x]=True
queue=deque([(x,0)])
while queue:
v,d=queue.popleft()
if v == y:
return d
for sub_v in tree[v]:
if not visited[sub_v[0]]:
visited[sub_v[0]]=True
queue.append((sub_v[0],sub_v[1]+d))
N,M=map(int,input().split())
tree=[[] for _ in range(N+1)]
for _ in range(N-1):
a,b,d=map(int,input().split())
tree[a].append((b,d))
tree[b].append((a,d))
for _ in range(M):
x,y=map(int,input().split())
visited=[False]*(N+1)
print(BFS(tree,visited,x,y,0))
โด๏ธ BFS์ depth ๊ตฌํ๋ ๋ฐฉ์๊ณผ ๋น์ทํ๊ฒ BFS์ ์ฃผ์ด์ง ๋ ๋ ธ๋ ์ฌ์ด์ ๊ฑฐ๋ฆฌ๋ ๊ณ์ updateํ๋ฉด์ ํ ์ ์๋ค.
โด๏ธ ์ฃผ์ด์ง node์ (x,0) tuple์ queue๋ก ๋๋ฆฌ๊ณ ((node ๋ฒํธ, ์ฃผ์ด์ง node์์ ํด๋น node ๋ฒํธ๊น์ง์ ๊ฑฐ๋ฆฌ)๊ฐ tuple ํํ), ์ํ๋ ๋ ธ๋์ ๋๋ฌํ๋ค๋ฉด(v==y) ๋ฐ๋ก d ์ถ๋ ฅ
โป BFS ์ฌ์ฉ ํ์! BFS๋ฉด ๋์์ ์ฌ๋ฌ ๋ฐฉํฅ์ผ๋ก ๋ป์น๊ธฐ ๋๋ฌธ์ ๊ฐ node์ ์๋ node๋ก๋ถํฐ์ ์ต๋จ๊ฑฐ๋ฆฌ(์ฆ, ๊ฐ์ ๊ฐ์ค์น์ ํฉ)์ด ์ ์ฅ๋๋ฏ๋ก, queue๋ก๋ง ์ด๋์ํจ๋ค๋ฉด ์ต์ข node์ ๊ฐ์ ๋์ tuple ๋๋ฒ์งธ ์์ ๋ฐ๋ก ์ถ๋ ฅํ๋ฉด ๋! (์ต๋จ๊ฑฐ๋ฆฌ ๋ณํ๋ฌธ์ )
โด๏ธ <์ต๋จ๊ฑฐ๋ฆฌ์ ๋ค๋ฅธ ์ > queue์ ๋์๊ฐ๋ ์ ๋ณด๊ฐ ๋ ๊ฐ + BFS์ด๋ฏ๋ก ์์ node์์ ์์๋๋ ๊ฑฐ๋ฆฌ ๋์ (d)
โ 13913 ์จ๋ฐ๊ผญ์ง 4 โ
import sys
input=sys.stdin.readline
from collections import deque
N,K=map(int,input().split())
def BFS(visited, depths, start):
visited[start] = True
depths[start] = 0
path[start] = 0
queue = deque([start])
while queue:
next_node= queue.popleft()
for child in [next_node-1, next_node+1, next_node*2]:
if child == K:
visited[child] = True
print(depths[next_node]+1)
path[child] = next_node
return
elif 0 <= child <= 100_000 and not visited[child]:
visited[child] = True
queue.append(child)
depths[child] = depths[next_node] + 1
path[child] = next_node
else:
pass
if N==K:
print(0)
print(K)
else:
visited=[False]*(100_000+1)
depths=[-1]*(100_000+1)
path=[-1]*(100_000+1)
start = N
ans=[K]
ind=K
BFS(visited, depths, start)
while path[ind]!=N:
ans.append(path[ind])
ind=path[ind]
ans.append(N)
print(*ans[::-1])
โด๏ธ ๊ฐ์ค์น ์ฐ์ ์์ ์๋ BFS์ ์ผ๋ฐ์ ์ธ ์ต๋จ๊ฒฝ๋ก ์ถ๋ ฅ์ ์ค๋ฒ1 <1697 ์จ๋ฐ๊ผญ์ง> ๋ฌธ์ . ์ฌ๊ธฐ์ ์ต๋จ๊ฒฝ๋ก์ ๋ชจ๋ ๋ ธ๋ ๋ฒํธ๋ฅผ ์ถ๋ ฅํ๋ ์ต๋จ๊ฒฝ๋ก ์ถ๋ ฅ ๊ธฐ๋ฅ์ด ์ถ๊ฐ
โด๏ธ ์๋ ๊ทธ๋ฆผ์ ์ฐธ์กฐํ๋ฏ, ์์ node๋ฅผ index๋กํ๊ณ ํด๋น ์์ node์ list value๋ก ๋ถ๋ชจ node๋ฅผ ์ ์ฅ. ๊ทธ๋ฌ๋ฉด ๋์ฐฉ ๋ ธ๋ K index๋ถํฐ ์ญ์ผ๋ก N index ๋ง๋ ๋ ๊น์ง ์ถ๋ ฅํ๋ idea
: queue์ node๊ฐ ๋ค์ด๊ฐ ๋ ๋ง๋ค path[child] = next_node๋ก path update. ์๋ฅผ ๋ค์ด 5์์ 17๊น์ง์ ์ต๋จ๊ฒฝ๋ก ๋ด์ฉ ์ถ๋ ฅํ๊ณ ์ถ๋ค๋ฉด ๊ฑฐ๊พธ๋ก index 17๋ถํฐ index 5๋ฅผ ๋ง๋ ๋๊น์ง ์ฐจ๋ก๋๋ก ์ญ์ถ์ ๊ฒฝ๋ก ์ ์ฅํด ์ถ๋ ฅ
โ 13549 ์จ๋ฐ๊ผญ์ง 3 โ
import sys
input = sys.stdin.readline
from collections import deque
N,K=map(int,input().split())
depths=[-1]*(100_001)
visited=[False]*(100_001)
def BFS(depths, visited, N):
queue = deque([N])
visited[N] = True
depths[N] = 0
while queue:
v = queue.popleft()
if (2*v)<=100_000 and not visited[2*v]:
visited[2*v] = True
queue.append(2*v)
depths[2*v] = depths[v]
if (v*2) == K:
return
if 0<=(v-1) and not visited[(v-1)]:
visited[v-1] = True
queue.append(v-1)
depths[v-1] = depths[v] + 1
if (v-1) == K:
return
if (v+1)<=100_000 and not visited[(v+1)]:
visited[v+1] = True
queue.append(v+1)
depths[v+1] = depths[v] + 1
if (v+1) == K:
return
if N==K:
print(0)
else:
BFS(depths, visited, N)
print(depths[K])
โด๏ธ ์ค๋ฒ1 1697 ์จ๋ฐ๊ผญ์ง ๋ฌธ์ ์ ์ ์ฌ
→ ํ์ง๋ง ์ ๋ฌธ์ ๋ 0์ด ํ์ 2*X๋ก ์ด๋ํ๋ค๋ ์ . ์ฆ X-1์ด๋ X+1 ์ด๋๋ณด๋ค 2*X๋ฅผ ๋จผ์ ๊ณ ๋ ค(๊ฐ์ค์น ์ฐ์ ์์๊ฐ ์๋ BFS ๋ณํ)ํด์ผ ํ๋ค. ๋ฐ๋ผ์ v*2๊ฐ K์ธ์ง ์๋ ์ง๋ฅผ ๋ค๋ฅธ ์ผ์ด์ค X-1 / X+1 ์ด๋๋ณด๋ค ๋จผ์ ๋ฐ์ ธ์ผ ํจ. queue์์ ๊ฐ์ฅ ๋จผ์ 2*X๋ก ์ด๋ํ ๊ฒฝ์ฐ๊ฐ ๋๊ธฐํด์ผ ํ์์์ธ X-1์ด๋ X+1๋ก ์ด๋ํ์ ๋ ๋์จ ์ต๋จ์๊ฐ๋ณด๋ค ๋ ์ต๋จ์๊ฐ์ด ๋์๋ ๋จผ์ ์ก์์ ์ถ๋ ฅํ ์ ์๊ธฐ ๋๋ฌธ
โ 16928 ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์ โ
import sys
input=sys.stdin.readline
from collections import deque
N,M=map(int,input().split())
depths=[-1]*101
visited=[False]*101
d=[1,2,3,4,5,6]
snake_ladder=dict() #no snake/ladder exists in 1 and 100
def BFS(depths, visited, start):
visited[start] = True
depths[start] = 0
queue=deque([start])
while queue:
v = queue.popleft()
for i in range(6):
nx=v+d[i]
if nx<=100 and not visited[nx]:
depths[nx] = depths[v] + 1
visited[nx] = True
if nx==100: #arrived at destination
return
if nx not in snake_ladder.keys(): # not arrived in snake / ladder
queue.append(nx)
else: #arrived in snake/ladder
after=snake_ladder[nx]
if not visited[after]:
visited[after] = True
depths[after] = depths[nx]
queue.append(after)
for _ in range(N):
x,y=map(int,input().split())
snake_ladder[x]=y
for _ in range(M):
u,v=map(int,input().split())
snake_ladder[u]=v
BFS(depths,visited,1)
print(depths[100])
โด๏ธ ์ต๋จ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ BFS depth ์ ํ์ ์ธ ์ ํ์, ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ๋ฅผ ํ์ฉํด ๋ค๋ฅธ node์์ ๋ค๋ฅธ node๋ก ์ด๋์ด ๊ฐ๋ฅํ ๊ธฐ๋ฅ๋ง ์ถ๊ฐ๋จ
โด๏ธ dict()๋ฅผ ๋ง๋ค์ด snake๋ ladder์ผ ๊ฒฝ์ฐ ์ดํ ์ด๋๋๋ node๊ฐ not visited์ผ ๊ฒฝ์ฐ depth ๋ณํ ์์ด ํ ๋น / ์ดํ ์ด๋๋๋ node ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ / queue์ ์ดํ ์ด๋๋๋ node ๋ฃ๊ธฐ 3๊ฐ๋ง ์ฒ๋ฆฌํ๋ฉด ๋ฐ๋ก ๋!
โ BFS์ด๊ณ ์์์ ์ด ์ ํด์ ธ์์ผ๋ฏ๋ก ๋์ฐฉ์ ์ ํฅํด ๋์์ ๋ฐฉํฅ์ด ์๋ ์ํ๋ก ์งํํ๊ธฐ ๋๋ฌธ์ ๋์ค์ ๋ฑ์ด๋ ์ฌ๋ค๋ฆฌ๋ก node๋ฅผ ์ด๋ํด๋ visited ์ฒ๋ฆฌ๋ง ์ ํ๋ค๋ฉด ๋์ ๊น์ง ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ BFS depth๋ก ๋ฐ๋ก ๊ตฌํ ์ ์๋ค.
โ 9019 DSLR โ
import sys
input=sys.stdin.readline
from collections import deque
def BFS(visited,start,end):
visited[start[0]]=True
queue=deque([start])
while queue:
v=queue.popleft()
#D
D=(2*v[0])%10000
#S
S=(v[0]-1) if (v[0]-1)>=0 else 9999
#L
l=str(v[0]).zfill(4)
L=int(l[1]+l[2]+l[3]+l[0])
#R
r=str(v[0]).zfill(4)
R=int(r[3]+r[0]+r[1]+r[2])
DSLR=[D,S,L,R]
for nv in range(len(DSLR)):
if not visited[DSLR[nv]]:
visited[DSLR[nv]]=True
if DSLR[nv] == end: #if answer found
return v[1]+['DSLR'[nv]]
queue.append([DSLR[nv],v[1]+['DSLR'[nv]]])
for _ in range(int(input())):
A,B=map(int,input().split())
visited=[False]*(10_000)
depths=[-1]*(10_000)
print(*BFS(visited,[A,[]],B),sep='')
โด๏ธ ์ BOJ 1240 ๋ ธ๋ ์ฌ์ด์ ๊ฑฐ๋ฆฌ์ ๋ง์ฐฌ๊ฐ์ง๋ก queue์ ๋ฃ์ ๋, ๋ ธ๋ ๋ฒํธ + ํด๋น ๋ ธ๋๊น์ง์ DSLR ์กฐํฉ ๋ช ๋ น์ด๋ฅผ ๋ฃ๋๋ค๋ BFS ์ต๋จ๊ฑฐ๋ฆฌ ์์ฉ๋ฌธ์ . BFS traversingํ๋ฉด์ ๋ ๋ฒ์งธ ์ ๋ณด ๋ฌธ์์ด์ด ๊ณ์ ๋ํด์ง๋ง ๋ณด๋ฉด ๋จ. queue์ ๋ ๊ฐ์ง ์ ๋ณด๊ฐ ๋ค์ด๊ฐ์ ์ธ์งํ๊ธฐ๋ง ํ๋ฉด OK!
โ 1963 ์์ ๊ฒฝ๋ก โ
import sys
input=sys.stdin.readline
from collections import deque
def BFS(start,end):
dq=deque([(start,0)])
while dq:
n,depth=dq.popleft()
if not visited[n]:
visited[n] = True
for i in range(4):
for j in range(10):
if i == 0:
if j != 0:
next_n = int(str(j)+str(n)[1:])
else: continue
else:
next_n = int(str(n)[:i]+str(j)+str(n)[i+1:])
if next_n == end:
return depth + 1
else:
if not visited[next_n] and sieve[next_n]:
dq.append((next_n,depth+1))
return 'Impossible'
#sieve ~9999
sieve = [True]*(10001)
sieve[0],sieve[1]=False,False
for i in range(2,10001):
if sieve[i] == True:
for j in range(i,10001,i):
sieve[j] = False
sieve[i] = True
for _ in range(int(input())):
x,y=map(int,input().split())
visited=[False]*10001
if x == y:
print(0)
else:
print(BFS(x,y))
โด๏ธ x → y๋ก ๊ฐ๋ ์์์ ๊ฒฝ๋ก๋ฅผ BFS๋ก ์ฐพ๋๋ค. x์ ๊ฐ ์๋ฆฌ๋ฅผ brute-force๋ก 0๋ถํฐ 9๊น์ง ๋ฐ๊พธ๋ฉฐ(์ฒซ ์๋ฆฌ๋ง 0 ์ ์ธ) ๋ง๋ next_n์ด ์์์ ํด๋น๋๊ณ not visited๋ผ๋ฉด BFS deque์ (next_n, depth + 1) append. (๋ค ์๋ฆฌ๊น์ง ์์ sieve ์๋ผํ ์คํ ๋ค์ค ์ฒด๋ก ๋ฏธ๋ฆฌ ์์ฑ)
โป ์ด ๋ not visited ์ ๊ฒ ์ด์ ๋, ์ด๋ฏธ ๋ฐฉ๋ฌธํ๋ค๋ฉด ๋ฐฉ๋ฌธํ ์ซ์์ depth๊ฐ ํ์ฌ depth๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ผ๋ฏ๋ก ํด๋น next_n ์ดํ๋ฅผ ์ ๊ฒํ ํ์ x
โ 12851 ์จ๋ฐ๊ผญ์ง 2 โ
import sys
input=sys.stdin.readline
from collections import deque
def BFS(visited, depths, start, cnt):
visited[start] = True
depths[start] = 0
queue = deque([start])
found=False
while queue:
v = queue.popleft()
for child in [v-1, v+1, v*2]:
if 0 <= child <= 100_000: #cond1
if child == K:
if not found:
depths[child] = depths[v] + 1
found = True
found_depth = depths[child]
visited[child] = True
cnt+=1
else: #found again
if found_depth == depths[v] + 1:
cnt+=1
else:
return cnt
else: #child!=K
if not visited[child]:
depths[child] = depths[v] + 1
visited[child] = True
queue.append(child)
if found:
if found_depth < depths[child]:
return cnt
else: #visited[child] = True
if depths[child] == depths[v] + 1:
depths[child] = depths[v] + 1
visited[child] = True
queue.append(child)
if found:
if found_depth < depths[child]:
return cnt
return cnt
N,K=map(int,input().split())
if N==K:
print(0,1,sep='\n')
else:
visited=[False]*(100_000+1)
depths=[-1]*(100_000+1)
start = N
cnt = 0
x = BFS(visited, depths, start, cnt)
print(depths[K], x, sep='\n')
โด๏ธ ์ ๊ทธ๋ํ 1 → 6์ ๊ฐ๊ธฐ ์ํ BFS ๊ทธ๋ฆผ ์์ ์ฐธ์กฐ. ์ต๋จ๊ฑฐ๋ฆฌ ๊ฐ์ง์๋ flag ๋ณ์ ํ์ฉ & depth ๋ถ๋ฑ์ ํ์ฉ์ผ๋ก ํ ์ ์๋ค.
(1) ์ฃผ์ด์ง ์ซ์ ๋ฒ์ ์๋ child node ์ ์ธ(๋ ธ๋์ X)
(2) ex) ํ์ฌ node์์ v-1, v+1, 2*v๋ก ๋ป์์ ๋(BFS ํ์)
โ child node๊ฐ ๋ชฉํ node๊ฐ ์๋ ๊ฒฝ์ฐ
[1] child node๋ฅผ ๋ฐฉ๋ฌธํ์ง ์์ ์๋ก์ด node (์ ๊ทธ๋ฆผ ๋ณด๋ผ์) : depths update, visited True ์ค์ , queue์ ๋ฃ๊ธฐ
[2] child node๋ฅผ ์ด๋ฏธ ๋ฐฉ๋ฌธํ node(ex ์์ ๊ทธ๋ฆผ์ ๊ฒฝ์ฐ depth 1์์ 2๋ก ๋ป์ ๋ node number 2) (์ ๊ทธ๋ฆผ ์ด๋ก์) : ๋์ผํ number์ node๋ ๋์ผ depth์ ์์ ๋๋ง ์ดํ ํ์ ๊ฐ๋ฅํ๊ณ , ์ด๋ฏธ ์ด์ depth์ ์กด์ฌํ๋ค๋ฉด pass
→ ๋ฐ๋ผ์ depths[child] == depths[v] + 1์ผ ๊ฒฝ์ฐ๋ง depths update, queue์ ๋ฃ๊ธฐ
โก child node๊ฐ ๋ชฉํ node์ธ ๊ฒฝ์ฐ
[1] ์ฒซ ๋ชฉํ node ๋๋ฌ์ด๋ผ๋ฉด(found == False) (์ ๊ทธ๋ฆผ ๋นจ๊ฐ์): depths update, visited True ์ค์ , queue์ ๋ฃ๊ธฐ, found = True, found_depth ์ค์ , cnt(์ ๋ต๋ณ์) += 1
[2] ์ด๋ฏธ ๋๋ฌํ ์ ์ด ์๋ค๋ฉด (์ ๊ทธ๋ฆผ ๋ ธ๋์): found_depth == depths[v] + 1์ผ ๊ฒฝ์ฐ๋ง cnt += 1
+ return ์ข ๋ฃ ์ฝ๋ ๋ฃ๊ธฐ +
โ child node๊ฐ ๋ชฉํ node๊ฐ ์๋ ๊ฒฝ์ฐ
[1] child node๋ฅผ ๋ฐฉ๋ฌธํ์ง ์์ ์๋ก์ด node์ธ ๊ฒฝ์ฐ(์ ๊ทธ๋ฆผ ๋ณด๋ผ์)
: if found ๋ต ์ฐพ์๋ค๋ฉด → found_depth๋ณด๋ค ํ์ฌ depth[child]๊ฐ ๋ ํฌ๋ค๋ฉด, return ์ข ๋ฃ(๊ทธ ๋ค์ depth๋ก ์งํ๋ ๊ฒ์ด๋ฏ๋ก)
[2] child node๋ฅผ ์ด๋ฏธ ๋ฐฉ๋ฌธํ node์ธ ๊ฒฝ์ฐ(์ ๊ทธ๋ฆผ ์ด๋ก์)
: ๋์ผํ number์ node๊ฐ ๋์ผ depth์ ์์ ๊ฒฝ์ฐ → if found ๋ต ์ฐพ์๋ค๋ฉด → found_depth๋ณด๋ค ํ์ฌ depth[child]๊ฐ ๋ ํฌ๋ค๋ฉด, return ์ข ๋ฃ(๊ทธ ๋ค์ depth๋ก ์งํ๋ ๊ฒ์ด๋ฏ๋ก)
โก child node๊ฐ ๋ชฉํ node์ธ ๊ฒฝ์ฐ
[1] ์ฒ์์ผ๋ก ๋๋ฌ(์ ๊ทธ๋ฆผ ๋นจ๊ฐ์)
: return x
[2] ์ด๋ฏธ ๋๋ฌํ ์ ์๋ ๊ฒฝ์ฐ(์ ๊ทธ๋ฆผ ๋ ธ๋์)
: ์ด๋ฏธ ์ฐพ์ found_depth์ ํ์ฌ depth(depths[v]+1)์ ์๋ก ๊ฐ์ง ์๋ค๋ฉด, return ์ข ๋ฃ
โป ์ต๋จ๊ฒฝ๋ก ๊ฐฏ์ ํต์ฌ์, found bool flag ๋ณ์๋ฅผ ๋ง๋ ๋ค,
์ฒ์์ผ๋ก ๋๋ฌํ์ง ์์ ๊ฒฝ์ฐ(found==True)์ผ ๋ ์ฒ์์ผ๋ก ๋๋ฌํ์ ๋์ node depth(found_depth)์ ํ์ฌ depth๋ฅผ ๋น๊ต. ์ฆ depth๋ฅผ ๋น๊ตํ๋ฉด์ ์ต๋จ๊ฒฝ๋ก ๊ฐฏ์๋ฅผ updateํ๋ฉด ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํ์ฌ depth๊ฐ found_depth๋ฅผ ๋์ด์ฐ์ผ๋ฉด return ์ข ๋ฃ โป
โ 3055 ํ์ถ โ
import sys
input=sys.stdin.readline
from collections import deque
R,C=map(int,input().split())
def spread_water(waters):
dx,dy = [-1,1,0,0], [0,0,-1,1]
lw = len(waters)
cnt = 0
while cnt<lw:
water = waters.popleft()
x,y = water[0],water[1]
for i in range(4):
nx,ny=x+dx[i],y+dy[i]
if 0<=nx<R and 0<=ny<C:
if graph[nx][ny] == '.':
graph[nx][ny] = '*'
waters.append((nx,ny))
cnt+=1
def BFS(graph, visited, waters, start, depths):
queue = deque([start])
visited[start[0]][start[1]] = True
dx,dy = [-1,1,0,0], [0,0,-1,1]
prev_depth = 0
while queue:
node = queue.popleft()
x,y=node[0],node[1]
for i in range(4):
nx,ny=x+dx[i],y+dy[i]
if 0<=nx<R and 0<=ny<C:
if graph[nx][ny] in ['D', '.'] and not visited[nx][ny]:
if prev_depth != depths[x][y] + 1:
spread_water(waters)
if graph[nx][ny] == 'D':
print(depths[x][y]+1)
sys.exit()
if graph[nx][ny] == '.' and not visited[nx][ny]:
visited[nx][ny] = True
depths[nx][ny] = depths[x][y] + 1
prev_depth = depths[nx][ny]
queue.append((nx,ny))
graph=[]
waters = deque()
visited = [[False]*C for _ in range(R)]
depths = [[-1]*C for _ in range(R)]
for i in range(R):
x = list(input().rstrip())
for j in range(C):
if x[j] == 'D':
gul = (i,j)
elif x[j] == 'S':
start = (i,j)
depths[i][j] = 0
elif x[j] == '*':
waters.append((i,j))
graph.append(x)
second = 0
BFS(graph, visited, waters, start, depths)
print('KAKTUS')
โด๏ธ ๊ณ ์ด๋์น๊ฐ S๋ถํฐ ์์ํด์ ์ด๋ํ๊ธฐ ์ , ๋ฌผ์ด ํผ์ง๋ ์๊ณ ๋ฆฌ์ฆ ์คํ. ์ด ๋ depth๊ฐ ๋ฌ๋ผ์ง ๋๋ง ์ด๋ํ๊ธฐ ์ ๋ฌผ์ด ํผ์ง๋ ์๊ณ ๋ฆฌ์ฆ ์คํ.
โด๏ธ ๋ฌผ์ด ํผ์ง๋ BFS ์๊ณ ๋ฆฌ์ฆ: ์ฃผ์ด์ง ๋ฌผ ๊ฐ์๋งํผ๋ง queue ๋๋ฆฌ๋ฉด์ BFS
โด๏ธ ์์ ์ง์ ์ depth 0์ผ๋ก ํ๊ณ ๋ชฉ์ ์ง๊น์ง ๊ฐ๋ ๊ฑฐ๋ฆฌ๊ฐ ์์์๊ฐ๊ณผ ๋์ผํ ๊ฐ๋ ์ด๋ฏ๋ก ๋ชฉ์ ์ง์์์ depth ์ถ๋ ฅ / depth ์ถ๋ ฅ์ ๋ชปํ๋ฉด KAKTUS ์ถ๋ ฅ
โ 14395 4์ฐ์ฐ โ
import sys
input=sys.stdin.readline
from collections import deque
s,t=map(int,input().split())
def BFS(queue,visited,start,end):
global found
found = False
while queue:
v = queue.popleft()
for op in ['*','+','-','/']:
if op=='*':
if v[0]*v[0] == t:
found=True
v[1]+='*'
print(*v[1],sep='')
return
if v[0]*v[0] not in visited and v[0]*v[0]<=10**9:
visited.add(v[0]*v[0])
new_v=v[1]+'*'
queue.append([v[0]*v[0],new_v])
elif op=='+':
if (v[0]+v[0]) == t:
found=True
v[1]+='+'
print(*v[1],sep='')
return
if (v[0]+v[0]) not in visited and (v[0]+v[0])<=10**9:
visited.add(v[0]+v[0])
new_v=v[1]+'+'
queue.append([v[0]+v[0],new_v])
elif op=='-':
if (v[0]-v[0]) == t:
found=True
v[1]+='-'
print(*v[1],sep='')
return
if (v[0]-v[0]) not in visited and 0<=(v[0]-v[0]):
visited.add(v[0]-v[0])
new_v=v[1]+'-'
queue.append([v[0]-v[0],new_v])
else:
if v[0]!=0:
if (v[0]//v[0])==t:
found=True
v[1]+='/'
print(*v[1],sep='')
return
if (v[0]//v[0]) not in visited and 1<=(v[0]//v[0]):
visited.add(v[0]//v[0])
new_v=v[1]+'/'
queue.append([v[0]//v[0],new_v])
if s==t: print(0)
else:
visited=set()
visited.add(s)
queue=deque([[s,'']])
BFS(queue,visited,s,t)
if not found:
print(-1)
โด๏ธ queue ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํด ๋จผ์ ๋ฃ์ ๊ฒ ๋จผ์ ๋นผ์ * + - / ์์๋ก ์ฐ์ฐ ์งํ. ์ต์ ์ฐ์ฐ์ ํ์๋ฅผ ๊ตฌํ๋ฏ๋ก, queue ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํด ๋จผ์ ๋บ ๊ฒ์(popleft()) ์ฐ์ฐ ์งํํด์ผ greedyํ ๊ด์ ์์ ์ต์ ์ฐ์ฐ ํ์๋ฅผ ๊ตฌํ ์ ์๋ค.
โด๏ธ visited ๋ฐฐ์ด์ ์ ์ฒด ์์ ํฌ๊ธฐ 10^9๊น์ง ๋ง๋ค๊ธฐ์๋ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ๋ฐ์. set๋ฅผ ํ์ฉํด bfs ๊ณผ์ ์ค ๋ง๋ค์ด์ง ์ซ์๋ง visited set ์๋ฃ๊ตฌ์กฐ์ ๋ฃ์ด ๋ฐฉ๋ฌธ ์ฌ๋ถ ์ฒดํฌ(set์ in O(1)). ๋ฐฉ๋ฌธํ๋ค๋ฉด ๋ ์ด์ bfs ์งํํ ํ์ x. ์ด๋ฏธ ์์ ์์์์ +, -,*, /์ ์ผ์ด์ค ๊ฐ๊ฐ์ ์ดํด๋ณด์์ผ๋ฏ๋ก ๋ใ ์ผํ๊ฒ ํ ๋ฒ ๋ +, -, *, /๋ฅผ ์ฒดํนํ ํ์๋ ์๋ค.
โด๏ธ queue ์์ฒด์ ๋ฃ๋ ๋ ธ๋๋ ๋ฐ๋๋ ์ซ์์ ์ ๋ฐ์ดํธ ๋๋ ์ฐ์ฐ์ ์์ ์ด๋ ๊ฒ 2๊ฐ๋ฅผ list๋ก ๋ง๋ค์ด ์งํ์ํค๋ฉด ๋๋ค.
'BOJ > ๐ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ Coordinate Compression Advanced - 2 Solvedโ (0) | 2023.12.28 |
---|---|
โ Tree Advanced I - 1 Solvedโ (1) | 2023.12.22 |
โ Greedy Upper-Advanced I - 1 Solvedโ (1) | 2023.10.18 |
โ BF Advanced I - 3 Solvedโ (0) | 2023.10.06 |
โ Greedy Advanced I - 5 Solvedโ (0) | 2023.08.30 |
๋๊ธ