forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
83 lines (77 loc) · 3.38 KB
/
Solution.cs
File metadata and controls
83 lines (77 loc) · 3.38 KB
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
namespace LeetCodeNet.G0901_1000.S0909_snakes_and_ladders {
// #Medium #Array #Breadth_First_Search #Matrix #Top_Interview_150_Graph_BFS
// #2025_07_18_Time_6_ms_(72.69%)_Space_46.25_MB_(32.25%)
using System;
using System.Collections.Generic;
public class Solution {
private int size;
public int SnakesAndLadders(int[][] board) {
Queue<int> queue = new Queue<int>();
size = board.Length;
int target = size * size;
// Using 0-indexed for visited array, corresponding to labels 1 to target
bool[] visited = new bool[target];
queue.Enqueue(1);
// Mark label 1 as visited (index 0)
visited[0] = true;
int step = 0;
while (queue.Count > 0) {
int queueSize = queue.Count;
for (int i = 0; i < queueSize; i++) {
int previousLabel = queue.Dequeue();
if (previousLabel == target) {
return step;
}
// Simulate rolling a die (1 to 6)
for (int currentLabel = previousLabel + 1;
currentLabel <= Math.Min(target, previousLabel + 6);
currentLabel++) {
// currentLabel - 1 to map to 0-indexed array
if (visited[currentLabel - 1]) {
continue;
}
// Mark as visited
visited[currentLabel - 1] = true;
int[] position = IndexToPosition(currentLabel);
int boardValue = board[position[0]][position[1]];
if (boardValue == -1) {
queue.Enqueue(currentLabel);
} else {
queue.Enqueue(boardValue);
}
}
}
step++;
}
// Target not reachable
return -1;
}
private int[] IndexToPosition(int index) {
// Adjust index to be 0-based for calculations related to 0 to size*size - 1
int adjustedIndex = index - 1;
// Calculate row
// From bottom to top, so (size - 1) - (row based on adjustedIndex)
int row = size - 1 - adjustedIndex / size;
// Calculate column
int col;
// Check if the row is "snake" (even from bottom) or "ladder" (odd from bottom)
// (size - 1 - row) gives the 0-indexed row number from the top of the board.
// If (size - 1 - row) is even, it's a left-to-right row.
// If (size - 1 - row) is odd, it's a right-to-left row.
// The original Java code uses (this.size - vertical) % 2 == 1,
// which corresponds to (row from top of board) % 2 == 1,
// meaning if the row is odd (0-indexed from top), it's left-to-right
// If row from top is 0 (bottom row), 2, 4... then it's left to right.
// If row from top is 1, 3, 5... then it's right to left.
// (size - 1 - row) is equivalent to the number of rows from the bottom.
// (size - 1 - vertical) is a more direct translation from the Java code.
if ((size - 1 - row) % 2 == 0) { // Even row from top (0-indexed), moves right (left to right)
col = adjustedIndex % size;
} else {
// Odd row from top (0-indexed), moves left (right to left)
col = size - 1 - (adjustedIndex % size);
}
return new int[] {row, col};
}
}
}