๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ˜ | Gold

[Kotlin, G5] ๋ฐฑ์ค€ 1711๋ฒˆ ์ง๊ฐ์‚ผ๊ฐํ˜•

by immgga 2024. 10. 1.

์ถœ์ฒ˜: unsplash.com

 

์ง๊ฐ์‚ผ๊ฐํ˜•(1711๋ฒˆ)

Gold 5

#๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜ #๊ธฐํ•˜ํ•™ #ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

๊ฐ ์ฃผ์–ด์ง€๋Š” ์ ์—์„œ ๋ฌด์ž‘์œ„๋กœ ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ 3๊ฐœ๋ฅผ ๊ณจ๋ผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

์ ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1500๊ฐœ๊นŒ์ง€ ์žˆ๊ณ , ์‹œ๊ฐ„์ œํ•œ์ด 5์ดˆ์ด๋‹ค.

๊ฒฐ์ •์ ์œผ๋กœ ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ถ„๋ฅ˜๋˜์–ด ์žˆ๋‹ค.

์ด๋Š” ์ตœ๋Œ€ 1500๊ฐœ์˜ ์ ์—์„œ 3๊ฐœ๋ฅผ ๊ณจ๋ผ์•ผ ํ•  ๋•Œ, ๋ฌด์ง€์„ฑ ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋œป์ด๋‹ค.

 

๋ฌด์ง€์„ฑ ํƒ์ƒ‰์—์„œ ๋‚˜์˜จ 3๊ฐœ์˜ ์ ์—์„œ ๊ฐ ์  ์‚ฌ์ด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๊ณ  ๊ทธ ๊ธธ์ด๋ฅผ ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ ๊ณต์‹์„ ์ด์šฉํ•ด ์ง๊ฐ์‚ผ๊ฐํ˜•์ธ์ง€ ํŒŒ์•…ํ•˜๋ฉด ๋œ๋‹ค.

๋‘ ์  (x1, y1), (x2, y2)๊ฐ€ ์žˆ์„ ๋•Œ, ๋‘ ์ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋Š”

(x2 - x1) * (x2 - x1)์˜ ์ œ๊ณฑ + (y2 - y1) * (y2 - y1)์˜ ์ œ๊ณฑ์— ๋ฃจํŠธ๋ฅผ ์”Œ์šด ๊ฐ’์ด ๋œ๋‹ค.

 

๋‘ ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ํ”ผํƒ€๊ณ ๋ผ์Šค์˜ ์ •๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val cnt = readLine().toInt()
    val points = mutableListOf<List<Int>>()
    var triangles = 0

    for (i in 0 until cnt) {
        points.add(readLine().split(" ").map { it.toInt() })
    }

    for (i in 0 until points.size) {
        for (j in i + 1 until points.size) {
            for (k in j + 1 until points.size) {
                val distance1 = getPointDistance(points[i][0], points[i][1], points[j][0], points[j][1])
                val distance2 = getPointDistance(points[j][0], points[j][1], points[k][0], points[k][1])
                val distance3 = getPointDistance(points[k][0], points[k][1], points[i][0], points[i][1])

                if (distance1 == distance2 + distance3 || distance2 == distance1 + distance3 || distance3 == distance1 + distance2) {
                    triangles++
                }
            }
        }
    }

    println(triangles)
}

private fun getPointDistance(x1: Int, y1: Int, x2: Int, y2: Int): Long {
    val distanceX = ((x2 - x1).toLong() * (x2 - x1).toLong())
    val distanceY = ((y2 - y1).toLong() * (y2 - y1).toLong())
    return distanceX + distanceY
}

 

๋ฌธ์ œ ํ’€์ด

์ ๋“ค์˜ ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋“ค์–ด๊ฐ„ ์ขŒํ‘œ๋“ค ์ค‘ 3๊ฐœ๋ฅผ ์ค‘๋ณต ์—†์ด ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ 3์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•ด ์ฃผ๊ณ , ์„ ํƒ๋œ 3๊ฐœ์˜ ์ ์„ ์ด์šฉํ•ด

๊ฐ ์ ์˜ ๊ฑฐ๋ฆฌ 3๊ฐœ๋ฅผ ๊ตฌํ•œ ๋‹ค์Œ, ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ๋ฅผ ๋Œ€์ž…ํ•ด์„œ ์ง๊ฐ์‚ผ๊ฐํ˜•์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ค€๋‹ค.

 

ํ”ผํƒ€๊ณ ๋ผ์Šค ๊ณต์‹์ด, ๊ฐ€์žฅ ๊ธด ๋ณ€์˜ ๊ธธ์ด์˜ ์ œ๊ณฑ์ด ๋‚˜๋จธ์ง€ ๋ณ€์˜ ๊ธธ์ด์˜ ์ œ๊ณฑ์ด๋‹ค.๋‘ ์ ์˜ ๊ฑฐ๋ฆฌ ๊ณต์‹์—์„œ๋Š” ๋งˆ์ง€๋ง‰์— ๋ฃจํŠธ๋ฅผ ์”Œ์›Œ์ค˜์•ผ ํ•œ๋‹ค.๋ฃจํŠธ๋ฅผ ์”Œ์šฐ๊ฒŒ ๋˜๋ฉด ์†Œ์ˆ˜๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณ„์‚ฐ์ด ๋ฒˆ๊ฑฐ๋กœ์›Œ์ง€๊ณ  ๋ฏธ์„ธํ•œ ์ˆ˜์˜ ์ฐจ์ด๋กœ ์˜ค๋‹ต์ด ๋‚  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—๋‘ ์  ์‚ฌ์ด์˜ ๊ธธ์ด๋Š” ๋ฃจํŠธ๋ฅผ ์ œ๊ฑฐํ•œ ์ƒํƒœ๋กœ long ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด ์ œ๊ณฑํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ ๊ณต์‹์„ ์ด์šฉํ•  ๋•Œ ์ œ๊ณฑ์„ ํ•  ํ•„์š” ์—†์ด ๋ง์…ˆ๋งŒ ์ง„ํ–‰ํ•ด์„œ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.๊ทธ๋Ÿฌ๋ฉด ์ •ํ™•ํ•˜๊ฒŒ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

์ด๊ฑฐ๋„ ํฌ๊ฒŒ ์–ด๋ ต์ง€ ์•Š๋‹ค.

๋ฌด์ง€์„ฑ ํƒ์ƒ‰์œผ๋กœ ์  3๊ฐœ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ํŽธํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

์ „์ฒด ํƒ์ƒ‰์œผ๋กœ ๊ตฌํ•œ 3๊ฐœ์˜ ์ ์œผ๋กœ ํ”ผํƒ€๊ณ ๋ผ์Šค ์ •๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๋‚ด ๊ธฐ์ค€ ์ฒด๊ฐ ๋‚œ์ด๋„๋Š” ์‹ค๋ฒ„ 4~์‹ค๋ฒ„ 3์ด๋‹ค.

728x90