From c61101ee5cdd6320fc10575792f592f37996b04a Mon Sep 17 00:00:00 2001 From: ex1 Date: Tue, 27 Sep 2022 22:47:04 +0900 Subject: [PATCH] . --- "\353\235\274\354\235\270/1.py" | 31 +++++++++ "\353\235\274\354\235\270/3.py" | 57 +++++++++++++++ "\353\235\274\354\235\270/4.py" | 118 ++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 "\353\235\274\354\235\270/1.py" create mode 100644 "\353\235\274\354\235\270/3.py" create mode 100644 "\353\235\274\354\235\270/4.py" diff --git "a/\353\235\274\354\235\270/1.py" "b/\353\235\274\354\235\270/1.py" new file mode 100644 index 0000000..86a2573 --- /dev/null +++ "b/\353\235\274\354\235\270/1.py" @@ -0,0 +1,31 @@ +from collections import defaultdict + +queries = [[2, 10], [7, 1], [2, 5], [2, 9], [7, 32]] +queries1 = [[1, 1], [1, 2], [1, 4], [1, 8]] + +def solution(queries): + d = defaultdict(list) + answer = 0 + + for i in queries: + v, c = i[0], i[1] # 배열 번호, 추가할 원소의 수를 받는다 + if d[v] == []: # 해당 배열 번호의 배열리 처음 만들어진 경우 + i = 0 + while 2**i<=c: + i+=1 + d[v] = [i, c] # 2n승의 n의 값을 딕셔너리에 넣는다. + else: + tmp = d[v][1] + c # 이미 해당 배열 번호의 배열이 나왔던 경우 + d_i = d[v][0] + if tmp > 2**d_i: # 배열의 크기(2**n)보다 큰 경우 + answer+= d[v][1] # 이미 존재하는 원소의 수를 answer에 더함 (복사하는 배열의 수) + while 2**d_i<=tmp: # 배열의 크기를 다시 만든다 + d_i+=1 + d[v] = [d_i, tmp] + else: d[v] = [d_i, tmp] # 배열의 크기(2**n)보다 작은 경우 경우 + + print(answer) + + +solution(queries) +solution(queries1) \ No newline at end of file diff --git "a/\353\235\274\354\235\270/3.py" "b/\353\235\274\354\235\270/3.py" new file mode 100644 index 0000000..d1df05c --- /dev/null +++ "b/\353\235\274\354\235\270/3.py" @@ -0,0 +1,57 @@ +from collections import deque + +# n=3; m=2; fires=[[1,1]]; ices=[[3,3]] +n=5; m=3; fires=[[5,5], [1,3], [5,2]]; ices=[[1,5],[3,2]] + + +dx = [0, 0, -1, 1, 1, 1, -1, -1] # (0~3) 상하좌우 (4~7) 대각 4방향 +dy = [-1, 1, 0, 0, 1, -1, 1, -1] + +def fire(y, x, mt, t, nn): # 좌표, 주어진 배열, 시간, nn = 가로길이 = 세로길이 + visited = [] + queue = deque([[[y, x], t+1]]) + mt[y][x] -=1 # + + while queue: + n = queue.popleft() + if n[0] not in visited and n[1] > 0: + y1 = n[0][0] + x1 = n[0][1] + visited.append(n[0]) + mt[y1][x1] += n[1] + for i in range(8): + yy=y1+dy[i] + xx=x1+dx[i] + if 0<= yy < nn and 0<= xx < nn: + queue.append([[yy, xx], n[1]-1]) + + +def ice(y, x, mt, t, nn): + visited = [] + queue = deque([[[y, x], t+1]]) + mt[y][x] +=1 + + while queue: + n = queue.popleft() + if n[0] not in visited and n[1] > 0: + y1 = n[0][0] + x1 = n[0][1] + visited.append(n[0]) + mt[y1][x1] -= n[1] + for i in range(4): + yy=y1+dy[i] + xx=x1+dx[i] + if 0<= yy < nn and 0<= xx < nn: + queue.append([[yy, xx], n[1]-1]) + + +def solution(n, m, fires, ices): + mt = [[0 for _ in range(n)] for _ in range(n)] + for i in fires: + fire(i[0]-1, i[1]-1, mt, m, n) #배열이 (1,1)에서 시작 + for i in ices: + ice(i[0]-1, i[1]-1, mt, m, n) + + print(mt) + +solution(n, m, fires, ices) \ No newline at end of file diff --git "a/\353\235\274\354\235\270/4.py" "b/\353\235\274\354\235\270/4.py" new file mode 100644 index 0000000..55bdd7b --- /dev/null +++ "b/\353\235\274\354\235\270/4.py" @@ -0,0 +1,118 @@ +# 못풀었음ㅜㅠ +# 조건에 맞는 이동방식을 일일이 계산하여 bfs로 풀음 + +from collections import deque + +# wall = ["H.H", ".HX", "H.H"] +wall = ["....HH", "H..H.H"] + +def BFS(y, x, wall, n1, m, visited): + if wall[y][x] == 'H': + cnt = 1 + else: + cnt = 0 + queue = deque([[[y, x], cnt]]) + + while queue: + n = queue.popleft() + y1 = n[0][0] + x1 = n[0][1] + c = n[1] + if wall[y1][x1] == 'H': + if visited[y1][x1] == 0: + visited[y1][x1] = c + else: + if visited[y1][x1] > c: + visited[y1][x1] = c + + + # case1 오른쪽 2칸 + if y1-1 >= 0 and x1+2 < n1: + f = True + for i in range(3): + if wall[y1-1][x1+i] != '.': + f = False + break + if wall[y1][x1+1] != '.': + f = False + break + if f == True: + queue.append([[y1,x1+2],c+1]) + + # case2 왼쪽 2칸 + if y1-1 >= 0 and x1-2 >= 0: + f = True + for i in range(3): + if wall[y-1][x-i] != '.': + f = False + break + if wall[y][x-1] != '.': + f = False + break + if f == True: + queue.append([[y1,x1-2], c+1]) + + # case3 오른쪽 3칸 + if y1-1 >= 0 and x1+3 < n1: + f = True + for i in range(4): + if wall[y-1][x+i] != '.': + f = False + break + if wall[y][x+1] != '.' or wall[y][x+2] != '.': + f = False + break + if f == True: + queue.append([[y1,x1+3], c+1]) + + # case4 왼쪽 3칸 + if y1-1 >= 0 and x1-3 >= 0: + f = True + for i in range(4): + if wall[y-1][x-i] != '.': + f = False + break + if wall[y][x-1] != '.' or wall[y][x-2] != '.': + f = False + break + if f == True: + queue.append([[y1, x1-3],c+1]) + + # case5 위로 2칸 + if y1-2 >=0: + if wall[y1-1][x1]=='.': + queue.append([[y1-2, x1], c+1]) + + # case6 오른쪽 대각 + if y1-1 >= 0 and x1+1= 0 and x1-1>=0: + if wall[y1-1][x1] == '.' and wall[y1][x1-1] == '.': + queue.append([[y1-1, x1-1],c+1]) + + +def solution(wall): + h = len(wall) + n = len(wall[0]) + visited = [[0 for _ in range(n)] for _ in range(h)] + answer = [[0 for _ in range(n)] for _ in range(h)] + + BFS(h-1, 0, wall, n, h, visited) + + for i in range(h): + for j in range(n): + if wall[i][j] == 'H': + if visited[i][j] > 0: + answer[i][j] = visited[i][j] + else: answer[i][j] = -1 + + print(answer) + + + + + +solution(wall) \ No newline at end of file