문제링크
1. Notation
- n : 세로
- m : 가로
- r : 회전 횟수
- sub : 한 번 회전 시, 필요한 회전 수
2. Source Code
fun main() {
val br = System.`in`.bufferedReader()
val bw = System.out.bufferedWriter()
val (n, m, r) = br.readLine().split(" ").map { it.toInt() }
val arr = Array(n) { mutableListOf<Int>() }
repeat(n) {
arr[it] = br.readLine().split(" ").map { it.toInt() }.toMutableList()
}
repeat(r) {
val sub = minOf(n, m) / 2
repeat(sub) { start ->
val temp = arr[start][start]
for (col in start until m - start - 1) {
arr[start][col] = arr[start][col + 1]
}
for (row in start until n - start - 1) {
arr[row][m - start - 1] = arr[row + 1][m - start - 1]
}
for (col in m - start - 1 downTo 1 + start) {
arr[n - start - 1][col] = arr[n - start - 1][col - 1]
}
for (row in n - start - 1 downTo 2 + start) {
arr[row][start] = arr[row - 1][start]
}
arr[1 + start][start] = temp
}
}
for (i in 0 until n) {
for (j in 0 until m) {
bw.write("${arr[i][j]} ")
}
bw.write("\n")
}
br.close()
bw.close()
}
3. Complexity
- Time : O(r x N), N = min(n, m)
- Space : O(n x m)