标签:hdu1026 ignatius and the pri 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
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <queue>
#define MAX 110
using namespace std ;
const int dir[4][2]={-1,0,1,0,0,-1,0,1} ;
int m , n , visited[MAX][MAX];
char map[MAX][MAX] ;
struct Point{
int x , y , step;
bool operator<(const Point &p) const
{
return step>p.step ;
}
} ;
struct Pre{
int x , y ;
}pre[MAX][MAX] ;
bool judge(Point p)
{
if(p.x<0||p.y<0||p.x>=n||p.y>=m)
{
return false ;
}
return true ;
}
void BFS()
{
Point s ;
s.x = n-1;
s.y = m-1;
s.step = 0 ;
visited[n-1][m-1] = 0 ;
pre[s.x][s.y].x = pre[s.x][s.y].y = -1 ;
priority_queue<Point> que ;
que.push(s) ;
while(!que.empty())
{
Point now = que.top();
que.pop() ;
if(now.x == 0 && now.y == 0)
{
int x = now.x ;
int y = now.y ;
int t = 1 ;
if(map[n-1][m-1]>'0' && map[n-1][m-1]<='9')
printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.step+map[n-1][m-1]-'0');
else
printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.step);
while(pre[x][y].x != -1)
{
int tempx = pre[x][y].x ;
int tempy = pre[x][y].y ;
printf("%ds:(%d,%d)->(%d,%d)\n",t++,x,y,tempx,tempy) ;
if(map[tempx][tempy] != '.')
{
for(int i = 0 ; i < map[tempx][tempy]-'0' ; ++i)
{
printf("%ds:FIGHT AT (%d,%d)\n",t++,tempx,tempy) ;
}
}
x = tempx ;
y = tempy ;
}
return ;
}
for(int i = 0 ; i < 4 ; ++i)
{
Point next ;
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1] ;
if(!judge(next) || map[next.x][next.y]=='X')
{
continue ;
}
if(map[next.x][next.y]>'0' && map[next.x][next.y]<='9')
{
if(visited[next.x][next.y]==-1||visited[next.x][next.y]>now.step+1+map[next.x][next.y]-'0')
{
visited[next.x][next.y] = now.step+1+map[next.x][next.y]-'0' ;
pre[next.x][next.y].x = now.x ;
pre[next.x][next.y].y = now.y ;
next.step = now.step+1+map[next.x][next.y]-'0';
que.push(next) ;
}
}
else
{
if(visited[next.x][next.y]==-1||visited[next.x][next.y]>now.step+1)
{
visited[next.x][next.y] = now.step+1 ;
pre[next.x][next.y].x = now.x ;
pre[next.x][next.y].y = now.y ;
next.step = now.step+1 ;
que.push(next) ;
}
}
}
}
puts ("God please help our poor hero.");
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(visited,-1,sizeof(visited)) ;
for(int i = 0 ; i < n ; ++i)
{
scanf("%s",map[i]);
}
BFS() ;
puts("FINISH") ;
}
return 0 ;
}
/*
5 6
.XX.1.
..X.5.
2...4.
...XX.
XXXXX.
13
6 7
.XX.1.X
..X.5.X
2...4.2
...XX.X
XXXXX3.
..XXX.1
19
*/hdu 1026 Ignatius and the Princess I 广搜+优先队列+记录路径
标签:hdu1026 ignatius and the pri bfs 优先队列
原文地址:http://blog.csdn.net/lionel_d/article/details/44056029