- Knight Shortest Path II
Given a knight in a chessboard n * m (a binary matrix with 0 as empty and 1 as barrier). the knight initialze position is (0, 0) and he wants to reach position (n - 1, m - 1), Knight can only be from left to right. Find the shortest path to the destination position, return the length of the route. Return -1 if knight can not reached.
Example
Example 1:
Input:
[[0,0,0,0],[0,0,0,0],[0,0,0,0]]
Output:
3
Explanation:
[0,0]->[2,1]->[0,2]->[2,3]
Example 2:
Input:
[[0,1,0],[0,0,1],[0,0,0]]
Output:
-1
Clarification
If the knight is at (x, y), he can get to the following positions in one step:
(x + 1, y + 2)
(x - 1, y + 2)
(x + 2, y + 1)
(x - 2, y + 1)
解法1:
struct Node {
int x;
int y;
Node(int _x = 0, int _y = 0) : x(_x), y(_y) {}
};
class Solution {
public:
/**
* @param grid: a chessboard included 0 and 1
* @return: the shortest path
*/
int shortestPath2(vector<vector<bool>> &grid) {
int n = grid.size();
if (n == 0) return -1;
int m = grid[0].size();
if (m == 0) return -1;
vector<vector<bool>> visited(n, vector<bool>(m, false));
vector<int> dx = {1, -1, 2, -2};
vector<int> dy = {2, 2, 1, 1};
queue<Node> q;
q.push(Node(0, 0));
visited[0][0] = true;
int step = 0;
while(!q.empty()) {
int qSize = q.size();
for (int i = 0; i < qSize; ++i) {
Node curNode = q.front();
q.pop();
if (curNode.x == n - 1 && curNode.y == m - 1) {
return step;
}
for (int j = 0; j < 4; ++j) {
int newX = curNode.x + dx[j];
int newY = curNode.y + dy[j];
if (newX >= 0 && newX < n && newY >= 0 && newY < m && !visited[newX][newY] && grid[newX][newY] != 1) {
q.push(Node(newX, newY));
visited[newX][newY] = true;
}
}
}
step++;
}
return -1;
}
};
解法2:DP
注意此处行方向可上可下,而列方向只能往右,所以先列循环在外,再行循环在内,即对每列,行要全部遍历一遍。
也就是说,执行到列j时,需要保证所有的行0…m都遍历过了,才能保证dp[i][j]能够在dp[0…n][j-1]的可行解中找到最大值。
class Solution {
public:
/**
* @param grid: a chessboard included 0 and 1
* @return: the shortest path
*/
int shortestPath2(vector<vector<bool>> &grid) {
int n = grid.size();
if (n == 0) return -1;
int m = grid[0].size();
if (m == 0) return -1;
vector<vector<int>> dp(n, vector<int>(m, INT_MAX));
dp[0][0] = 0;
vector<int> dx = {1, -1, 2, -2};
vector<int> dy = {2, 2, 1, 1};
for (int j = 0; j < m; ++j) {
for (int i = 0; i < n; ++i) {
if (!grid[i][j]) {
for (int k = 0; k < 4; ++k) {
int newX = i - dx[k];
int newY = j - dy[k];
if (newX >= 0 && newX < n && newY >= 0 && newY < m && grid[newX][newY] != 1 && dp[newX][newY] != INT_MAX) {
dp[i][j] = min(dp[i][j], dp[newX][newY] + 1);
}
}
}
}
}
return dp[n - 1][m - 1] == INT_MAX ? -1 : dp[n - 1][m - 1];
}
};
代码同步在
https://github.com/luqian2017/Algorithm