Skip to content
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

Merged
merged 1 commit into from
May 26, 2024
Merged

56-tgyuyAn #193

merged 1 commit into from
May 26, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented May 22, 2024

πŸ”— 문제 링크

κ³΅μ£Όλ‹˜μ„ ꡬ해라!

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

40λΆ„

✨ μˆ˜λ„ μ½”λ“œ

μ•„λž˜μ™€ 같은 μ„± 정보가 μ£Όμ–΄μ‘Œμ„ λ•Œ,

(1, 1) μ—μ„œ μ‹œμž‘ν•˜μ—¬ (N, M) 에 μžˆλŠ” κ³΅μ£Όλ‹˜μ„ κ΅¬ν•˜λ©΄ λ˜λŠ” 문제...!

image







이제 ν‰μƒμ‹œμ—λŠ” 벽을 λš«μ§€ λͺ»ν•˜λŠ”데,

성에 ν•˜λ‚˜ μžˆλŠ” μ „μ„€μ˜ κ²€ κ·ΈλžŒμ„ μ–»κ²Œ 되면 λͺ¨λ“  벽을 νŒŒκ΄΄ν•˜κ³  μ§€λ‚˜κ°ˆ 수 있게 λœλ‹€...!







근데 이제 κ³΅μ£Όλ‹˜μ΄ μ‚΄μ•„μžˆμ„ μ œν•œ μ‹œκ°„μ΄ μžˆμ–΄μ„œ ν•΄λ‹Ή μ œν•œ μ‹œκ°„ μ•ˆμ— 곡주λ₯Ό ꡬ할 수 있으면 μ΅œλ‹¨ μ‹œκ°„μ„ 좜λ ₯ν•˜κ³ ,

무슨 수λ₯Ό μ¨μ„œλΌλ„ μ œν•œ μ‹œκ°„ μ•ˆμ— ꡬ할 수 μ—†μœΌλ©΄ "Fail" 을 좜λ ₯ν•˜λ©΄ λ˜λŠ” λ¬Έμ œμ΄λ‹€.







이 문제λ₯Ό 보자마자 2가지 경우둜 λ‚˜λˆ μ„œ μƒκ°ν–ˆλ‹€.

1. μ „μ„€μ˜ κ²€ κ·ΈλžŒμ„ μ–»μ–΄μ„œ 벽을 λš«μ—ˆμ„ λ•Œ μ΅œλ‹¨ μ‹œκ°„

2. 검을 κ΅¬ν•˜μ§€ 말고 정상적인 길둜만 갔을 λ•Œμ˜ μ΅œλ‹¨ μ‹œκ°„







즉, 검을 μ–»κ³  벽을 뚫고 갈 λ•Œμ˜ μ΅œλ‹¨ μ‹œκ°„κ³Ό 검을 얻지 μ•Šκ³  정상적인 길둜만 갔을 λ•Œμ˜ μ΅œλ‹¨ μ‹œκ°„μ„ 각각 κ΅¬ν•œ λ’€,

μ΅œμ’…μ μΈ μ΅œλ‹¨ μ‹œκ°„μ΄ κ³΅μ£Όλ‹˜μ΄ μ‚΄μ•„μžˆμ„ μ‹œκ°„ 보닀 μž‘μœΌλ©΄ 좜λ ₯ μ•„λ‹ˆλ©΄ Fail을 좜λ ₯ν•˜λ©΄ λ˜λŠ” κ°„λ‹¨ν•œ 문제!







근데 이제 λ‚˜λŠ” μΌμš”μΌ μ•„μΉ¨μ˜ 데이트 λ¬Έμ œμ™€ λΉ„μŠ·ν•œ 문제인 쀄 μ•Œκ³ ,

νž™μ„ μ¨μ„œ ν’€μ—ˆμ§€λ§Œ μ΅œμ’…μ μΈ μ½”λ“œλŠ” κ²°κ΅­ BFS μ˜€λ‹€.







κ·Έλƒ₯ visitedμ—μ„œ μ™”λ˜ κΈΈ λ‹€μ‹œ 가지 μ•Šκ²Œ μ„€μ •ν•˜κ³ ,

μ „μ„€μ˜ κ²€ 그람이 μžˆλŠ” μœ„μΉ˜μ— λ„μ°©ν–ˆμ„ 경우

검을 μ–»μœΌλŸ¬ 갈 λ•Œ κΉŒμ§€μ˜ μ‹œκ°„ + (M- ν•΄λ‹Ή μœ„μΉ˜ x μ’Œν‘œ) + (N - ν•΄λ‹Ή μœ„μΉ˜ y μ’Œν‘œ) 만 λ”ν•˜λ©΄ 검을 μ–»μ—ˆμ„ λ•Œμ˜ μ΅œλ‹¨ μ‹œκ°„μ„ ꡬ할 수 μžˆμ—ˆκΈ° λ•Œλ¬Έμ΄λ‹€.







μœ„ κΉŒμ§€ μ§„ν–‰ν•˜λ©΄ κ³¨λ“œ 5λ¬Έμ œμ—μ„œ 싀버 μˆ˜μ€€μ˜ 일반적인 BFS 문제둜 λ°”λ€Œμ—ˆλ‹€.




그럼...

μ•„μžμ•„μžλ΅€!

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@tgyuuAn tgyuuAn added tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. μž‘μ„± 쀑 ⏱️ labels May 22, 2024
@tgyuuAn tgyuuAn self-assigned this May 22, 2024
Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μš°μ„ μˆœμœ„ 큐λ₯Ό μ•ˆμ“°λ‹ˆκΉ μ΅œμ†Œκ°’μ„ 계속 μƒκ°ν•œλ‹€κ³  무수히 λ§Žμ€ λ°˜λ‘€λ₯Ό λ§Œλ‚¬λ„€μš”..ν•˜ν•˜

image
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)
        }
    }
}

Copy link
Collaborator

@H0ngJu H0ngJu left a 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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였호? μ΄λ ‡κ²Œ if문을 뒀에 써도 λ˜λ„€μš” γ„·γ„· ν•˜λ‚˜ μ•Œμ•„κ°‘λ‹ˆλ‹€

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@H0ngJu 막 κ·Έλ ‡κ²Œ μœ μš©ν•œ 건 μ•„λ‹Œλ° μ‚Όν•­ μ—°μ‚°μž λŠλ‚ŒμœΌλ‘œ μ“Έ 수 μžˆμ–΄μš” ν‚€ν‚Ό

@alstjr7437
Copy link
Member

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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사싀 μ²˜μŒμ— ν’€μ–΄μ„œ λ§žμ•„λ†“κ³  Heap을 μ“°μ‹œκΈΈλž˜ λ‹€λ₯Έ 풀이 방법을 μ΄ν•΄ν•˜λ €κ³  μ–΄....🧐 ν–ˆμŠ΅λ‹ˆλ‹€.
그런데 κ·Έλƒ₯ BFS둜 ν’€λ©΄ ν’€λ¦¬κΈΈλž˜ heapμœΌλ‘œλ„ ν’€ 수 μžˆκ² κ΅¬λ‚˜ ν•˜κ³  λ„˜μ–΄κ°”μ–΄μš”!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alstjr7437 μ˜λ―Έμ—†λŠ” μ§“μ΄μ—ˆμ”λ‹ˆλ‹€... ...

@tgyuuAn tgyuuAn merged commit 8217773 into main May 26, 2024
5 checks passed
@tgyuuAn tgyuuAn deleted the 56-tgyuuAn branch May 26, 2024 04:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. 리뷰 μ™„λ£Œ βœ”οΈ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants