hd3152 Obstacle Course

本文介绍了一种针对火星探测车的能量节约型路径规划算法。该算法通过寻找从起点到终点的最低成本路径来实现最优路径规划。使用了宽度优先搜索和优先队列的数据结构来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Obstacle Course

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

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
 
恩,题目大意就是说,矩阵内都是一些路的崎岖指数,现在要从左上角走到右下角,问如何走才能走的路的崎岖指数之和最小,输出最小值。
广搜,优先队列即可
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int N,map[150][150],vis[150][150];
int dir[4][2]={-1,0,1,0,0,-1,0,1};
struct node
{
    int x,y,num;
    friend bool operator < (node a,node b)
    {
        return a.num>b.num;
    }
};
int bfs()
{
    node ss,now,next;
    priority_queue<node>q;
    ss.x=0;
    ss.y=0;
    ss.num=map[0][0];
    vis[0][0]=1;
    q.push(ss);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.x==N-1&&now.y==N-1)
            return now.num;
        for(int i=0;i<4;++i)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            if(!vis[next.x][next.y]&&next.x>=0&&next.x<N&&next.y>=0&&next.y<N)
            {
                next.num=now.num+map[next.x][next.y];
                vis[next.x][next.y]=1;
                q.push(next);
            }
        }
    }
}
int main()
{
    int cnt=0;
    while(scanf("%d",&N)&&N)
    {
        cnt++;
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<N;++i)
        {
            for(int j=0;j<N;++j)
            {
                scanf("%d",&map[i][j]);
            }
        }
        printf("Problem %d: %d\n",cnt,bfs());
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值