Sicily 1321(Dijkstra算法)_karell incorporated has designed a new exploration-程序员宅基地

技术标签: 算法  算法设计  Dijkstra  

1321. Robot

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

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.

\epsfbox{p3481.eps}
Path Example
From (1,1) to (5,5)
Fuel needed 10

Input

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 < row$ \le$100 , 0 < column$ \le$100

The next lines are the data of the terrain grid

The last line of the test has the starting and ending coordinates.

Output

One line, for each test will have the amount of fuel needed by the robot

Sample Input

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

Sample Output

10
15
19
解析:STL中优先级队列与普通的队列不太一样,在于取出队列的元素不是从队列的首部取出,而是根据设定的元素优先级来取出对象.这一道题中,很显然是一道最短路径算法的题目,很容易想到使用Dijkstra算法,将临近的点加入到队列中,每次取出代价最小的元素,若该元素已经被访问过,则出列;否则将其临近的点加入到队列中,知道到达终点.
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

int dir_x[] = {-1, 0, 1, 0};    // 上下左右四个方向
int dir_y[] = {0, 1, 0, -1};

struct Node {
	int x;
	int y;
	int cost;
	Node(int a, int b, int c) {
		x = a;
		y = b;
		cost = c;
	}
	friend bool operator < (Node a, Node b) {
		return a.cost > b.cost;    // 返回">"的结果使得队列默认为最小堆
	}
};

