๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail
 

20057๋ฒˆ: ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ํ† ๋„ค์ด๋„

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๊ฐ€ ํ† ๋„ค์ด๋„๋ฅผ ๋ฐฐ์› ๊ณ , ์˜ค๋Š˜์€ ํ† ๋„ค์ด๋„๋ฅผ ํฌ๊ธฐ๊ฐ€ N×N์ธ ๊ฒฉ์ž๋กœ ๋‚˜๋ˆ„์–ด์ง„ ๋ชจ๋ž˜๋ฐญ์—์„œ ์—ฐ์Šตํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์œ„์น˜ (r, c)๋Š” ๊ฒฉ์ž์˜ rํ–‰ c์—ด์„ ์˜๋ฏธํ•˜๊ณ , A[r][c]๋Š” (r, c)์— ์žˆ๋Š” ๋ชจ๋ž˜์˜ ์–‘์„

www.acmicpc.net

๊ณจ๋“œ3

๋ฌธ์ œ ์„ค๋ช…

 

๋ฌธ์ œ ํ’€์ด

์ผ๋‹จ ํ† ๋„ค์ด๋„ ์ด๋™๋ถ€ํ„ฐ ๋ณด๋ฉด ์ผ์ • ๊ทœ์น™์ด ์žˆ๋‹ค.

 

n=5์ผ๋•Œ

์™ผ์ชฝ1๋ฒˆ, ์•„๋ž˜1๋ฒˆ, ์˜ค๋ฅธ์ชฝ2๋ฒˆ, ์œ„์ชฝ2๋ฒˆ, ์™ผ์ชฝ3๋ฒˆ, ์•„๋ž˜3๋ฒˆ, ์˜ค๋ฅธ์ชฝ4๋ฒˆ, ์œ„์ชฝ4๋ฒˆ, ์™ผ์ชฝ5๋ฒˆ ์ด๋™ ์ด๋ ‡๊ฒŒ (์™ผ, ์•„๋ž˜) (์˜ค๋ฅธ, ์œ„) ๊ฐ€ ์„ธํŠธ๋กœ ์›€์ง์ด๋ฉฐ ์›€์ง์ž„ ํšŸ์ˆ˜๊ฐ€ 1์”ฉ ๋Š˜๊ณ  ์žˆ๋‹ค.

var moveCount = 1
// ํ† ๋„ค์ด๋„ ์ด๋™ ๊ทœ์น™ -> ์™ผ1 ์•„1 ์˜ค2 ์œ„2 ์™ผ3 ์•„3 ์˜ค4 ์œ„4 ..

while true {
    // n์ด๋ž‘ ๊ฐ™์œผ๋ฉด left ํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜๊ณ  ๋
    if moveCount == n {
        move(.left, moveCount)
        break
    }
    
    // moveCount๊ฐ€ ํ™€์ˆ˜๋ฉด left, down
    if moveCount % 2 == 1 {
        move(.left, moveCount)
        move(.down, moveCount)
    }
    // moveCount๊ฐ€ ์ง์ˆ˜๋ฉด right, up
    else {
        move(.right, moveCount)
        move(.up, moveCount)
    }
    // ์›€์ง์ž„์ˆ˜ +1
    moveCount += 1
}

 

 

๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋น„์œจ์˜ ์œ„์น˜๋„ ๋‹ค๋ฅด๋‹ค. (ํ—ท๊ฐˆ๋ ค์„œ ์‚ฌ์ง„ ๋Œ๋ ค๊ฐ€๋ฉฐ ํ™•์ธํ–ˆ๋‹ค,,)

enum์œผ๋กœ ๋ฐฉํ–ฅ์„ ๊ด€๋ฆฌํ•˜๋ฉฐ ๊ฐ ์œ„์น˜์™€ ํผ์„ผํŠธ๋ฅผ ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค. (ratios ํ”„๋กœํผํ‹ฐ)

enum Direct: Int {
    case left
    case right
    case up
    case down
    
    // ๋ฐฉํ–ฅ๋Œ€๋กœ ์ด๋™ํ•  ๋•Œ x,y ์ขŒํ‘œ ๋ฆฌํ„ด
    var movedPoint: (x: Int, y: Int) {
        switch self {
        case .left:
            return (currentPoint.x-1, currentPoint.y)
        case .right:
            return (currentPoint.x+1, currentPoint.y)
        case .up:
            return (currentPoint.x, currentPoint.y-1)
        case .down:
            return (currentPoint.x, currentPoint.y+1)
        }
    }
    
