From b87a7bc268a4152e59327147508479e0f3adb65b Mon Sep 17 00:00:00 2001 From: smhwang0109 Date: Thu, 4 Feb 2021 09:30:28 +0900 Subject: [PATCH] Update 2021-1 4th Weekly Plan Algo add Finish --- ...0_\354\233\220\354\210\255\354\235\264.py" | 58 +++++++++++++++++++ ..._\353\221\220_\353\217\231\354\240\204.py" | 54 +++++++++++++++++ .../16397_\355\203\210\354\266\234.py" | 42 ++++++++++++++ ...04\353\241\234\354\240\235\355\212\270.py" | 34 +++++++++++ 4 files changed, 188 insertions(+) create mode 100644 "2021_year/1_month/4_week/Soomin/Algorithm/1600_\353\247\220\354\235\264_\353\220\230\352\263\240\355\224\210_\354\233\220\354\210\255\354\235\264.py" create mode 100644 "2021_year/1_month/4_week/Soomin/Algorithm/16197_\353\221\220_\353\217\231\354\240\204.py" create mode 100644 "2021_year/1_month/4_week/Soomin/Algorithm/16397_\355\203\210\354\266\234.py" create mode 100644 "2021_year/1_month/4_week/Soomin/Algorithm/9466_\355\205\200_\355\224\204\353\241\234\354\240\235\355\212\270.py" diff --git "a/2021_year/1_month/4_week/Soomin/Algorithm/1600_\353\247\220\354\235\264_\353\220\230\352\263\240\355\224\210_\354\233\220\354\210\255\354\235\264.py" "b/2021_year/1_month/4_week/Soomin/Algorithm/1600_\353\247\220\354\235\264_\353\220\230\352\263\240\355\224\210_\354\233\220\354\210\255\354\235\264.py" new file mode 100644 index 0000000..3f3462f --- /dev/null +++ "b/2021_year/1_month/4_week/Soomin/Algorithm/1600_\353\247\220\354\235\264_\353\220\230\352\263\240\355\224\210_\354\233\220\354\210\255\354\235\264.py" @@ -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)) diff --git "a/2021_year/1_month/4_week/Soomin/Algorithm/16197_\353\221\220_\353\217\231\354\240\204.py" "b/2021_year/1_month/4_week/Soomin/Algorithm/16197_\353\221\220_\353\217\231\354\240\204.py" new file mode 100644 index 0000000..d667562 --- /dev/null +++ "b/2021_year/1_month/4_week/Soomin/Algorithm/16197_\353\221\220_\353\217\231\354\240\204.py" @@ -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)) \ No newline at end of file diff --git "a/2021_year/1_month/4_week/Soomin/Algorithm/16397_\355\203\210\354\266\234.py" "b/2021_year/1_month/4_week/Soomin/Algorithm/16397_\355\203\210\354\266\234.py" new file mode 100644 index 0000000..add38e6 --- /dev/null +++ "b/2021_year/1_month/4_week/Soomin/Algorithm/16397_\355\203\210\354\266\234.py" @@ -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)) diff --git "a/2021_year/1_month/4_week/Soomin/Algorithm/9466_\355\205\200_\355\224\204\353\241\234\354\240\235\355\212\270.py" "b/2021_year/1_month/4_week/Soomin/Algorithm/9466_\355\205\200_\355\224\204\353\241\234\354\240\235\355\212\270.py" new file mode 100644 index 0000000..b0bb2bb --- /dev/null +++ "b/2021_year/1_month/4_week/Soomin/Algorithm/9466_\355\205\200_\355\224\204\353\241\234\354\240\235\355\212\270.py" @@ -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()) \ No newline at end of file