int main() {
	int t;
	int city[101][101];
	bool visit[101][101];          // 记录节点是否已经访问
	cin >> t;
	while (t--) {
		int m, n;
		cin >> m >> n;
		memset(visit, false, sizeof(visit));
		for (int i = 1; i <= m; i++) {
			for (int j = 1; j <= n; j++) {
				cin >> city[i][j];
			}
		}
		int s_x, s_y, e_x, e_y;     // 记录起始坐标和终点坐标
		cin >> s_x >> s_y >> e_x >> e_y;
		visit[s_x][s_y] = true;
		priority_queue<Node> q;
		q.push(Node(s_x, s_y, city[s_x][s_y]));
		Node top = q.top();         // 取出头顶元素
		while (!visit[e_x][e_y]) {
			for (int i = 0; i < 4; i++) {
				int x = top.x + dir_x[i];
				int y = top.y + dir_y[i];
				if (x >= 1 && x <= m && y >= 1 && y <= n && !visit[x][y]) {
					q.push(Node(x, y, top.cost + city[x][y]));
				}
			}
			top = q.top();          // 若头顶元素已经访问过,则使其出队
			while (visit[top.x][top.y]) {
				q.pop();
				top = q.top();;
			}
			visit[top.x][top.y] = true;
		}
		cout << top.cost << endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tianman90/article/details/50735860

智能推荐

Linux(CentOS-7.0)下安装MySQL5.6.22_mysql 5.6.22 linux 版本下载-程序员宅基地

文章浏览阅读3.2k次。一 安装环境二 安装方式选择三 安装步骤1. 解压下载的zip包2. 卸载MariaDB3. 安装MYSQL4. 启动MYSQL5. 默认root用户登录MYSQL6 登录MYSQL_mysql 5.6.22 linux 版本下载

UE4-(蓝图)第一百课 用.csv格式文件作为配置文件并获取、使用数据_ue4 读取配置文件蓝图-程序员宅基地

文章浏览阅读2.1k次,点赞2次,收藏12次。.csv文件是以逗号分割的值文件。excel支持csv文件,方便修改。.csv是纯文本,可以使用记事本打开看到内容一、创建Excel数据,二、将文件另存为.csv格式的文本命名为Map,也可以使用记事本打开.csv文件查看内容。三、重点:在UE4中创建结构体命名为MapData,与Map文件中表头一致(否则数据导入后没有对应的结构体与之对应会出现错误)注意:1.用e..._ue4 读取配置文件蓝图

华硕x450jn拆机_华硕笔记本电脑X450JB拆卸并安装固态驱动器-程序员宅基地

文章浏览阅读3.6k次。链接到本文:多年来一直希望能启动笔记本. 尽管对笔记本的需求实际上并不大,但家庭和工作单位中都装有计算机,因此无需外出工作. 我去京东住了一段时间,我得到了一个华硕X450JB,一个标准的I5CPU,一个双显卡,而且价格也不贵. 这也是一个长期的愿望. 对于习惯在家中使用固态硬盘台式机的我来说,这台机器的运行速度显然是无法接受的. 我回头发现原来的机器配备了1400硬盘,速度为5400 rpm. ..._x450j怎么拆lcd

java接口中的内部类_详解Java中的接口与内部类的使用-程序员宅基地

文章浏览阅读1k次。一、接口接口的理解 Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现; 也就是说,接口自身自提供方法的基本声明,而一、接口接口的理解Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现;也就是说,接口自身自提供方法的基本声明,而不提供方法体;接口中声明的方法只能被实现该接口的子类所具体实现。接口是Java中另一种非常重..._java内部类usb接口

ASP.NET项目创建_vs2022 asp.net core mvc-程序员宅基地

文章浏览阅读2k次。20220328磁盘扩容ASP.NET项目创建_vs2022 asp.net core mvc

appium+python自动化33-解锁九宫格(TouchAction)-程序员宅基地

文章浏览阅读79次。TouchAction1.源码可以在这个路径找到:Lib\site-packages\appium\webdriver\common\touch_action.pyclass TouchAction(object): def __init__(self, driver=None): self._driver = driver self._actions ..._tkinter九宫格解锁

随便推点

Pycharm中使用Anaconda_pycharm怎么把anaconda的包拿出来-程序员宅基地

文章浏览阅读1.9w次,点赞26次,收藏139次。Pycharm中使用Anaconda问题:安装完Pycharm和Anaconda后,想让Pycharm能调用Anaconda中包含的各种包。这样就不用重复安装各种包了。Anaconda下载安装Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。 因为包含了大量的科学包,Anaconda 的下载文件比较大(约 515..._pycharm怎么把anaconda的包拿出来

Python-Django毕业设计二手图书回收销售网站(程序+Lw)_二手回收的后台管理界面-程序员宅基地

文章浏览阅读40次。该项目含有源码、文档、程序、数据库、配套开发软件、软件安装教程项目运行环境配置:Pychram社区版+ python3.7.7 + Mysql5.7 + HBuilderX+list pip+Navicat11+Django+nodejs。项目技术:django + python+ Vue 等等组成,B/S模式 +pychram管理等等。环境需要1.运行环境:最好是python3.7.7,我们在这个版本上开发的。其他版本理论上也可以。2.pycharm环境:pycharm都可以。_二手回收的后台管理界面

Qt数据可视化_qt 数据可视化-程序员宅基地

文章浏览阅读1.5k次。属性Q3DBars属性说明graph3D->activeTheme()->setGridEnabled(checked);设置网格graph3D->setReflection(checked);设置反射graph3D->valueAxis()->setTitleVisible(checked);设置轴标题graph3D->rowAxis()->setTitleVisible(checked);设置轴标题gr_qt 数据可视化

2020网络安全国赛题-Web安全加固(Web)答案_限制目录执行权限,对目录设置权限为无-程序员宅基地

文章浏览阅读1.7k次,点赞2次,收藏13次。2020国赛题-Web安全加固(Web) _限制目录执行权限,对目录设置权限为无

计算机等级考试考试机配置批处理_高新考试 批处理-程序员宅基地

文章浏览阅读375次。@echo offclscolor 0AEcho ************注意****************Echo 此批处理修改的计算机须符合一下要去:Echo 1.计算机名称是k62Echo 2.用户名称是k62,用户全名是k62.即,此ghost镜像安装的系统set ip=set /p ip=请_高新考试 批处理

强烈推荐iOS开发取色器_oc 相机取色-程序员宅基地

文章浏览阅读2.1k次。强烈推荐iOS开发取色器小编今天特别向大家介绍一款开发时一款利器,在开发时候我们常常根据设计师提取某些元素的颜色,这就是Sip取色器,选择后,可以直接通过粘贴生成oc的代码。_oc 相机取色

推荐文章

热门文章

相关标签