Skip to content

Update 2021-1 4th Weekly Plan Algo add Finish #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import sys
from collections import deque


input = lambda : sys.stdin.readline().strip()

di = [1, -1, 0, 0]
dj = [0, 0, 1, -1]
hi = [2, -2, 2, -2, 1, -1, 1, -1]
hj = [1, 1, -1, -1, 2, 2, -2, -2]

def bfs(init_K):
global H, W
que = deque()
# que : [i, j, 말처럼 갈 수 있는 횟수, 현재까지 온 개수]
que.append([0, 0, init_K, 0])
visited[0][0] = init_K
while que:
i, j, K, cnt = que.popleft()
for idx in range(8):
# 상하좌우 순회
if idx < 4:
ni = i + di[idx]
nj = j + dj[idx]
# 이동 가능한지 탐색
if 0 <= ni < H and 0 <= nj < W and not board[ni][nj]:
# 최종 지점 도착 시 종료
if ni == H - 1 and nj == W - 1:
return cnt + 1
# 방문 리스트의 특정 지점에 K가 더 많이 남은 상태로 오지 않았다면
if visited[ni][nj] < K:
visited[ni][nj] = K
que.append([ni, nj, K, cnt + 1])
# 말처럼 이동 가능할 경우 말처럼 이동
if K > 0:
ni = i + hi[idx]
nj = j + hj[idx]
# 이동 가능한지 탐색
if 0 <= ni < H and 0 <= nj < W and not board[ni][nj]:
# 최종 지점 도착 시 종료
if ni == H - 1 and nj == W - 1:
return cnt + 1
# 방문 리스트의 특정 지점에 K가 더 많이 남은 상태로 오지 않았다면
if visited[ni][nj] < K - 1:
visited[ni][nj] = K - 1
que.append([ni, nj, K - 1, cnt + 1])
return -1

K = int(input())
W, H = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(H)]
# visited default가 0이 아니라 -1이 되는게 중요!
visited = [[-1] * W for _ in range(H)]

if W == 1 and H == 1:
print(0)
else:
print(bfs(K))
54 changes: 54 additions & 0 deletions 2021_year/1_month/4_week/Soomin/Algorithm/16197_두_동전.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
from collections import deque


input = lambda : sys.stdin.readline().strip()

di = [1, -1, 0, 0]
dj = [0, 0, 1, -1]

def bfs(i1, j1, i2, j2):
global N, M
que = deque()
que.append((i1, j1, i2, j2, 0))
visited[i1][j1].add((i2, j2))
while que:
i1, j1, i2, j2, cnt = que.popleft()
for k in range(4):
ni1 = i1 + di[k]
nj1 = j1 + dj[k]
ni2 = i2 + di[k]
nj2 = j2 + dj[k]
if (((ni1 < 0 or ni1 >= N) or (nj1 < 0 or nj1 >= M)) and (0 <= ni2 < N and 0 <= nj2 < M)) or\
(((ni2 < 0 or ni2 >= N) or (nj2 < 0 or nj2 >= M)) and (0 <= ni1 < N and 0 <= nj1 < M)):
return cnt + 1
elif 0 <= ni1 < N and 0 <= nj1 < M and 0 <= ni2 < N and 0 <= nj2 < M:
if cnt >= 9:
continue
if board[ni1][nj1] == '#' and board[ni2][nj2] == '#':
continue
elif board[ni1][nj1] == '#':
ni1, nj1 = i1, j1
elif board[ni2][nj2] == '#':
ni2, nj2 = i2, j2
originLen = len(visited[ni1][nj1])
visited[ni1][nj1].add((ni2, nj2))
if len(visited[ni1][nj1]) > originLen:
que.append((ni1, nj1, ni2, nj2, cnt + 1))
return -1

N, M = map(int, input().split())
board = [list(input()) for _ in range(N)]
visited = [[set() for _ in range(M)] for __ in range(N)]

coin_list = []
for i in range(N):
for j in range(M):
if board[i][j] == 'o':
coin_list.extend([i, j])
if len(coin_list) == 4:
break
if len(coin_list) == 4:
break

print(bfs(*coin_list))
42 changes: 42 additions & 0 deletions 2021_year/1_month/4_week/Soomin/Algorithm/16397_탈출.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sys
from collections import deque


input = lambda : sys.stdin.readline().strip()

def bfs(N):
global T, G
que = deque()
que.append((N, 0))
visited[N] = True
if N == G:
return 0
while que:
n, cnt = que.popleft()
if cnt >= T:
return "ANG"
n1 = n + 1
n2 = False
if n * 2 < 100000:
n2 = n * 2
s2 = str(n2)
for i in range(len(s2)):
i2 = int(s2[i])
if i2:
s2 = s2.replace(s2[i], str(i2 - 1), 1)
break
n2 = int(s2)
if n1 == G or n2 == G:
return cnt + 1
if n1 < 100000 and not visited[n1]:
visited[n1] = True
que.append((n1, cnt + 1))
if n2 and not visited[n2]:
visited[n2] = True
que.append((n2, cnt + 1))
return "ANG"

N, T, G = map(int, input().split())
visited = [False] * 100000

print(bfs(N))
34 changes: 34 additions & 0 deletions 2021_year/1_month/4_week/Soomin/Algorithm/9466_텀_프로젝트.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys

input = lambda : sys.stdin.readline().strip()

def dfs():
global n
answer = 0
for i in range(1, n + 1):
if visited[i]:
continue
level = i
cnt = 0
while True:
if visited[i] and level == visited[i][0]:
break
cnt += 1
if visited[i]:
visited[i] = (level, cnt)
break
visited[i] = (level, cnt)
i = D[i]
answer += visited[i][1] - 1
return answer

T = int(input())
for tc in range(T):
n = int(input())
selected = list(map(int, input().split()))

D = dict(zip([i for i in range(1, n+1)], selected))

visited = [0] * (n + 1)

print(dfs())