【Lintcode】1732. Snakes and Ladders
題目地址:
https://www.lintcode.com/problem/snakes-and-ladders/description
給定一個 N × N N\times N N×N棋盤 A A A,對每個格子進行編號,從最左下角開始從 1 1 1編號,向右依次 2 , 3 , . . . , N 2,3,...,N 2,3,...,N,走到最右邊則向上走一步是 N + 1 N+1 N+1,然後一路再向左依次是 N + 2 , N + 3 , . . . , 2 N N+2,N+3,...,2N N+2,N+3,...,2N,這樣走到第 0 0 0行直至走到 N 2 N^2 N2這個位置。從編號 1 1 1的位置出發,每次處於 a a a的時候,可以走到 a + j , j = 1 , 2 , . . . , 6 a+j,j=1,2,...,6 a+j,j=1,2,...,6這些位置(當然這些位置的編號不能超出 N 2 N^2 N2),如果 A A A在這個位置不等於 − 1 -1 −1,那麼就需要瞬間移動到 A A A在此處的值對應的編號的位置(瞬移是強制的,不消耗步數)。問從 1 1 1出發至少需要走多少步能走到 N 2 N^2 N2這個位置。題目保證 A [ N − 1 ] [ 0 ] = − 1 A[N-1][0]=-1 A[N−1][0]=−1,並且 N > 1 N>1 N>1。
思路是BFS。我們可以直接視為是在一維陣列上操作的,問題的關鍵在於怎麼將一維陣列的下標轉化為二維陣列。如果某個位置在一維陣列中的下標是 i i i,那麼其在二維陣列中的行座標應該是 x = N − 1 − ⌊ i / N ⌋ x=N-1-\lfloor i/N\rfloor x=N−1−⌊i/N⌋,而列座標則需要取決於 N − 1 − x N-1-x N−1−x的奇偶性,如果 N − 1 − x N-1-x N−1−x是偶數,則是從左到右的,那麼列座標就是 i % N i\% N i%N,否則則是 N − 1 − i % N N-1-i\%N N−1−i%N。程式碼如下:
import java.util.ArrayDeque;
import java.util.Queue;
public class Solution {
/**
* @param board: board
* @return: snakesAndLadders
*/
public int snakesAndLadders(int[][] board) {
// write your code here
int n = board.length;
int start = 0, end = n * n - 1;
Queue<Integer> queue = new ArrayDeque<>();
queue.offer(start);
boolean[] visited = new boolean[n * n];
visited[0] = true;
int res = 0;
while (!queue.isEmpty()) {
// 走一步
res++;
// 由於是分層BFS,需要記錄佇列size
int size = queue.size();
for (int i = 0; i < size; i++) {
int cur = queue.poll();
for (int j = 1; j <= 6 && cur + j < n * n; j++) {
int next = cur + j;
int[] trans = transfer(next, n);
// 如果能瞬移則需要瞬移
if (board[trans[0]][trans[1]] != -1) {
next = board[trans[0]][trans[1]] - 1;
}
// 走到終點了,返回步數
if (next == end) {
return res;
}
if (!visited[next]) {
queue.offer(next);
visited[next] = true;
}
}
}
}
return -1;
}
private int[] transfer(int idx, int n) {
int x = n - 1 - idx / n;
int y = (n - 1 - x) % 2 == 0 ? idx % n : n - 1 - idx % n;
return new int[]{x, y};
}
}
時空複雜度 O ( N 2 ) O(N^2) O(N2)。
相關文章
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- Lintcode 1263. Is Subsequence
- 【Lintcode】1189. Minesweeper
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- [LintCode] Check Full Binary Tree
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- [LintCode] 3Sum Smaller
- 【Lintcode】1615. The Result of Investment
- [LintCode] Binary Tree Level Order
- 【Lintcode】1736. Throw Garbage
- 【Lintcode】1665. Calculate Number
- 【Lintcode】1789. Distinguish UsernameNGUI
- 【Lintcode】1562. Number of RestaurantsREST
- 【Lintcode】576. Split Array
- 【Lintcode】1267. Lexicographical Numbers
- 【Lintcode】141. Sqrt(x)
- 【Lintcode】1415. Residual Product
- 【Lintcode】1230. Assign CookiesCookie
- 【Lintcode】1218. Number Complement
- 【Lintcode】1850. Pick ApplesAPP
- 【Lintcode】572. Music PairsAI
- 【Lintcode】318. Character Grid
- 【Lintcode】1891. Travel Plan
- [LintCode/LeetCode] Check Sum of K PrimesLeetCode
- [LintCode]NumberofIslands(島嶼個數)
- lintcode-514-柵欄染色
- 【Lintcode】1322. Product Equal B
- 【Lintcode】191. Maximum Product Subarray
- 【Lintcode】1786. Pub Sub Pattern
- 【Lintcode】1793. Balanced Sales Array
- 【Lintcode】1623. Minimal Distance In The Array
- 【Lintcode】1484. The Most Frequent Word
- 【Lintcode】1025. Custom Sort String
- 【Lintcode】1485. Holy Grail spellAI
- 【Lintcode】1601. Boats to Save People