๊ณจ๋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)
'์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 20207] ๋ฌ๋ ฅ swift (0) | 2024.02.01 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค 258707] ์นด์นด์ค ๊ฒจ์ธ ์ธํด์ญ, n + 1 ์นด๋๊ฒ์, swift (0) | 2024.01.10 |
[ํ๋ก๊ทธ๋๋จธ์ค 92342] ์๊ถ ๋ํ swift (1) | 2023.11.08 |
[ํ๋ก๊ทธ๋๋จธ์ค 92341] ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ swift (1) | 2023.11.03 |
[ํ๋ก๊ทธ๋๋จธ์ค 92335] k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ swift (0) | 2023.10.30 |