-
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
56-tgyuyAn #193
56-tgyuyAn #193
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.
μ°μ μμ νλ₯Ό μμ°λκΉ μ΅μκ°μ κ³μ μκ°νλ€κ³ 무μν λ§μ λ°λ‘λ₯Ό λ§λ¬λ€μ..νν
import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.math.min
lateinit var castle: Array<Array<Int>>
private val dx_1 = listOf(0, 1, -1, 0)
private val dy_1 = listOf(1, 0, 0, -1)
const val INF = 987_654_321
var gramTime = INF
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (n, m, T) = br.readLine().split(" ").map { it.toInt() }
castle = Array(n) { Array(m) { 0 } }
for (i in 0 until n) {
val line = br.readLine().split(" ").map { it.toInt() }
line.forEachIndexed { index, value ->
castle[i][index] = if (value == 0) INF else if (value == 2) -2 else value
}
}
bfs(0 to 0, n, m, T)
val result = min(gramTime, castle[n - 1][m - 1])
println(if (result == INF || result > T) "Fail" else result)
}
// -2 κ²
fun bfs(start: Pair<Int, Int>, n: Int, m: Int, T: Int) {
val q = ArrayDeque<Pair<Int, Int>>()
q.add(start)
castle[start.first][start.second] = 0
while (q.isNotEmpty()) {
val (x, y) = q.removeFirst()
for (i in 0..3) {
val nextX = x + dx_1[i]
val nextY = y + dy_1[i]
// λ²μλ₯Ό λμ΄κ°
if (nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) {
continue
}
// μΉΌ μ£Όμμ λ
if (castle[nextX][nextY] == -2) {
val distance = (n - 1) - nextX + (m - 1) - nextY
val nowTime = castle[x][y] + 1
gramTime = min(gramTime, nowTime + distance)
continue
}
// μ΄λ―Έ λ°©λ¬Ένκ±°λ λ²½
if (castle[nextX][nextY] != INF) {
continue
}
// μ΅μκ° μ
λ°μ΄νΈ && νμ λ£κΈ°
castle[nextX][nextY] = min(castle[nextX][nextY], castle[x][y] + 1)
q.add(nextX to nextY)
}
}
}
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.
μ½λ λ€ μ§κ³ 보λκΉ κ·Έλ₯ νκ·λ μ½λλ μμ κ°λ€μ©
heapμ΄λ dequeλ λ°μ μ°¨μ΄κ° μλ κ² κ°μμ
μ¬λ°κ² λ¬Έμ νκ³ κ°λλ€ ~~ μκ³ νμ ¨μ΅λλ€ ~~~ππ
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()
N, M, T = map(int, input().split())
maze = [list(map(int, input().split())) for _ in range(N)]
visited = [[0 for _ in range(M)] for _ in range(N)]
direc = [(1,0),(-1,0),(0,1),(0,-1)]
time = 9999999
q = deque()
q.append((0, 0, 0))
visited[0][0] = 1
while q:
cx, cy, c_time = q.popleft()
if cx == N-1 and cy == M-1:
time = min(c_time, time)
break
if maze[cx][cy] == 2:
time = min(time, c_time + abs(N-1-cx) + abs(M-1-cy))
for dx, dy in direc:
nx = cx + dx
ny = cy + dy
if 0 <= nx < N and 0 <= ny < M and not visited[nx][ny] and maze[nx][ny] != 1:
q.append((nx, ny, c_time + 1))
visited[nx][ny] = 1
if time <= T:
print(time)
else:
print("Fail")
|
||
visited.add((new_row, new_col)) | ||
|
||
print(answer) if answer <= T else print("Fail") |
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.
μ€νΈ? μ΄λ κ² ifλ¬Έμ λ€μ μ¨λ λλ€μ γ·γ· νλ μμκ°λλ€
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.
@H0ngJu λ§ κ·Έλ κ² μ μ©ν 건 μλλ° μΌν μ°μ°μ λλμΌλ‘ μΈ μ μμ΄μ ν€νΌ
BFS κΈ°λ³Έλ¬Έμ λΌμ μ½κ² νΌ κ² κ°λ€μ!!! import sys
from collections import deque
input = sys.stdin.readline
n, m, t = map(int, input().split())
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
graph = [list(map(int, input().split())) for _ in range(n)]
visited = [[0] * m for _ in range(n)]
queue = deque()
def bfs():
gram = 10001
queue.append((0, 0))
visited[0][0] = 1
while queue:
x, y = queue.popleft()
if (x, y) == (n-1, m-1): # λͺ©μ μ§ (n-1, m-1)
return min(visited[x][y] - 1, gram)
if graph[x][y] == 2: # κ²μ μ°Ύμ κ²½μ°
gram = visited[x][y] - 1 + (n-1-x) + (m-1-y)
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny]:
if graph[nx][ny] == 0 or graph[nx][ny] == 2:
visited[nx][ny] = visited[x][y] + 1
queue.append((nx, ny))
return gram
res = bfs()
if res > t:
print('Fail')
else:
print(res) |
|
||
dx = [0, 0, -1, 1] | ||
dy = [-1, 1, 0, 0] | ||
heap = [(0, 0, 0)] |
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.
μ¬μ€ μ²μμ νμ΄μ λ§μλκ³ Heapμ μ°μκΈΈλ λ€λ₯Έ νμ΄ λ°©λ²μ μ΄ν΄νλ €κ³ μ΄....π§ νμ΅λλ€.
κ·Έλ°λ° κ·Έλ₯ BFSλ‘ νλ©΄ ν리길λ heapμΌλ‘λ ν μ μκ² κ΅¬λ νκ³ λμ΄κ°μ΄μ!!
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.
@alstjr7437 μλ―Έμλ μ§μ΄μμλλ€... ...
π λ¬Έμ λ§ν¬
곡주λμ ꡬν΄λΌ!
βοΈ μμλ μκ°
40λΆ
β¨ μλ μ½λ
μλμ κ°μ μ± μ λ³΄κ° μ£Όμ΄μ‘μ λ,
(1, 1)
μμ μμνμ¬(N, M)
μ μλ 곡주λμ ꡬνλ©΄ λλ λ¬Έμ ...!μ΄μ νμμμλ λ²½μ λ«μ§ λͺ»νλλ°,
μ±μ νλ μλ μ μ€μ κ² κ·Έλμ μ»κ² λλ©΄ λͺ¨λ λ²½μ νκ΄΄νκ³ μ§λκ° μ μκ² λλ€...!
κ·Όλ° μ΄μ 곡주λμ΄ μ΄μμμ μ ν μκ°μ΄ μμ΄μ ν΄λΉ μ ν μκ° μμ 곡주λ₯Ό ꡬν μ μμΌλ©΄ μ΅λ¨ μκ°μ μΆλ ₯νκ³ ,
λ¬΄μ¨ μλ₯Ό μ¨μλΌλ μ ν μκ° μμ ꡬν μ μμΌλ©΄
"Fail"
μ μΆλ ₯νλ©΄ λλ λ¬Έμ μ΄λ€.μ΄ λ¬Έμ λ₯Ό 보μλ§μ 2κ°μ§ κ²½μ°λ‘ λλ μ μκ°νλ€.
1. μ μ€μ κ² κ·Έλμ μ»μ΄μ λ²½μ λ«μμ λ μ΅λ¨ μκ°
2. κ²μ ꡬνμ§ λ§κ³ μ μμ μΈ κΈΈλ‘λ§ κ°μ λμ μ΅λ¨ μκ°
μ¦, κ²μ μ»κ³ λ²½μ λ«κ³ κ° λμ μ΅λ¨ μκ°κ³Ό κ²μ μ»μ§ μκ³ μ μμ μΈ κΈΈλ‘λ§ κ°μ λμ μ΅λ¨ μκ°μ κ°κ° ꡬν λ€,
μ΅μ’ μ μΈ μ΅λ¨ μκ°μ΄ 곡주λμ΄ μ΄μμμ μκ° λ³΄λ€ μμΌλ©΄ μΆλ ₯ μλλ©΄ Failμ μΆλ ₯νλ©΄ λλ κ°λ¨ν λ¬Έμ !
κ·Όλ° μ΄μ λλ μΌμμΌ μμΉ¨μ λ°μ΄νΈ λ¬Έμ μ λΉμ·ν λ¬Έμ μΈ μ€ μκ³ ,
νμ μ¨μ νμμ§λ§ μ΅μ’ μ μΈ μ½λλ κ²°κ΅ BFS μλ€.
κ·Έλ₯ visitedμμ μλ κΈΈ λ€μ κ°μ§ μκ² μ€μ νκ³ ,
μ μ€μ κ² κ·Έλμ΄ μλ μμΉμ λμ°©νμ κ²½μ°
κ²μ μ»μΌλ¬ κ° λ κΉμ§μ μκ°
+(M- ν΄λΉ μμΉ x μ’ν) + (N - ν΄λΉ μμΉ y μ’ν)
λ§ λνλ©΄ κ²μ μ»μμ λμ μ΅λ¨ μκ°μ ꡬν μ μμκΈ° λλ¬Έμ΄λ€.μ κΉμ§ μ§ννλ©΄ 골λ 5λ¬Έμ μμ μ€λ² μμ€μ μΌλ°μ μΈ BFS λ¬Έμ λ‘ λ°λμλ€.
κ·ΈλΌ...
μμμμλ΅€!
π μλ‘κ² μκ²λ λ΄μ©