Sicily 1321. Robot (不一样的宽搜)

题目描述

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.

Path Example
From (1,1) to (5,5)
Fuel needed 10
输入格式

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 < row1000 < 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数组)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值