hdu 3152 Obstacle Course

15 篇文章 0 订阅

题目链接:传送门

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

【题意】

给你一个n*n的图,找一条从[0,0]到[n-1,n-1]的路径,使得路上所有节点的权值之和最小。

【分析】

显然最容易想到的就是暴力搜索一下,每次能更新的时候就往下暴力搜索,但是这样过不了,显然是会t的。然后就想一下怎么优化。首先如果一个节点更新的次数越少越好。那么我们当我们搜索到一个点的时候,如果多次搜索到了,显然是有一次比其他的时候更优,我们如果能尽可能的取到那个最优的显然是最好的,于是,我们就用队列优化一下,存下所有的节点,不是每次搜索到了就更新后往下dfs,而是一个点搜索完了再搜索剩余的节点。举个例子:

搜索到[1,1],接着搜索到了[1,2]和[2,1],紧接着我们会接连搜索[2,2]两次,这显然是我们不希望的。

于是我们的做法是:

搜索到[1,2]和[2,1]后,我们搜索两次[2,2]后,我们把这两次的搜索值存起来,然后比较一下,选择最优值往下dfs。

第二种做法显然最优。

【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<sstream>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS_1(a) memset(a,-1,sizeof(a))
#define MSinf(a) memset(a,0x3f,sizeof(a))
#define MSfalse(a) memset(a,false,sizeof(a))
#define inf 0x3f3f3f3f
#define lson i<<1,l,mid
#define rson ((i<<1)|1),mid+1,r
#define uint unsigned int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define mp make_pair
#define ll long long
#define eps 1e-8
inline void read1(int &num) {
    char in;
    bool IsN=false;
    in=getchar();
    while(in!='-'&&(in<'0'||in>'9')) in=getchar();
    if(in=='-') {
        IsN=true;
        num=0;
    } else num=in-'0';
    while(in=getchar(),in>='0'&&in<='9') {
        num*=10,num+=in-'0';
    }
    if(IsN) num=-num;
}
inline void read2(int &a,int &b) {
    read1(a);
    read1(b);
}
inline void read3(int &a,int &b,int &c)
{
    read1(a);
    read1(b);
    read1(c);
}
inline void read1(ll &num) {
    char in;
    bool IsN=false;
    in=getchar();
    while(in!='-'&&(in<'0'||in>'9')) in=getchar();
    if(in=='-') {
        IsN=true;
        num=0;
    } else num=in-'0';
    while(in=getchar(),in>='0'&&in<='9') {
        num*=10,num+=in-'0';
    }
    if(IsN) num=-num;
}
inline void read2(ll &a,ll &b) {
    read1(a);
    read1(b);
}
inline void read3(ll &a,ll &b,ll &c)
{
    read1(a);
    read1(b);
    read1(c);
}
inline void read1(double &num) {
    char in;
    double Dec=0.1;
    bool IsN=false,IsD=false;
    in=getchar();
    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
        in=getchar();
    if(in=='-') {
        IsN=true;
        num=0;
    } else if(in=='.') {
        IsD=true;
        num=0;
    } else num=in-'0';
    if(!IsD) {
        while(in=getchar(),in>='0'&&in<='9') {
            num*=10;
            num+=in-'0';
        }
    }
    if(in!='.') {
        if(IsN) num=-num;
        return ;
    } else {
        while(in=getchar(),in>='0'&&in<='9') {
            num+=Dec*(in-'0');
            Dec*=0.1;
        }
    }
    if(IsN) num=-num;
}
inline void read2(double &a,double &b)
{
    read1(a);
    read1(b);
}
inline void read3(double &a,double &b,double &c)
{
    read1(a);
    read1(b);
    read1(c);
}
///--------------------------------------------------------------------------------
const int maxm = 150;
int mat[maxm][maxm];
int ans[maxm][maxm];
int changex[]={0,0,1,-1};
int changey[]={1,-1,0,0};
int n;
queue<pair<int,pair<int,int> > > Q;
void dfs(int x,int y,int v)
{
    if(v>=ans[x][y]) return ;
    ans[x][y] = v;
    if(x<=0||x>n) return ;
    if(y<=0||y>n) return ;
    rep0(i,0,4)
    {
        int xx = x+changex[i];
        int yy = y+changey[i];
        if(ans[xx][yy]>ans[x][y]+mat[xx][yy])
        {
            pair<int,pair<int,int> > a;
            a.first = ans[x][y]+mat[xx][yy];
            a.second.first = xx;
            a.second.second = yy;
            Q.push(a);
        }
    }
}
int main(void)
{
//    freopen("in.txt","r",stdin);
    int g_case = 1;
    while(scanf("%d",&n),n)
    {
        while(!Q.empty()) Q.pop();
        printf("Problem %d: ",g_case++);
        rep1(i,1,n) rep1(j,1,n) read1(mat[i][j]);
        rep1(i,0,n) rep1(j,0,n) ans[i][j] = inf;
        Q.push(mp(mat[1][1],mp(1,1)));
        while(!Q.empty())
        {
            pair<int,pair<int,int> > a;
            a = Q.front();
            Q.pop();
            dfs(a.second.first,a.second.second,a.first);
        }
        printf("%d\n",ans[n][n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值