- ๋ชฉํ -
1. ์์ ์ ๋ ฌ
์ ํ ๊ณผ๋ชฉ์ด ์๋ ๊ฒฝ์ฐ ์ ํ ๊ณผ๋ชฉ์ ์ถ๋ ฅํ๊ณ ์ดํ์ ์ฐ๊ณ๋ ๊ณผ๋ชฉ์ ์ถ๋ ฅํ๋ ๋ฌธ์ ์๋ค.
๊ณผ๋ชฉ๋ช ์ด ์ฒ์์ ์ฃผ์ด์ง๋ฏ๋ก ์ด ๊ณผ๋ชฉ๋ช ์ dictํ์ผ๋ก ์ ์ฅํ๋ค.
M๊ฐ์ ์ ๋ ฅ ๊ฐ์์ ์ ํ ๊ณผ๋ชฉ, ํํ ๊ณผ๋ชฉ์ด ์ ๋ ฅ๋๋๋ฐ ์ ํ ๊ณผ๋ชฉ์ด ์๋ ๊ฒฝ์ฐ ์ด๋ฅผ count ํด์ค๋ค.
count๊ฐ 0์ด๋ฉด ์ด๋ ์ ํ ๊ณผ๋ชฉ์ด ์๋ ๊ฒฝ์ฐ์ด๋ค.
์ดํ BFS๋ฅผ ํตํด ๊ณผ๋ชฉ ์ด์ ์์๋ฅผ ํ์ํ๋ค.
์ต์ข ์ฝ๋๋
def find_path(subject_dict, count, first):
visited = []
while first:
first.sort(reverse=True)
e = first.pop()
visited.append(e)
for i in range(len(subject_dict[e])):
count[subject_dict[e][i]] -= 1
if count[subject_dict[e][i]] == 0:
first.append(subject_dict[e][i])
return visited
def main():
n, m = map(int, input().split())
subject_dict = {}
count = {}
first = []
subject = list(input().split())
for i in range(n):
subject_dict[subject[i]] = []
count[subject[i]] = 0
for _ in range(m):
i, j = input().split()
subject_dict[i].append(j)
count[j] += 1
for i in subject:
if count[i] == 0:
first.append(i)
result = find_path(subject_dict, count, first)
print(*result)
if __name__ == "__main__":
main()
2. ์ ๊ธ์ ๋ฒ์น
์์ ์ ๋ ฌ๋ก ์ ๊ทผํ์ง ์๊ณ ๊ทธ๋ฅ ๋ธ๋ฃจํธ ํฌ์ค์ ๋ฐฉ๋ฒ์ ์ผ๋ค.
์ต์ข ์ฝ๋๋
def main():
n = int(input())
input_list = [0 for _ in range(n)]
count = [0 for _ in range(n)]
for i in range(n):
a, b = map(int, input().split())
input_list[i] = [a, b]
count[i] = 0 # ๋จน๊ณ ๋จนํ๋ ๊ฒ์ ํ์ํ๊ธฐ ์ํ ๋ฆฌ์คํธ
sum_all = n
input_list = sorted(input_list, key = lambda x : (x[0], x[1]))
for i in range(n):
for j in range(i+1, n):
if input_list[i][0] <= input_list[j][0] and input_list[i][1] <= input_list[j][1] and count[i] >= 0 and count[j] >= 0 and count[j] <= 2:
sum_all -= 1 # ํ ์ข
๋ฅ์ ๋ฏธ์๋ฌผ์ด ๋จนํ์ผ๋ฏ๋ก -1
count[i] = -1 # j ๋ฏธ์๋ฌผ์๊ฒ ๋จนํ i ๋ฏธ์๋ฌผ
count[j] += 1 # j ๋ฏธ์๋ฌผ์ด ๋ค๋ฅธ ๋ฏธ์๋ฌผ์ ๋จน์ ํ์
print(sum_all)
if __name__ == "__main__":
main()
3. ๊ธฐ๋ฆ์ด ๊ฐ๋น๊ฐ๋น
์ถ๋ฐ ์ง์ ๊ณผ ๋์ฐฉ ์ง์ ์ ์ฐพ๊ณ A* ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํ์ฌ ํ์ํ๋ค.
์ต์ข ์ ์ผ๋ก ๊ณ์ฐ๋ ๊ธฐ๋ฆ ์์ด ์ ๋ ฅ๋ ๊ธฐ๋ฆ ์๋ณด๋ค ์ ์ ๊ฒฝ์ฐ ๋จ์ ๊ธฐ๋ฆ ์์ ์ถ๋ ฅํ๊ณ , ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ๋ค.
์ต์ข ์ฝ๋๋
import math
def heuristics(x1, y1, x2, y2):
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
def oil_path(edge_dict, n, m, src_x, src_y, dst_x, dst_y):
open_list = []
close_list = []
for i in edge_dict[(src_x,src_y)]:
G = i[1]
H = heuristics(i[0][0], i[0][1], dst_x, dst_y)
F = G + H
open_list.append([i[0], F, G, H])
open_list = sorted(open_list, key = lambda x : -x[1])
close_list.append(open_list.pop())
while True:
check_open_list = []
check_close_list = []
for i in open_list:
check_open_list.append(i[0])
for i in close_list:
check_close_list.append(i[0])
if (dst_x, dst_y) in check_close_list:
break
for i in range(len(close_list)):
for j in edge_dict[close_list[i][0]]:
if j[0] in check_close_list:
continue
G = j[1] + close_list[i][2]
if ((dst_x-1 <= j[0][0] <= dst_x+1) and (dst_y == j[0][1])) or ((dst_x == j[0][0]) and (dst_y-1 <= j[0][1] <= dst_y+1)):
H = 0
else:
H = heuristics(j[0][0], j[0][1], dst_x, dst_y)
F = G + H
if j[0] in check_open_list:
if [j[0], F, G, H][1] < open_list[check_open_list.index(j[0])][1]:
open_list[check_open_list.index(j[0])] = [j[0], F, G, H]
else:
open_list.append([j[0], F, G, H])
open_list = sorted(open_list, key = lambda x : -x[1])
close_list.append(open_list.pop())
return close_list[len(close_list)-1][2]
def main():
n, m, oil = map(int, input().split())
map_list = [[] for _ in range(n)]
edge_dict = {}
src_x = 0
src_y = 0
dst_x = 0
dst_y = 0
for i in range(n):
map_list[i] = list(input())
# src์ dst๊ฐ (0, 0), (n-1, n-1)์ด ์๋ ๊ฒฝ์ฐ๋ ์์ผ๋ฏ๋ก ์ขํ๋ฅผ ์ฐพ๋๋ค.
for i in range(n):
for j in range(m):
if map_list[i][j] == "S":
src_x = j
src_y = i
map_list[i][j] = 0
elif map_list[i][j] == "E":
dst_x = j
dst_y = i
map_list[i][j] = 0
for i in range(n): #ํ
for j in range(m): #์ด
edge = []
if i != 0:
edge.append([(j, i-1), int(map_list[i-1][j])])
if i != n-1:
edge.append([(j, i+1), int(map_list[i+1][j])])
if j != 0:
edge.append([(j-1, i), int(map_list[i][j-1])])
if j != m-1:
edge.append([(j+1, i), int(map_list[i][j+1])])
edge_dict[j, i] = edge
answer = oil_path(edge_dict, n, m, src_x, src_y, dst_x, dst_y)
if oil >= answer:
print(oil-answer)
else:
print("Not enough oil!")
if __name__ == "__main__":
main()
- ๊นํ๋ธ -
github.com/k906506/2020_Winter_Assemble-And-selfcode
'โน๏ธ ๋ผ์ดํ > 2020 ๊ฒจ์ธ๋ฐฉํ ๋ชจ๊ฐ์ฝ(๊ฐ์ธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.27(์) - 6์ฃผ์ฐจ ๊ฐ์ธ ๊ฒฐ๊ณผ (0) | 2021.01.27 |
---|---|
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.27(์) - 6์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2021.01.27 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.20(์) - 5์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2021.01.20 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.13(์) - 4์ฃผ์ฐจ ๊ฐ์ธ ๊ฒฐ๊ณผ (0) | 2021.01.13 |
[์ฝ๋ ํ๊ตฌ๋ง ํ] 2021.01.13(์) - 4์ฃผ์ฐจ ๊ฐ์ธ ๋ชฉํ (0) | 2021.01.13 |