-
Notifications
You must be signed in to change notification settings - Fork 2
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
62-9kyo-hwang #217
62-9kyo-hwang #217
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
무μ§μ± bfs λ°λ‘ λλ Έμ΅λλ€.
import collections
def can_go(x, y):
if not (0 <= x < N and 0 <= y < M):
return False
if visited[x][y]:
return False
if maps[x][y] == 'X':
return False
return True
def get_food_amount(start_x, start_y):
global dx, dy, maps, visited
food = 0
queue = collections.deque([(start_x, start_y)])
visited[start_x][start_y] = True
while queue:
x, y = queue.popleft()
food += int(maps[x][y])
for i in range(4):
next_x = x + dx[i]
next_y = y + dy[i]
if can_go(next_x, next_y):
visited[next_x][next_y] = True
queue.append((next_x, next_y))
return food
N, M = 0, 0
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
maps, visited = [], []
def solution(mapss):
global N, M, visited, maps
maps = mapss
N = len(maps)
M = len(maps[0])
visited = [[False] * M for _ in range(N)]
answer = []
for i in range(N):
for j in range(M):
if maps[i][j] != 'X' and not visited[i][j]:
answer.append(get_food_amount(i, j))
if not answer:
return [-1]
return sorted(answer)
μ€... mapss κΉλ¦¬νλ°μ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νμ΄μ¬μ λ¬Έμμ΄ μμ λ³κ²½μ΄ μλμ, λ°λ‘ 체ν¬ν μ μλλ‘ μ²λ¦¬ν΄μ£Όμ΄μΌνλλ°...
visited(방문체ν¬)λ₯Ό λ§λ€κ±°λ mapsλ₯Ό μ°μ°ν λΆν΄νλ λ°©λ²μ΄ μμ΅λλ€...
μ λ mapsλ₯Ό νλμ© λΆλ¦¬μμΌμ€¬μ΅λλ€!
μ½λ
def solution(maps):
answer = []
offset = [(-1, 0), (0, 1), (1, 0), (0, -1)]
# μ¬κΈ°μ mapsμ λΆλ¦¬ν΄μ£Όκ³ , Xλ 0μΌλ‘ μΉνν΄μ€¬μ΅λλ€.
memo = [[int(i) if not i == 'X' else 0 for i in row] for row in maps]
h, w = len(maps), len(maps[0])
def bfs(x, y):
total = memo[x][y]
memo[x][y] = 0
dq = [(x, y)]
while dq:
x, y = dq.pop()
for dx, dy in offset:
px = x + dx
py = y + dy
if not 0 <= px < h or not 0 <= py < w:
continue
if (value := memo[px][py]) == 0:
continue
total += value
memo[px][py] = 0
dq.append((px, py))
return total
for x in range(h):
for y in range(w):
if memo[x][y] == 0:
continue
answer.append(bfs(x, y))
if not answer:
answer.append(-1)
return sorted(answer)
π λ¬Έμ λ§ν¬
무μΈλ μ¬ν
βοΈ μμλ μκ°
10λΆ
β¨ μλ μ½λ
1. λ¬Έμ μ€λͺ
1 x 1ν¬κΈ°μ μ¬κ°νλ€λ‘ μ΄λ£¨μ΄μ§ μ§μ¬κ°ν 격μ ννμ μ§λ μ λ³΄κ° μ£Όμ΄μ§λ€. 격μμ κ° μΉΈμλ 'X' λλ 1μμ 9 μ¬μ΄μ μμ°μκ° μ νμλ€.
μ§λμ κ° μΉΈμμ μ, ν, μ’, μ°λ‘ μ°κ²°λλ μΉΈμ μ ν μ«μλ₯Ό λͺ¨λ ν©ν κ°λ€μ ꡬνκ³ μ νλ€.
μλ₯Ό λ€μ΄,
["X591X","X1X5X","X231X", "1XXX1"]
μ κ°μ΄ λ¬Έμμ΄λ‘ μ£Όμ΄μ§λ€λ©΄ μ΄λ λ€μκ³Ό κ°μ μ§λλ₯Ό λνλ΄λ κ²μ΄λ€.μ°κ²°λ μΉΈλ€μ κ°μ ν©μΉλ©΄ λ€μκ³Ό κ°μΌλ©°
μ΄λ
[1, 1, 27]
λ‘ μ μ₯ν μ μλ€.μ§λλ₯Ό λνλ΄λ λ¬Έμμ΄ λ°°μ΄ mapsκ° λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, μ«μ μΉΈλ€μ μ°κ²°ν΄ μ»μ μ μλ μ«μλ€μ λ°°μ΄μ μ€λ¦μ°¨μμΌλ‘ λ΄μ return νλ solution ν¨μλ₯Ό μμ±νλΌ. λ§μ½ μ«μ μΉΈμ΄ μ‘΄μ¬νμ§ μλλ€λ©΄ -1μ λ°°μ΄μ λ΄μ return νλΌ.
2. μ€λͺ
κ·Έλν μ°κ²° μμ κ΄λ ¨ λ¬Έμ μμ μ μ μλ€. μ°κ²°μμ κ΄λ ¨λ λ¬Έμ λ μ¬λ¬ μ νμ΄ μμΌλ©°, μ¬κΈ°μλ κ° μ°κ²°μμμ 'ν¬κΈ°'λ₯Ό ꡬνλ λ¬Έμ λ‘ ν΄μν μ μλ€.
κ° μΉΈ(μ¦, κ·Έλν λ Έλ)μ ν¬κΈ°κ° 1 ~ 9 μ¬μ΄μ΄κ³ , μ/ν/μ’/μ°λ‘ λ Έλκ° μ°κ²°λ κ·Έλνλ‘ ν΄μν μ μλ€.
λ¬Έμ ν΄μμ΄ μλ£λλ€λ©΄, μ½λλ μ½κ² ꡬνν μ μλ€.
무μΈλκ° μλ μΉΈ(λ Έλ)μ΄λΌλ©΄,
Merge
ν¨μλ₯Ό νΈμΆν΄ ν΄λΉ ν¨μκ° λ°ννλ μ°κ²° μμμ ν¬κΈ°λ₯ΌDaysofStay
리μ€νΈμ μ μ₯νλ€.μ°κ²°μμμ ν¬κΈ°λ₯Ό λ°ννλ
Merge
ν¨μλ DFSλ₯Ό κΈ°λ°μΌλ‘ μμ±νλ€.νμ¬ (x, y) μ’νκ° κ·Έλ¦¬λ λ²μλ₯Ό λ²μ΄λ¬κ±°λ 무μΈλ('X') μΉΈμ΄λΌλ©΄ 0μ λ°ννκ³ , μλλΌλ©΄ νμ¬ μΉΈμ μ«μ(
Maps[x][y]
, charμ΄λ―λ‘ intλ‘ λ³ν νμ)μ μΈμ ν 4λ°©ν₯ μΉΈμ λν μ¬κ· νΈμΆμ κ²°κ³Όλ₯Ό λͺ¨λ λν΄ λ°ννλ€.μ΄ λ, νμ¬ μΉΈμ λ μ΄μ λ°©λ¬Ένμ§ μλλ‘ λ§νΉ(무μΈλ, μ¦ 'X' κ°μΌλ‘ λ³κ²½)ν΄μ€λ€.
μ§λμ λͺ¨λ μΉΈμ λν΄ κ²μ¬λ₯Ό μλ£νλλ°λ
DaysofStay
리μ€νΈκ° λΉμ΄μλ€λ©΄, μ«μ μΉΈμ΄ μ‘΄μ¬νμ§ μλ κ²μ΄λ―λ‘ [-1]μ λ°ννλ€.μλλΌλ©΄ μ€λ¦μ°¨μ μ λ ¬ ν λ°ννλ€.
μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μ μ΄λ²μλ λ무 빨리 νμ΄λ²λ¦°...