[BOJ] 14499 주사위 굴리기

‍csk·2022년 10월 25일
0

알고리즘

목록 보기
11/13
post-thumbnail

백준 14499번 주사위 굴리기(swift) [G4]

구현, 시뮬레이션

  • 잘 구현하기

생각회로

  • 진짜 주사위 객체를 만든다.
  • 굴린다.
    • 어느 방향으로 굴렸는가?
    • 상 하 좌 우 에 따라서, 어느 면이 어느 면으로 가는지는 정해져있다.
    • 이에 맞게 구현한다.

주의사항

  • 굴렀을 때, 어떤 면이 어디로 가는지가 하드코딩 되어있다.
  • 하지만 추상화하기엔... 그게 더 어려워보인다.
  • 실제 코테에서 이렇게 객체지향적으로 풀었다간, 다 못풀겠다 생각이 들다가도
  • 이래야 더 잘 풀 것 같다.

소스코드

import Foundation

enum Direction:Int {
  case right = 1
  case left, up, down
}

struct Cube {
  var (up, down, left, right, top, bottom) = (0, 0, 0, 0, 0, 0)
}

class Dice {
  var cube = Cube()
  var li:[[Int]]
  var x,y:Int
  
  init(li: [[Int]], X:Int, Y:Int) {
    (self.li, self.x, self.y) = (li, X, Y)
  }
  
  func turnUp(){
    var newCube = Cube()
    if li[x][y] == 0{
      newCube.bottom = cube.up
      li[x][y] = cube.up
    }else {
      newCube.bottom = li[x][y]
      li[x][y] = 0
    }

    newCube.left = cube.left
    newCube.right = cube.right
    newCube.down = cube.bottom
    newCube.up = cube.top
    newCube.top = cube.down
    cube = newCube
  }
  
  func turnDown() {
    var newCube = Cube()
    if li[x][y] == 0{
      newCube.bottom = cube.down
      li[x][y] = cube.down
    }else {
      newCube.bottom = li[x][y]
      li[x][y] = 0
    }

    newCube.left = cube.left
    newCube.right = cube.right
    newCube.down = cube.top
    newCube.up = cube.bottom
    newCube.top = cube.up
    cube = newCube
  }
  
  func turnLeft() {
    var newCube = Cube()
    if li[x][y] == 0{
      newCube.bottom = cube.left
      li[x][y] = cube.left
    }else {
      newCube.bottom = li[x][y]
      li[x][y] = 0
    }

    newCube.left = cube.top
    newCube.right = cube.bottom
    newCube.down = cube.down
    newCube.up = cube.up
    newCube.top = cube.right
    cube = newCube
  }
  
  func turnRight() {
    var newCube = Cube()
    if li[x][y] == 0{
      newCube.bottom = cube.right
      li[x][y] = cube.right
    }else {
      newCube.bottom = li[x][y]
      li[x][y] = 0
    }

    newCube.left = cube.bottom
    newCube.right = cube.top
    newCube.down = cube.down
    newCube.up = cube.up
    newCube.top = cube.left
    cube = newCube
  }
  
  
  func roll(direction: Direction) {
    switch direction {
      case .right:
        guard y+1 < li[0].count else { return }
        y += 1
        turnRight()
      case .left:
        guard y-1 >= 0 else { return }
        y -= 1
        turnLeft()
      case .up:
        guard x-1 >= 0 else { return }
        x -= 1
        turnUp()
      case .down:
        guard x+1 < li.count else { return }
        x += 1
        turnDown()
    }
    print(cube.top)
  }
}

func main() {
  var input = readLine()!.split(separator: " ").map{ Int($0)! }
  let (N, M, x, y, K) = (input[0], input[1], input[2], input[3], input[4])
  var li = Array(repeating: Array(repeating: 0, count: M), count: N)
  var order: [Direction] = []
  
  (0..<N).forEach{
    li[$0] = readLine()!.split(separator: " ").map{Int($0)!}
  }
  order = readLine()!.split(separator: " ").map{ Direction(rawValue: Int($0)!)! }
  
  let dice = Dice(li: li, X: x, Y: y)
  
  order.forEach{
    dice.roll(direction: $0)
  }
}

main()
profile
Developer

0개의 댓글