【搜索】 HDU 3152 Obstacle Course

题目连接点击打开链接

优先队列点按数值最小出队

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <time.h>;
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define FOR(i,a,b)  for(int i=a;i<=b;i++)
#define IN   freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 100010;
const int MAXM = 444;
//const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int INF= 9999999;
int mp[126][126];
bool vis[126][126];
int n ;
int xx[4]= {1,0,0,-1};
int yy[4]= {0,1,-1,0};
struct node
{
    int x,y,step;
    bool operator < (const node& a) const
    {
        return step>a.step;
    }
};
bool in(int x,int y)
{
    if(x<=n&&y<=n&&x>=1&&y>=1)
        return true;
    return false;
}
priority_queue<node> q;
int bfs(int x,int y)
{
    while(!q.empty()) q.pop();
    vis[x][y] =true;
    node front;
    front.x=x;
    front.y=y;
    front.step=mp[x][y];
    q.push(front);
    while(!q.empty())
    {
        front=q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int dx=front.x+xx[i],dy=front.y+yy[i];
            if(in(dx,dy)&&!vis[dx][dy])
            {
                vis[dx][dy]=true;
                node rear;
                rear.x=dx,rear.y=dy,rear.step=front.step+mp[dx][dy];
                if(dx==n&&dy==n)
                    return rear.step;
                q.push(rear);
            }
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    int cas=1;
    while(~scanf("%d", &n) && n)
    {
        memset(vis,false,sizeof(vis));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&mp[i][j]);
        printf("Problem %d: %d\n",cas++,bfs(1,1));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值