标签:ignatius and the pri hdu 1026 优先队列 bfs+输出路径
5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX. 5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX1 5 6 .XX... ..XX1. 2...X. ...XX. XXXXX.
It takes 13 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) FINISH It takes 14 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) 14s:FIGHT AT (4,5) FINISH God please help our poor hero. FINISH
题意:给出n*m的地图,求从(0,0)走到(n-1,m-1)最短时间,方格上的数字表示杀怪所要的时间。
思路:优先队列+bfs,输出路径的时候用一个path数组记录,path[i][j]=d表示(i,j)位置是由d方向过来的。
代码:
#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 105;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005;
struct Node
{
int x,y,step;
bool operator<(const Node &a)const
{
return step>a.step;
}
};
int n,m,cnt;
int dir[4][2]={1,0,0,1,0,-1,-1,0};
char mp[maxn][maxn];
int vis[maxn][maxn];
int path[maxn][maxn];
bool isok(int x,int y)
{
if (x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='X') return true;
return false;
}
void out(int x,int y)
{
if (path[x][y]==-1) return ;
int dx=x-dir[path[x][y]][0];
int dy=y-dir[path[x][y]][1];
out(dx,dy);
if (isdigit(mp[dx][dy]))
{
for (int i=0;i<mp[dx][dy]-'0';i++)
printf("%ds:FIGHT AT (%d,%d)\n",cnt++,dx,dy);
}
printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,dx,dy,x,y);
}
int bfs()
{
Node st,now;
mem(path,-1);
mem(vis,0);
st.x=st.y=st.step=0;
vis[0][0]=1;
priority_queue<Node>Q;
Q.push(st);
while (!Q.empty())
{
st=Q.top();
Q.pop();
if (st.x==n-1&&st.y==m-1)
return st.step;
for (int i=0;i<4;i++)
{
now.x=st.x+dir[i][0];
now.y=st.y+dir[i][1];
if (isok(now.x,now.y)&&!vis[now.x][now.y])
{
vis[now.x][now.y]=1;
now.step=st.step+1;
if (isdigit(mp[now.x][now.y])) now.step=now.step+mp[now.x][now.y]-'0';
path[now.x][now.y]=i;
Q.push(now);
}
}
}
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j;
while (~scanf("%d%d",&n,&m))
{
for (i=0;i<n;i++)
scanf("%s",mp[i]);
int ans=bfs();
if (ans==0) printf("God please help our poor hero.\nFINISH\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
cnt=1;
out(n-1,m-1);
if (isdigit(mp[n-1][m-1]))
for (i=0;i<mp[n-1][m-1]-'0';i++)
printf("%ds:FIGHT AT (%d,%d)\n",cnt++,n-1,m-1);
printf("FINISH\n");
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)
标签:ignatius and the pri hdu 1026 优先队列 bfs+输出路径
原文地址:http://blog.csdn.net/u014422052/article/details/47704897