首页 技术 正文
技术 2022年11月20日
0 收藏 777 点赞 4,586 浏览 2671 个字

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=3152

Obstacle Course

Description

hdu 3152 Obstacle Course

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.

hdu 3152 Obstacle Course

$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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290