HDU3152 LA4435 Obstacle Course【优先搜索】

Obstacle Course

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 534 Accepted Submission(s): 341

Problem 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

Source
2008 ACM-ICPC Pacific Northwest Region

问题链接HDU3152 LA4435 Obstacle Course
问题简述:给出一个权值矩阵,找一条从(0,0)点到(n-1,n-1)点的路径使得路径之和最大,并且输出这个和。
问题分析:最优问题,用优先搜索来实现。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU3152 LA4435 Obstacle Course */

#include <bits/stdc++.h>

using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int DL = sizeof(dx) / sizeof(dx[0]);
const int INF = 1 << 30;
const int N = 125;
int n, g[N][N], vis[N][N];
struct Node {
    int x, y, sum;
    friend bool operator < (Node a, Node b) {return a.sum > b.sum;}
};

int bfs()
{
    fill(&vis[0][0], &vis[0][0] + N * N, INF);
    vis[0][0] = g[0][0];

    priority_queue<Node>q;
    q.push({0, 0, g[0][0]});
    while(q.size()) {
        Node t = q.top(), nt;
        q.pop();

        if(t.x == n - 1 && t.y == n - 1)
            return t.sum;
        for(int i = 0; i < DL; i++) {
            nt = t;
            nt.x += dx[i];
            nt.y += dy[i];
            if(0 <= nt.x && nt.x < n && 0 <= nt.y && nt.y < n) {
                nt.sum += g[nt.x][nt.y];
                if(vis[nt.x][nt.y] <= nt.sum) continue;
                vis[nt.x][nt.y] = nt.sum;
                q.push(nt);
            }
        }
    }

    return -1;
}

int main()
{
    int caseno = 0;
    while(~scanf("%d", &n) && n) {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                scanf("%d", &g[i][j]);

        printf("Problem %d: %d\n", ++caseno, bfs());
    }

    return 0;
}
原创文章 2323 获赞 2382 访问量 269万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105778340