-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14499_주사위 굴리기.swift
103 lines (89 loc) · 2.16 KB
/
14499_주사위 굴리기.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import Foundation
let input = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
let n = input[0]
let m = input[1]
var x = input[2]
var y = input[3]
let k = input[4]
var map: [[Int]] = []
for _ in 0..<n {
let input = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
map.append(input)
}
var commands: [Int] = readLine()!
.components(separatedBy: " ")
.map { Int($0)!-1 }
let dx = [0, 0, -1, 1]
let dy = [1, -1, 0, 0]
var dice = Dice()
for command in commands {
let nx = x + dx[command]
let ny = y + dy[command]
guard 0..<n ~= nx else { continue }
guard 0..<m ~= ny else { continue }
x = nx
y = ny
map[x][y] = dice.move(command: command, value: map[x][y])
}
struct Dice {
private var top = 0
private var bottom = 0
private var left = 0
private var right = 0
private var back = 0
private var front = 0
/*
back
left top right
front
bottom
*/
/// - Parameters:
/// - command: 이동방향
/// - value: 움직인 위치의 값
mutating func move(command: Int, value: Int) -> Int {
let temp = (
top: top,
bottom: bottom,
left: left,
right: right,
back: back,
front: front
)
switch command {
case 0:
top = temp.left
bottom = temp.right
left = temp.bottom
right = temp.top
case 1:
top = temp.right
bottom = temp.left
left = temp.top
right = temp.bottom
case 2:
top = temp.front
bottom = temp.back
back = temp.top
front = temp.bottom
case 3:
top = temp.back
bottom = temp.front
back = temp.bottom
front = temp.top
default:
break
}
print(top)
if value == 0 {
return bottom
} else {
bottom = value
return 0
}
}
}