Karell Incorporated has designed a new exploration robot that has the ability to explore new terrains, this new robot can move in all kinds of terrain, it only needs more fuel to move in rough terrains, and less fuel in plain terrains. The only problem this robot has is that it can only move orthogonally, the robot can only move to the grids that are at the North, East, South or West of its position.
The Karell`s robot can communicate to a satellite dish to have a picture of the terrain that is going to explore, so it can select the best route to the ending point, The robot always choose the path that needs the minimum fuel to complete its exploration, however the scientist that are experimenting with the robot, need a program that computes the path that would need the minimum amount of fuel. The maximum amount of fuel that the robot can handle is 9999 units
The Terrain that the robot receives from the satellite is divided into a grid, where each cell of the grid is assigned to the amount of fuel the robot would need to pass thought that cell. The robot also receives the starting and ending coordinates of the exploration area.
The first line of the input file is the number of tests that must be examined.
The first line of the test is the number of rows and columns that the grid will contain. The rows and columns will be 0 < row100, 0 < column100
The next lines are the data of the terrain grid
The last line of the test has the starting and ending coordinates.
One line, for each test will have the amount of fuel needed by the robot
3 5 5 1 1 5 3 2 4 1 4 2 6 3 1 1 3 3 5 2 3 1 2 2 1 1 1 1 1 1 5 5 5 4 2 2 15 1 5 1 15 1 5 3 10 1 5 2 1 1 8 13 2 15 1 1 1 4 10 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10
10 15 19
刚碰到这道题的时候脑子一热,用了DFS,后来屡次尝试后均已失败告终,才发现自己又一次在该用BFS上使用了DFS,有时间的话一定好好深究下深搜和宽搜的区别。
这道题应该也是可以使用Dijkstra算法来实现,不过自己还没有尝试。
这道题比较有意思的地方在于,传统的宽搜在访问过后就不访问了,而在这里,若新的访问能够使得访问该点的路程变短的话,就必须重新访问
<span style="font-size:18px;">#include <iostream>
#include <stack>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cstring>
using namespace std;
const int maxn = 1000000;
int map[105][105];
int cost[105][105];
bool visit[105][105];
int start_x, start_y, end_x, end_y;
int row, col;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
struct P {
int x, y;
P(int _x = 0, int _y = 0) {
x = _x;
y = _y;
}
};
bool valid(int x, int y) {
if (x >= 1 && x <= row && y >= 1 && y <= col)
return true;
return false;
}
void bfs(int x, int y) {
queue<P> q;
q.push(P(x, y));
struct P dir[4];
while (!q.empty()) {
P front = q.front();
q.pop();
visit[front.x][front.y] = true;
for (int i = 0; i < 4; i++)
{
dir[i].x = front.x + dx[i];
dir[i].y = front.y + dy[i];
if (valid(dir[i].x, dir[i].y))
{
//若新的访问能够使得访问该点的路程变短的话,就必须重新访问
if(cost[dir[i].x][dir[i].y] > cost[front.x][front.y] + map[dir[i].x][dir[i].y])
{
cost[dir[i].x][dir[i].y] = cost[front.x][front.y] + map[dir[i].x][dir[i].y];
q.push(dir[i]);
}
}
}
}
}
int main() {
int tst;
cin >> tst;
while (tst--) {
cin >> row >> col;
for (int i = 1; i <= row; ++i)
for (int j = 1; j <= col; ++j)
cin >> map[i][j];
cin >> start_x >> start_y >> end_x >> end_y;
memset(visit, 0, sizeof(visit));
memset(cost, maxn, sizeof(cost));
cost[start_x][start_y] = map[start_x][start_y];
bfs(start_x, start_y);
cout << cost[end_x][end_y] << endl;
}
return 0;
}</span>
在BFS中,每一次在四个搜索方向上首先判断是否可行,然后根据新的访问是否能使得改节点路径变短,若可以,则压入队列,否则就搜索另一个方向,在这个BFS中,不需要判断该节点是否访问过(也就是没有bool类型的visit数组)。