hdu 3152 Obstacle Course

时间:2023-03-10 00:23:20
hdu 3152 Obstacle Course

题目连接

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 ;
}