    // ๋ฐฉํ–ฅ์— ๋”ฐ๋ฅธ x,y์ขŒํ‘œ, ํผ์„ผํŠธ๋ฅผ ๋‹ด์€ ratios
    var ratios: [(y: Int, x: Int, percent: Int)] {
        switch self {
        case .left:
            return [
                (-2, 0, 2), (-1, -1, 10), (-1, 0, 7), (-1, 1, 1), (0, -2, 5), (1, -1, 10), (1, 0, 7), (1, 1, 1), (2, 0, 2), (0, -1, 0)
            ]
        case .down:
            return [
                (2, 0, 5), (1, -1, 10), (1, 1, 10), (0, -2, 2), (0, -1, 7), (0, 1, 7), (0, 2, 2), (-1, 1, 1), (-1, -1, 1), (1, 0, 0)
            ]
        case .up:
            return [
                (-2, 0, 5), (-1, 1, 10), (-1, -1, 10), (0, 2, 2), (0, 1, 7), (0, -1, 7), (0, -2, 2), (1, -1, 1), (1, 1, 1), (-1, 0, 0)
            ]
        case .right:
            return [
                (2, 0, 2), (1, 1, 10), (1, 0, 7), (1, -1, 1), (0, 2, 5), (-1, 1, 10), (-1, 0, 7), (-1, -1, 1), (-2, 0, 2), (0, 1, 0)
            ]
        }
    }
    
}

 

 

์ด์™ธ์—๋Š” ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„๋Œ€๋กœ ๊ตฌํ˜„ํ•˜์˜€๊ณ 

์ด๋™ํ•  y์ขŒํ‘œ์˜ ๋ชจ๋ž˜์–‘์ด 0์ด๋ฉด ๋„˜์–ด๊ฐ€๊ณ ,

10๋ฏธ๋งŒ์ด๋ฉด ๋‹ค a์ขŒํ‘œ์— ๋ฟŒ๋ฆฌ๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋‹ค. (๋ชจ๋ž˜๊ฐ€ 9๋ผ๋ฉด ๊ฐ€์žฅ ํฐ ํผ์„ผํŠธ์ธ 10%์ด์–ด๋„  0์„ ๊ฐ€์ ธ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค a์ขŒํ‘œ์— ๋“ค์–ด๊ฐ)

 

์ฝ”๋“œ

import Foundation

let n = Int(readLine()!)!
var map: [[Int]] = []

for _ in 0..<n {
    let input = readLine()!.split(separator: " ").map {Int(String($0))!}
    map.append(input)
}

// ์ดˆ๊ธฐ ์ขŒํ‘œ๋Š” ๊ฐ€์šด๋ฐ
var currentPoint: (x: Int, y: Int) = (Int(n/2), Int(n/2))

enum Direct: Int {
    case left
    case right
    case up
    case down
    
    // ๋ฐฉํ–ฅ๋Œ€๋กœ ์ด๋™ํ•  ๋•Œ x,y ์ขŒํ‘œ ๋ฆฌํ„ด
    var movedPoint: (x: Int, y: Int) {
        switch self {
        case .left:
            return (currentPoint.x-1, currentPoint.y)
        case .right:
            return (currentPoint.x+1, currentPoint.y)
        case .up:
            return (currentPoint.x, currentPoint.y-1)
        case .down:
            return (currentPoint.x, currentPoint.y+1)
        }
    }
    
    // ๋ฐฉํ–ฅ์— ๋”ฐ๋ฅธ x,y์ขŒํ‘œ, ํผ์„ผํŠธ๋ฅผ ๋‹ด์€ ratios
    var ratios: [(y: Int, x: Int, percent: Int)] {
        switch self {
        case .left:
            return [
                (-2, 0, 2), (-1, -1, 10), (-1, 0, 7), (-1, 1, 1), (0, -2, 5), (1, -1, 10), (1, 0, 7), (1, 1, 1), (2, 0, 2), (0, -1, 0)
            ]
        case .down:
            return [
                (2, 0, 5), (1, -1, 10), (1, 1, 10), (0, -2, 2), (0, -1, 7), (0, 1, 7), (0, 2, 2), (-1, 1, 1), (-1, -1, 1), (1, 0, 0)
            ]
        case .up:
            return [
                (-2, 0, 5), (-1, 1, 10), (-1, -1, 10), (0, 2, 2), (0, 1, 7), (0, -1, 7), (0, -2, 2), (1, -1, 1), (1, 1, 1), (-1, 0, 0)
            ]
        case .right:
            return [
                (2, 0, 2), (1, 1, 10), (1, 0, 7), (1, -1, 1), (0, 2, 5), (-1, 1, 10), (-1, 0, 7), (-1, -1, 1), (-2, 0, 2), (0, 1, 0)
            ]
        }
    }
    
}

