-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<n1: | ||
if wall[y1-1][x1] == '.' and wall[y1][x1+1] == '.': | ||
queue.append([[y1-1, x1+1], c+1]) | ||
|
||
# case6 왼쪽 대각 | ||
if y1-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) |