hdu 3152 Obstacle Course
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=3152
Obstacle Course
Description

You are working on the team assisting with programming for the Mars rover. To conserve energy, the rover needs to find optimal paths across the rugged terrain to get from its starting location to its final location. The following is the first approximation for the problem.

$N * N$ square matrices contain the expenses for traversing each individual cell. For each of them, your task is to find the minimum-cost traversal from the top left cell $[0][0]$ to the bottom right cell $[N-1][N-1]$. Legal moves are up, down, left, and right; that is, either the row index changes by one or the column index changes by one, but not both.
Input
Each problem is specified by a single integer between 2 and 125 giving the number of rows and columns in the $N * N$ square matrix. The file is terminated by the case $N = 0$.
Following the specification of $N$ you will find $N$ lines, each containing $N$ numbers. These numbers will be given as single digits, zero through nine, separated by single blanks.
Output
Each problem set will be numbered (beginning at one) and will generate a single line giving the problem set and the expense of the minimum-cost path from the top left to the bottom right corner, exactly as shown in the sample output (with only a single space after "Problem" and after the colon).
Sample Input
3
5 5 4
3 9 1
3 2 7
5
3 7 2 0 1
2 8 0 9 1
1 2 1 8 1
9 8 9 2 0
3 6 5 1 5
7
9 0 5 1 1 5 3
4 1 2 1 6 5 3
0 7 6 1 6 8 5
1 1 7 8 3 2 3
9 4 0 7 6 4 1
5 8 3 2 4 8 3
7 4 8 4 8 3 4
0
Sample Output
Problem 1: 20
Problem 2: 19
Problem 3: 36
bfs搜索,注意走过的点可以重复走。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
int H, G[N][N], vis[N][N];
const int dx[] = { , , -, }, dy[] = { -, , , };
struct Node {
int x, y, s;
Node(int i = , int j = , int k = ) :x(i), y(j), s(k) {}
inline bool operator<(const Node &a) const {
return s > a.s;
}
};
inline void read() {
rep(i, H) {
rep(j, H) {
scanf("%d", &G[i][j]);
vis[i][j] = -;
}
}
}
int bfs() {
priority_queue<Node> q;
q.push(Node(, , G[][]));
while (!q.empty()) {
Node t = q.top(); q.pop();
if (t.x == H - && t.y == H - ) break;
rep(i, ) {
int x = t.x + dx[i], y = t.y + dy[i];
if (x < || x >= H || y < || y >= H) continue;
if (t.s + G[x][y] < vis[x][y] || vis[x][y] == -) {
q.push(Node(x, y, t.s + G[x][y]));
vis[x][y] = t.s + G[x][y];
}
}
}
return vis[H - ][H - ];
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int k = ;
while (~scanf("%d", &H), H) {
read();
printf("Problem %d: %d\n", k++, bfs());
}
return ;
}
hdu 3152 Obstacle Course的更多相关文章
- HDU 3152 Obstacle Course(优先队列,广搜)
题目 用优先队列优化普通的广搜就可以过了. #include<stdio.h> #include<string.h> #include<algorithm> usi ...
- HDU 5794 A Simple Chess (容斥+DP+Lucas)
A Simple Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 Description There is a n×m board ...
- War Chess (hdu 3345)
http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HDU 5794 A Simple Chess dp+Lucas
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 A Simple Chess Time Limit: 2000/1000 MS (Java/O ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- MyBatis框架
MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的xml或者注解用于 ...
- AD采样问题总结
说明:来源http://bbs.csdn.net/topics/390899032论坛讨论 一个100HZ的正弦波,我用300HZ的采样率去采样,那么根据香农定律是不是一秒钟就采集到300个点,因为这 ...
- 延迟jquery,ready事件触发的时间
$.holdReady(true);//holdReady必须在ready()方法调用之前来调用,来延迟ready()方法的执行 $(document).ready(function(){ conso ...
- Oracle笔记 六、PL/SQL简单语句块、变量定义
1.简单SQL语句,HellWorld示例 --输出信息 begin dbms_output.put_line('Oracle Hello World!'); end; 2.变量的定义.使用 --定义 ...
- poj2070
#include <stdio.h> int main(){ double a,b,c; int s; while(~scanf("%lf%lf%lf",&a, ...
- 1Android系统移植与驱动开发概述
1.Android系统架构分为四层,从下至上依次为Linux内核层,C/C++代码库.Android SDK API.应用程序,要熟悉每一层的内容以及功能: 2.Android移植分为应用移植和系统移 ...
- Android IOS WebRTC 音视频开发总结(五四)-- WebRTC标准之父谈WebRTC
本文主要是整理自国内首届WebRTC大会上对Daniel的一些专访,转载必须说明出处,欢迎关注微信公众号blacker,更多说明详见www.rtc.help 说明:以下内容主要整理自InfoQ的专访, ...
- PUTTY使用Ctrl+s僵死的问题
算是分享个小经验吧! 一直都是使用VM+PUTTY的方式调试Linux程序,有时候在Vi中编辑了程序,Windowns下旧习难改,顺手就Ctrl+s了,尽管我知道Vi的保存是:w. 很不幸这时的PUT ...
- ADO.NET数据库
ASP.NET提供了ADO.NET技术,它是ASP.NET应用程序与数据库进行交互的一种技术. ADO.NET技术把对数据库的操作分为几个步骤,并为每个步骤提供对象来封装操作过程,从而使对数据库的操作 ...
- c#中执行多条sql语句【ORA-00911: 无效字符】
问题描述: 在plsql里执行多条sql语句的时候,使用“,”(逗号)分隔,测试可以执行多条,而在C#执行多条sql语句的时候[ORA-00911: 无效字符]. 有时我们需要一次性执行多条sql语句 ...