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

63-tgyuuAn #216

Merged
merged 1 commit into from
Jul 14, 2024
Merged

63-tgyuuAn #216

merged 1 commit into from
Jul 14, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Jul 10, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋กœ๊ณ 

๋Šฆ์–ด์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค ํžˆํžˆ..

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

20๋ถ„

image

์ง !!!!!

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

image




๊ณจ๋“œ 2๋ผ์„œ ๊ณจ๋ž๋Š”๋ฐ ์งฑ ์ด์ง€ํ”ผ์ง€ ํ–ˆ์Šต๋‹ˆ๋‹ค!










ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๐ŸŒŸ

1. ์ฃผ์–ด์ง„ ์ง์‚ฌ๊ฐํ˜• ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ, ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ ๊ธฐ๋กํ•ด๋‘ก๋‹ˆ๋‹ค.

can_visit = set()
for _ in range(N):
    x1, y1, x2, y2 = map(int,input().split())

    x1 *= 2
    y1 *= 2
    x2 *= 2
    y2 *= 2

    for draw_x in range(x1, x2+1):
        can_visit.add((draw_x, y1))
        can_visit.add((draw_x, y2))

    for draw_y in range(y1, y2+1):
        can_visit.add((x1, draw_y))
        can_visit.add((x2, draw_y))




2. ์ดํ›„ ์‹œ์ž‘์ง€์ ์„ (0, 0) ์œผ๋กœ ์‹œ์ž‘ํ•˜์—ฌ์„œ BFS๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ™์€ ๊ทธ๋ฃน์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋กœ์ง์œผ๋กœ ํ•œ ๋ถ“์œผ๋กœ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋„ํ˜•์€ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค.




3. ์—ฐํ•„์„ ๋•Œ์•ผ ํ•  ๋•Œ ๋งˆ๋‹ค answer ์„ +1 ์”ฉ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
can_visit.add((0, 0))
draw_coordinate = deque(can_visit)
answer = 0
while draw_coordinate:
    deq = deque()

    if (0, 0) in can_visit: 
        deq.append((0, 0))
        can_visit.discard((0, 0))

    else:
        now_x, now_y = draw_coordinate.popleft()
        if (now_x, now_y) not in can_visit: continue

        deq.append((now_x, now_y))
        can_visit.discard((now_x, now_y))

    while deq:
        now_x, now_y = deq.popleft()

        for dir in range(4):
            new_x = now_x + dx[dir]
            new_y = now_y + dy[dir]

            if (new_x, new_y) not in can_visit: continue

            can_visit.discard((new_x, new_y))
            deq.append((new_x, new_y))

    answer += 1




4. ๋งˆ์ง€๋ง‰์— answer ์—์„œ -1 ํ•ด์ค๋‹ˆ๋‹ค!

์™œ๋ƒํ•˜๋ฉด ๊ฑฐ๋ถ์ด๊ฐ€ ๋กœ๊ณ ๋ฅผ ์™„์„ฑํ•˜๋Š” ์‹œ์ ์—์„œ์˜ answer ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋งˆ์ง€๋ง‰์— ๋‹ค ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ ๋˜ ๊ณ ๊ฐœ๋ฅผ ๋“ค ํ•„์š” ์—†๊ธฐ ๋–„๋ฌธ์ž…๋‹ˆ๋‹ค. ใ…‹ใ…Œใ…‹ใ…Œใ…‹ใ…Œ

image







์ด ๋ฌธ์ œ์˜ ์ˆ˜๋„ ์ฝ”๋“œ๋Š” ์œ„๊ฐ€ ๋‹ค ์ž…๋‹ˆ๋‹ค...

ํ•˜์ง€๋งŒ ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ขŒํ‘œ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ๋•Œ, *2๋ฅผ ํ•ด์„œ ์ €์žฅํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ ๋ฟ ๋งŒ์•„๋‹ˆ๋ผ ์ขŒํ‘œ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ *2๋ฅผ ํ•ด์„œ ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ์ •๋ง์ •๋ง์ •๋ง์ •๋ง ๋งŽ์ด ์“ฐ์ด๋Š” ๋ฐฉ๋ฒ•์ด๋‹ˆ ๊ผญ ์ฒดํ™”ํ•˜์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ขŒํ‘œ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ๋•Œ, *2๋ฅผ ํ•ด์„œ ์ €์žฅํ•˜๋Š” ์ด์œ ๋Š” #104 ์— ์žˆ์Šต๋‹ˆ๋‹ค!




๊ฒฐ๋ก ์ ์œผ๋กœ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์„ ์ฒดํ™”ํ•˜์…จ์œผ๋ฉด ์—ฌ๋Ÿฌ ์ขŒํ‘œ๊ณ„ ๋ฌธ์ œ๋ฅผ ์„ญ๋ ตํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ˜€๐Ÿ˜‹๐Ÿ˜€

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Jul 10, 2024
@tgyuuAn tgyuuAn requested review from alstjr7437 and SeongHoonC July 10, 2024 22:51
@tgyuuAn tgyuuAn self-assigned this Jul 10, 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.

์ €๋Š” ๋‹น์—ฐํžˆ Union Find ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ..

  1. ์ขŒํ‘œ๋ฅผ 2๋ฐฐ๋กœ ๋งŒ๋“ค๊ณ  ์‚ฌ์ด๋ฅผ 1๋กœ ์ถ”๊ฐ€.
  2. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ ๊ฐ€์ง€ ๋ชปํ•˜๋Š” ๊ณณ์„ ๊ตฌ๋ถ„ํ•˜๊ณ  ๊ฒน์น˜๋Š”์ง€ ํŒ๋‹จ.
  3. ๊ทธ ํ›„ BFS ๋กœ ํ’€๊ธฐ

์ข‹์€ ์ธ์‚ฌ์ดํŠธ ์–ป๊ณ  ๊ฐ‘๋‹ˆ๋‹ค

import java.io.BufferedReader
import java.io.InputStreamReader

private var squares: List<Square> = mutableListOf(Square(0, 0, 0, 0))
private lateinit var parents: MutableList<Int>

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    repeat(n) {
        val (x1, y1, x2, y2) = br.readLine().split(" ").map { it.toInt() }
        squares += Square(x1, y1, x2, y2)
    }

    parents = MutableList(n + 1) { it }
    for (i in 0 until n) {
        for (j in i..n) {
            if (compare(squares[i], squares[j])) {
                union(i, j)
            }
        }
    }
    println(parents.map { findParent(it) }.toSet().size - 1)
}

private fun union(i: Int, j: Int) {
    val pi = findParent(i)
    val pj = findParent(j)
    if (pi < pj) {
        parents[pj] = pi
    } else if (pi > pj) {
        parents[pi] = pj
    }
}

private fun findParent(i: Int): Int {
    if (parents[i] == i)
        return i
    return findParent(parents[i])
}

private fun compare(a: Square, b: Square): Boolean {
    // a > b
    if (a.x1 < b.x1 && a.x2 > b.x2 && a.y1 < b.y1 && a.y2 > b.y2) {
        return false
    }

    // b > a
    if (b.x1 < a.x1 && b.x2 > a.x2 && b.y1 < a.y1 && b.y2 > a.y2) {
        return false
    }

    // ์‚ฌ๋ฐฉ
    if (a.x1 > b.x2 || a.x2 < b.x1 || a.y1 > b.y2 || a.y2 < b.y1) {
        return false
    }
    return true
}

data class Square(
    val x1: Int,
    val y1: Int,
    val x2: Int,
    val y2: Int,
)

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Jul 14, 2024

์ €๋Š” ๋‹น์—ฐํžˆ Union Find ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ..

  1. ์ขŒํ‘œ๋ฅผ 2๋ฐฐ๋กœ ๋งŒ๋“ค๊ณ  ์‚ฌ์ด๋ฅผ 1๋กœ ์ถ”๊ฐ€.
  2. ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ ๊ฐ€์ง€ ๋ชปํ•˜๋Š” ๊ณณ์„ ๊ตฌ๋ถ„ํ•˜๊ณ  ๊ฒน์น˜๋Š”์ง€ ํŒ๋‹จ.
  3. ๊ทธ ํ›„ BFS ๋กœ ํ’€๊ธฐ

์ข‹์€ ์ธ์‚ฌ์ดํŠธ ์–ป๊ณ  ๊ฐ‘๋‹ˆ๋‹ค

import java.io.BufferedReader
import java.io.InputStreamReader

private var squares: List<Square> = mutableListOf(Square(0, 0, 0, 0))
private lateinit var parents: MutableList<Int>

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    repeat(n) {
        val (x1, y1, x2, y2) = br.readLine().split(" ").map { it.toInt() }
        squares += Square(x1, y1, x2, y2)
    }

    parents = MutableList(n + 1) { it }
    for (i in 0 until n) {
        for (j in i..n) {
            if (compare(squares[i], squares[j])) {
                union(i, j)
            }
        }
    }
    println(parents.map { findParent(it) }.toSet().size - 1)
}

private fun union(i: Int, j: Int) {
    val pi = findParent(i)
    val pj = findParent(j)
    if (pi < pj) {
        parents[pj] = pi
    } else if (pi > pj) {
        parents[pi] = pj
    }
}

private fun findParent(i: Int): Int {
    if (parents[i] == i)
        return i
    return findParent(parents[i])
}

private fun compare(a: Square, b: Square): Boolean {
    // a > b
    if (a.x1 < b.x1 && a.x2 > b.x2 && a.y1 < b.y1 && a.y2 > b.y2) {
        return false
    }

    // b > a
    if (b.x1 < a.x1 && b.x2 > a.x2 && b.y1 < a.y1 && b.y2 > a.y2) {
        return false
    }

    // ์‚ฌ๋ฐฉ
    if (a.x1 > b.x2 || a.x2 < b.x1 || a.y1 > b.y2 || a.y2 < b.y1) {
        return false
    }
    return true
}

data class Square(
    val x1: Int,
    val y1: Int,
    val x2: Int,
    val y2: Int,
)

์ขŒํ‘œ 2๋ฐฐํ•˜๋Š” ๊ฑฐ ์ขŒํ‘œ๊ณ„ ๋ฌธ์ œ์—์„œ ์—„์ฒญ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋‹ˆ ๊ผญ ๊ธฐ์–ตํ•˜๊ธฐ!!

@tgyuuAn tgyuuAn merged commit 2472c41 into main Jul 14, 2024
9 checks passed
@tgyuuAn tgyuuAn deleted the 63-tgyuuAn branch July 14, 2024 11:40
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.

2 participants