题目连接

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的更多相关文章

  1. HDU 3152 Obstacle Course(优先队列,广搜)

    题目 用优先队列优化普通的广搜就可以过了. #include<stdio.h> #include<string.h> #include<algorithm> usi ...

  2. 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 ...

  3. War Chess (hdu 3345)

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...

  4. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  5. 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 ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 如何制作prezi swf格式字体(prezi 中文字体)

    如何制作prezi swf格式字体(prezi 中文字体) 文/玄魂 前言 Prezi软件虽然没有正式进入中国,但是中国的Prezi爱好者却在不遗余力的推广着Prezi.我接触这款软件比较晚,但是从接 ...

  2. 慕课网-安卓工程师初养成-2-5 如何命名Java变量

    来源:http://www.imooc.com/code/1221 如同酒店会给每个房间起个性化的名字一样,程序中的变量也需要用合理的名字进行管理---变量名! 需要注意,给酒店房间起名字时可以是数字 ...

  3. android + javascript 相互通信实例分析

    1.  AndroidManifest.xml中必须使用许可 "android.permission.INTERNET", 否则会出Web page not available错误 ...

  4. SQL 1:常用SQL语句

    导读:最近写代码,几乎是天天泡在SQL语句里,各种代码各种写.但一直缺少总结,要不就是觉得简单,要不就是觉得大家都知道.想来,我还是没能明白总结的价值在哪里.现在也就写写最近都常写的一些语句. 一,i ...

  5. 使用ffmpeg快速生成视频截图

    1 ffmpeg -i input.mkv -ss 00:10:00 -f image2 output.jpg 但是这个命令会花费相当长的时间. 对一个清晰的或者较大的视频文件进行操作, 会花费半分钟 ...

  6. (笔记)angular 事件传递获取当前

  7. VS2010调试生成的文件

    调试时IntelliTrace打开了,关闭IntelliTrace就可以了, Win7的话在C:\ProgramData\Microsoft Visual Studio\10.0\TraceDebug ...

  8. AnyCAD C++ SDK与OpenCASCADE互操作

    AnyCAD SDK有.Net和C++两个版本,使用C++版本的AnyPlatformOcc模块可以实现与OpenCASCADE互操作. C++版本(VS2010 32bit)下载 在AOBridge ...

  9. 开发iOS应用程序需要的工具和编程技术

    似乎每个iOS应用开发学习者都是从这篇文章开始写博客的,我也不例外,也从这里写吧,内容雷同,但绝对是原创.因为一直相信,通过自己的理解写出来,是掌握技术一个很好的途径. Xcode苹果最为优秀的集成开 ...

  10. C puzzles详解【6-8题】

    第六题 #include<stdio.h> int main() { ; switch(a) { ': printf("ONE\n"); break; ': print ...