您的位置:首页 > 其它

【搜索】 HDU 3152 Obstacle Course

2014-10-07 19:46 155 查看
题目连接点击打开链接

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: