Untitled
unknown
plain_text
a year ago
1.2 kB
9
Indexable
fun solution(R: Array<String>): Int {
val n = R.size
val m = R[0].length
// 3D array to track visited squares in each direction
val visited = Array(n) { Array(m) { BooleanArray(4) } }
// Robot starts at (0,0) facing right
var x = 0
var y = 0
var d = 0 // 0: right, 1: down, 2: left, 3: up
// Direction vectors for movement
val dx = intArrayOf(0, 1, 0, -1)
val dy = intArrayOf(1, 0, -1, 0)
while (true) {
// If this direction from (x,y) has been visited, stop
if (visited[x][y][d]) break
// Mark current cell and direction as visited
visited[x][y][d] = true
val nx = x + dx[d]
val ny = y + dy[d]
// If next move is within bounds and is an empty space, move forward
if (nx in 0 until n && ny in 0 until m && R[nx][ny] == '.') {
x = nx
y = ny
} else {
// Rotate 90 degrees clockwise
d = (d + 1) % 4
}
}
// Count unique cleaned squares
var clean = 0
for (i in 0 until n) {
for (j in 0 until m) {
if (visited[i][j].any { it }) clean++
}
}
return clean
}
Editor is loading...
Leave a Comment