๐ ๋ฌธ์
๐ง ์์ด๋์ด
ArrayDeque ๋ฅผ ์ฌ์ฉํ์ฌ ํ ํธ๋ฆฌ์ค์ ๋ฐ์ด ๋ถ์ด๊ธฐ ์์ด๋์ด๋ฅผ ์ฌ์ฉ.
์ด ๋, index ๋ฐฉํฅ์ ์ ๋๋ก ์ค์ ํด์ค์ผํ๋ค.
ํฉ์น๋ ๊ตฌ๊ฐ์์๋ ์งํ ๋ฐฉํฅ์ชฝ ๋ธ๋ก์ ๋จผ์ ํฉ์ณ์ผ ํ๊ธฐ ๋๋ฌธ์ index ์งํ ๋ฐฉํฅ์ ์ ์ํด์ผํ๋ค.
์ด ๋ถ๋ถ์์ ์ค์ํ๊ธฐ ๊ต์ฅํ ์ฌ์ด ๋ฌธ์ ๋ค.
๐ Kotlin Code
import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.math.max
enum class Direction{
EAST,
WEST,
SOUTH,
NORTH
}
// (1) 0์ผ๋ก ๋น์นธ์ ๋ชจ๋ ๋น๊ฒจ์ค๊ธฐ ์ฒ๋ฆฌ
// (2) ๋น๊ฒจ์จ ๋ค์์ ๊ฒน์น๊ธฐ.
// ๊ฒน์น ๋ ์ํ ํ์ธ
// Current modification ๋ฐฉ์ง
private fun moveRight(map: Array<ArrayDeque<Int>>) {
// ์ค๋ฅธ์ชฝ์ผ๋ก ๋ค ๋ถ์ด๊ธฐ
for (deque in map) {
for (idx in deque.indices) {
if (deque[idx] == 0) {
deque.removeAt(idx)
deque.addFirst(0)
}
}
}
// ๋ถ์์ผ๋ฉด ๊ฒน์น๊ธฐ
for (deque in map) {
for (idx in deque.lastIndex downTo 1) {
if (deque[idx] == 0) break
if (deque[idx] != deque[idx - 1]) continue
deque[idx] *= 2
deque.removeAt(idx - 1)
deque.addFirst(0)
}
}
}
private fun moveLeft(map: Array<ArrayDeque<Int>>) {
// ์ผ์ชฝ์ผ๋ก ๋ค ๋ถ์ด๊ธฐ
for (deque in map) {
for (idx in deque.indices.reversed()) {
if (deque[idx] == 0) {
deque.removeAt(idx)
deque.addLast(0)
}
}
}
// ๋ถ์์ผ๋ฉด ๊ฒน์น๊ธฐ
for (deque in map) {
for (idx in 0 until deque.size - 1) {
if (deque[idx] == 0) break
if (deque[idx] != deque[idx + 1]) continue
deque[idx] *= 2
deque.removeAt(idx + 1)
deque.addLast(0)
}
}
}
private fun moveDown(map: Array<ArrayDeque<Int>>) {
val deque = Array(map.size){ArrayDeque<Int>(map.size)}
for (col in map[0].indices) {
for (row in map.indices) {
deque[col].addLast(map[row][col])
}
}
// ๋จ์ชฝ์ผ๋ก ๋ค ๋ถ์ด๊ธฐ
for (colQueue in deque) {
for (rowIdx in colQueue.indices) {
if (colQueue[rowIdx] == 0) {
colQueue.removeAt(rowIdx)
colQueue.addFirst(0)
}
}
}
// ๋ถ์์ผ๋ฉด ๊ฒน์น๊ธฐ
for (colQueue in deque) {
for (idx in colQueue.lastIndex downTo 1) {
if (colQueue[idx] == 0) break
if (colQueue[idx] != colQueue[idx - 1]) continue
colQueue[idx] *= 2
colQueue.removeAt(idx - 1)
colQueue.addFirst(0)
}
}
// ๋ฎ์ด์ฐ๊ธฐ
for (col in deque.indices) {
for (row in deque[col].indices) {
map[row][col] = deque[col][row]
}
}
}
private fun moveUp(map: Array<ArrayDeque<Int>>) {
val deque = Array(map.size){ArrayDeque<Int>(map.size)}
for (col in map[0].indices) {
for (row in map.indices) {
deque[col].addLast(map[row][col])
}
}
// ๋ถ์ชฝ์ผ๋ก ๋ค ๋ถ์ด๊ธฐ
for (colQueue in deque) {
for (rowIdx in colQueue.indices.reversed()) {
if (colQueue[rowIdx] == 0) {
colQueue.removeAt(rowIdx)
colQueue.addLast(0)
}
}
}
// ๋ถ์์ผ๋ฉด ๊ฒน์น๊ธฐ
for (colQueue in deque) {
for (idx in 0 until colQueue.lastIndex) {
if (colQueue[idx] == 0) break
if (colQueue[idx] != colQueue[idx + 1]) continue
colQueue[idx] *= 2
colQueue.removeAt(idx + 1)
colQueue.addLast(0)
}
}
// ๋ฎ์ด์ฐ๊ธฐ
for (col in deque.indices) {
for (row in deque[col].indices) {
map[row][col] = deque[col][row]
}
}
}
fun main() {
val bufferedReader = BufferedReader(InputStreamReader(System.`in`))
val size = bufferedReader.readLine().toInt()
val matrix = Array(size){
bufferedReader.readLine().split(" ").map(String::toInt).toIntArray()
}
val map = Array(size){ArrayDeque<Int>(size)}
// ํ
ํธ๋ฆฌ์ค์ฒ๋ผ 0 ์ ์ญ์ ํ๋ฉฐ ์๋ค๋ก ์ถ๊ฐํ๊ธฐ ์ํด Deque ์ฌ์ฉ.
for (row in matrix.indices) {
for (col in matrix[row].indices) {
map[row].addLast(matrix[row][col])
}
}
var answer = 0
val maxCaseCnt = 1 shl 10
for (i in 0 until maxCaseCnt) {
var num = i
// 5๋ฒ์ ์ด๋ ์๋ฃ
for (flagCnt in 1 .. 5) {
val directionNum = num % 4
num /= 4
when (directionNum) {
Direction.EAST.ordinal -> moveRight(map)
Direction.WEST.ordinal -> moveLeft(map)
Direction.SOUTH.ordinal -> moveDown(map)
Direction.NORTH.ordinal -> moveUp(map)
}
}
// ๋ผ์ด๋๋ฅผ ์งํํ ํ ํ์ ์ ํ ์ซ์์ค ๊ฐ์ฅ ํฐ ์ ๋ฝ์๋ด๊ธฐ
for (row in map) {
answer = max(answer, row.maxOf { it })
}
// ๋ผ์ด๋๋ณ ์ด๊ธฐํ
for (row in matrix.indices) {
for (col in matrix[row].indices) {
map[row][col] = matrix[row][col]
}
}
}
print(answer)
}
'Algorithm > Algorithm (๋ฌธ์ ํ์ด)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2295 ์ธ ์์ ํฉ (1) | 2022.11.05 |
---|---|
[๋ฐฑ์ค] 18870 ์ขํ ์์ถ (0) | 2022.11.01 |
[๋ฐฑ์ค] 14891 ํฑ๋๋ฐํด (0) | 2022.10.25 |
[๋ฐฑ์ค] 2457 ๊ณต์ฃผ๋์ ์ ์ (0) | 2022.10.21 |
[๋ฐฑ์ค] 14499 ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ (0) | 2022.10.13 |