HDU1026 BFS 優先佇列 路徑儲存

2021-06-19 08:33:44 字數 1154 閱讀 5938

剛開始做bfs專題時遇到它,因為路徑儲存放棄了

現在做,剛開始各種wa。。。。。

後來加了個優先佇列,就ac了。。額。。

#include#include#include#include#include#include#include#include #include #include#includeusing namespace std;

#define inf 2147483647

#define eps 1e-8

#define ll long long

#define m 50005

#define mol 1000000007

int mov[4][2]=,,,};

struct node;};

int n,m,vis[105][105];

char s[105][105];

int pd(int x,int y)

{ if(x>=0&&x=0&&yq;

int ans=0,k,dx,dy;

f[n-1][m-1].x=-1;//終點標誌

vis[n-1][m-1]=1;

node p,q;

p.x=n-1;

p.y =m-1;

p.step =0;

if(s[n-1][m-1]!='.')

p.step =s[n-1][m-1]-'0';

q.push(p);

while(!q.empty())

{p=q.top ();

q.pop();

if(p.x==0&&p.y==0)

{printf("it takes %d seconds to reach the target position, let me show you the way.\n",p.step);

k=1;

int a=p.x;//當前點

int b=p.y;

while(f[a][b].x!=-1)

{int c=f[a][b].x;//當前點的前一點

int d=f[a][b].y;

printf("%ds:(%d,%d)->(%d,%d)\n",k++,a,b,c,d);

if(s[c][d]!='.')

{for(int l=0;l

HDU 1026 (BFS 記錄路徑)

bfs 路徑記錄 problem id hdu 1026 author id fuqiang11 time 2013 07 17 algorithm bfs status accept include include include using namespace std define inf 0x...

HDU1026(BFS求最短路徑並記錄)

一.題意 英雄去救公主,起點 0,0 終點 row 1,col 1 x 不能走,遇到數字停下來打怪,數字多少就要打多少秒,求最快到達時間。二.解題思路 看結構體怎麼定義的就直接知道我怎麼記錄路徑的,記得路徑的陣列要開大一些。三.ac include include include include u...

1026優先佇列

include include using namespace std int n,m,a 2 4 struct node struct labyrinth maze 100 100 int bfs else maze n 1 m 1 c x q.push now while q.empty maz...