1364. the minium distance
You are now given a two-dimensional tabular graph , in which each grid contains a integer num.
If num is - 2, it means this grid is the starting grid. If num is - 3, it means this grid is the ending grid. If num is - 1, it means this grid has an obstacle on it and you can't move to it. If num is a positive number or 0,you can walk normally on it.
In each move you can travel from one grid to another if and only if they're next to each other or they contain the same positive number num. The cost of each move is 1.
Now you are asked to find the lowest cost of travelling from the starting grid to the ending grid. If the ending grid could not be reached, print -1.
Example
Example
Input:[[1,0,-1,1],[-2,0,1,-3],[2,2,0,0]]
Output:3
In this example,you can reach the ending grid through these moves:
First, move up from the starting grid to the grid that contains the number 1. Second, move to the grid with the same number at the top right.
Finally, move down to the ending grid. There are three moves in total, so the minimun cost will be 3.
Notice
It is guaranteed that the maxium number of rows and columns is 400, and the number in each grid will not exceed 50.
Input test data (one parameter per line)How to understand a testcase?
解法1:
这是典型的传送门题目。其实就是用BFS,然后把传送门当作第5维就可以了。
class Solution {
public:
/**
* @param mazeMap: a 2D grid
* @return: return the minium distance
*/
int getMinDistance(vector<vector<int>> &mazeMap) {
int num_row = mazeMap.size();
int num_col = mazeMap[0].size();
vector<vector<int>> visited(num_row, vector<int>(num_col, false));
vector<bool> visited_fast_door(51, false); //if the fast door number is used.
vector<vector<pair<int, int>>> fast_door(51); //each entry records the list of doors (row, col) that have the number i
queue<pair<int, int>> q;
pair<int, int> source, destination;
vector<vector<int>> distance(num_row, vector<int>(num_col, 0));
for (int i = 0; i < num_row; ++i) {
for (int j = 0; j < num_col; ++j) {
if (mazeMap[i][j] == -2) {
source = {i, j};
} else if (mazeMap[i][j] == -3) {
destination = {i, j};
} else if (mazeMap[i][j] > 0) {
fast_door[mazeMap[i][j]].push_back({i, j});
visited_fast_door[mazeMap[i][j]] = false;
}
}
}
q.push(source);
visited[source.first][source.second] = true;
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
while(!q.empty()) {
int qSize = q.size();
for (int i = 0; i < qSize; ++i) {
auto front = q.front();
q.pop();
int x = front.first;
int y = front.second;
int index = mazeMap[x][y];
int dist = distance[x][y];
if (front == destination) {
return distance[x][y];
}
for (int j = 0; j < 4; ++j) {
int newX = front.first + dx[j];
int newY = front.second + dy[j];
if (newX < 0 ||
newX >= num_row ||
newY < 0 ||
newY >= num_col ||
visited[newX][newY] ||
mazeMap[newX][newY] == -1) {
continue;
} else {
q.push({newX, newY});
visited[newX][newY] = true;
distance[newX][newY] = dist + 1;
}
}
//the 5th dimension, check if it is fast door
if (index > 0) {
if (visited_fast_door[index]) {
continue;
} else {
for (int i = 0; i < fast_door[index].size(); ++i) {
int newX = fast_door[index][i].first;
int newY = fast_door[index][i].second;
if (visited[newX][newY]) continue;
visited[newX][newY] = 1;
distance[newX][newY] = dist + 1;
q.push(fast_door[index][i]);
}
visited_fast_door[index] = true;
}
}
}
}
return -1;
}
};
解法2:跟解法1一样,但不用distance数组,仍然用steps
class Solution {
public:
/**
* @param mazeMap: a 2D grid
* @return: return the minium distance
*/
int getMinDistance(vector<vector<int>> &mazeMap) {
int num_row = mazeMap.size();
int num_col = mazeMap[0].size();
vector<vector<int>> visited(num_row, vector<int>(num_col, false));
vector<bool> visited_fast_door(51, false); //if the fast door number is used.
vector<vector<pair<int, int>>> fast_door(51); //each entry records the list of doors (row, col) that have the number i
queue<pair<int, int>> q;
pair<int, int> source, destination;
for (int i = 0; i < num_row; ++i) {
for (int j = 0; j < num_col; ++j) {
if (mazeMap[i][j] == -2) {
source = {i, j};
} else if (mazeMap[i][j] == -3) {
destination = {i, j};
} else if (mazeMap[i][j] > 0) {
fast_door[mazeMap[i][j]].push_back({i, j});
visited_fast_door[mazeMap[i][j]] = false;
}
}
}
int steps = 0;
q.push(source);
visited[source.first][source.second] = true;
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
while(!q.empty()) {
int qSize = q.size();
for (int i = 0; i < qSize; ++i) {
auto front = q.front();
q.pop();
int x = front.first;
int y = front.second;
int index = mazeMap[x][y];
if (front == destination) {
return steps;
}
for (int j = 0; j < 4; ++j) {
int newX = front.first + dx[j];
int newY = front.second + dy[j];
if (newX < 0 ||
newX >= num_row ||
newY < 0 ||
newY >= num_col ||
visited[newX][newY] ||
mazeMap[newX][newY] == -1) {
continue;
} else {
q.push({newX, newY});
visited[newX][newY] = true;
}
}
//the 5th dimension, check if it is fast door
if (index > 0) {
if (visited_fast_door[index]) {
continue;
} else {
for (int i = 0; i < fast_door[index].size(); ++i) {
int newX = fast_door[index][i].first;
int newY = fast_door[index][i].second;
if (visited[newX][newY]) continue;
visited[newX][newY] = 1;
q.push(fast_door[index][i]);
}
visited_fast_door[index] = true;
}
}
}
steps++;
}
return -1;
}
};