var answer = 0

func move(_ direct: Direct, _ count: Int) {
    let ratios = direct.ratios
    for _ in 0..<count {
        let movePoint = direct.movedPoint

        // y์˜ ์ขŒํ‘œ๊ฐ€ ๋ฒ”์œ„์— ์žˆ์œผ๋ฉด
        if (movePoint.x >= 0 && movePoint.y >= 0 && movePoint.x < n && movePoint.y < n) {
            currentPoint = movePoint
            let sand = map[currentPoint.y][currentPoint.x]
            var spreadSand = sand
            map[currentPoint.y][currentPoint.x] = 0
            
            // ๋ชจ๋ž˜์–‘์ด 0์ด๋ฉด ๋„˜์–ด๊ฐ
            if sand == 0 {
                continue
            }
            
            // ๋ชจ๋ž˜์–‘์ด 10 ์ด์ƒ์ผ ๋•Œ๋งŒ ๋น„์œจ ๊ณ„์‚ฐ
            else if sand >= 10 {
                for i in 0..<9 {
                    let nx = currentPoint.x + ratios[i].x
                    let ny = currentPoint.y + ratios[i].y
                    
                    // ๋ชจ๋ž˜์–‘ ๋น„์œจ ๊ณ„์‚ฐํ•ด์„œ ๋ฟŒ๋ฆฌ๊ณ  ๋‚จ์€ ๋ชจ๋ž˜์—์„œ ๋บŒ
                    let num = Int(Double(sand)*Double(ratios[i].percent)*0.01)
                    spreadSand -= num
                    
                    // ๋ฒ”์œ„์—์„œ ๋ฒ—์–ด๋‚˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ•ด๋‹น ์นธ์— ๋”ํ•œ๋‹ค.
                    if nx >= 0 && ny >= 0 && nx < n && ny < n {
                        map[ny][nx] += num
                    }
                    // ๋ฒ”์œ„์—์„œ ๋ฒ—์–ด๋‚ฌ๋‹ค๋ฉด ๋‹ต์— ๋”ํ•œ๋‹ค.
                    else {
                        answer += num
                    }
                }
                let ay = currentPoint.y + ratios[9].y
                let ax = currentPoint.x + ratios[9].x
                // a์ขŒํ‘œ๊ฐ€ ๋ฒ”์œ„์•ˆ์— ์žˆ๋‹ค๋ฉด ๋‚จ์€ ๋ชจ๋ž˜๋ฅผ ๋”ํ•œ๋‹ค.
                if ax >= 0 && ay >= 0 && ax < n && ay < n {
                    map[ay][ax] += spreadSand
                }
                
                // ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ๋‹ค๋ฉด ๋‹ต์— ๋”ํ•œ๋‹ค.
                else {
                    answer += spreadSand
                }
            }
            
            // ๋ชจ๋ž˜์–‘์ด 10 ๋ฏธ๋งŒ์ด๋ฉด a์— ๋‹ค ๋ฟŒ๋ ค์ง
            else {
                let ay = currentPoint.y + ratios[9].y
                let ax = currentPoint.x + ratios[9].x
    
                if ax >= 0 && ay >= 0 && ax < n && ay < n {
                    map[ay][ax] += spreadSand
                }
                
                // ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฌ๋‹ค๋ฉด ๋‹ต์— ๋”ํ•œ๋‹ค.
                else {
                    answer += spreadSand
                }
            }
            
        }
    }
    
}

var moveCount = 1
// ํ† ๋„ค์ด๋„ ์ด๋™ ๊ทœ์น™ -> ์™ผ1 ์•„1 ์˜ค2 ์œ„2 ์™ผ3 ์•„3 ์˜ค4 ์œ„4 ..
while true {
    // n์ด๋ž‘ ๊ฐ™์œผ๋ฉด left ํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜๊ณ  ๋
    if moveCount == n {
        move(.left, moveCount)
        break
    }
    
    // moveCount๊ฐ€ ํ™€์ˆ˜๋ฉด left, down
    if moveCount % 2 == 1 {
        move(.left, moveCount)
        move(.down, moveCount)
    }
    // moveCount๊ฐ€ ์ง์ˆ˜๋ฉด right, up
    else {
        move(.right, moveCount)
        move(.up, moveCount)
    }
    // ์›€์ง์ž„์ˆ˜ +1
    moveCount += 1
}

print(answer)

 

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...