Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2d9fea

Browse files
1
1 parent 8e3e08e commit c2d9fea

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

‎src/main/kotlin/p34xx/Problem3459.kt

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,36 @@ fun main() {
2020
initPos.add(r to c)
2121
}
2222

23-
Array(4) { intArrayOf(0, 0) }
23+
IntArray(8)
2424
}
2525
}
2626

2727
fun dfs(pos: Pair<Int, Int>, directionIndex: Int, turnCount: Int): Int {
28-
return if (cache[pos.first][pos.second][directionIndex][turnCount] > 0) {
29-
cache[pos.first][pos.second][directionIndex][turnCount]
28+
return if (cache[pos.first][pos.second][directionIndex*2+turnCount] > 0) {
29+
cache[pos.first][pos.second][directionIndex*2+turnCount]
3030
} else {
31-
var result = 0
32-
3331
val currentNum = grid[pos.first][pos.second]
3432

35-
val forwardPos = pos.first + directions[directionIndex].first to pos.second + directions[directionIndex].second
33+
val result = (0..turnCount).maxOf { diffDirectionIndex ->
34+
val newDirectionIndex = (directionIndex + diffDirectionIndex) % 4
3635

37-
directions[directionIndex].let {
38-
grid.getOrNull(forwardPos.first)?.getOrNull(forwardPos.second)
39-
}?.takeIf {
40-
currentNum == 1 && it == 2 || (currentNum - it).absoluteValue == 2
41-
}?.also {
42-
result = dfs(forwardPos, directionIndex, turnCount)
43-
}
36+
val newPos = pos.first + directions[newDirectionIndex].first to pos.second + directions[newDirectionIndex].second
4437

45-
if (turnCount == 1 && currentNum != 1) {
46-
val turnDirectionIndex = (directionIndex + 1) % 4
47-
val turnPos = pos.first + directions[turnDirectionIndex].first to pos.second + directions[turnDirectionIndex].second
48-
grid.getOrNull(turnPos.first)?.getOrNull(turnPos.second)?.takeIf {
49-
it != currentNum && it != 1
50-
}?.also {
51-
result = maxOf(result, dfs(turnPos, turnDirectionIndex, 0))
52-
}
53-
}
38+
grid.getOrNull(newPos.first)?.getOrNull(newPos.second)?.takeIf {
39+
(currentNum - it).absoluteValue == 2 || diffDirectionIndex + currentNum == 1 && it == 2
40+
}?.let {
41+
dfs(newPos, newDirectionIndex, diffDirectionIndex xor turnCount)
42+
} ?: 0
43+
} + 1
5444

55-
result++
56-
cache[pos.first][pos.second][directionIndex][turnCount] = result
45+
cache[pos.first][pos.second][directionIndex * 2 + turnCount] = result
5746
result
5847
}
5948
}
6049

6150
return initPos.maxOfOrNull { pos ->
62-
(0 until 4).maxOf { directionIndex ->
63-
maxOf(dfs(pos, directionIndex, 0), dfs(pos, directionIndex, 1))
51+
(0 until 8).maxOf {
52+
dfs(pos, it /2, it %2)
6453
}
6554
} ?: 0
6655
}
@@ -69,9 +58,7 @@ fun main() {
6958
expect {
7059
Solution().lenOfVDiagonal(
7160
arrayOf(
72-
intArrayOf(1, 1, 2, 1, 0, 1, 1, 0, 0),
73-
intArrayOf(1, 0, 1, 2, 2, 0, 2, 1, 1),
74-
intArrayOf(1, 0, 2, 0, 2, 1, 1, 1, 1),
61+
intArrayOf(1),
7562
)
7663
)
7764
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /