Topic links
is mainly the output path problem;
Pre[x][y] means that the arrival (x, y) is the point (pre[x][y].x, PRE[X][Y].Y);
#include <stdio.h>#include<iostream>#include<string.h>#include<queue>#include<algorithm>using namespacestd;#defineN 220#defineINF 0XFFFFFFFintdir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };intMap[n][n];intVis[n][n], N, m;structnode{intx, y, step; FriendBOOL operator<(Node A, Node B) {returnA.step>B.step; }}pre[n][n],path[n];node BFs () {memset (Vis,0,sizeof(VIS)); vis[0][0] =1; Priority_queue<node>Q; Node p,q; P.x= P.Y = P.step =0; Q.push (P); while(Q.size ()) {p=Q.top (); Q.pop (); if(p.x = =4&& P.Y = =4) returnp; for(intI=0; i<4; i++) {q.x=p.x+dir[i][0]; Q.y=p.y+dir[i][1]; if(q.x>=0&&q.x<5&& q.y>=0&&q.y<5&& map[q.x][q.y]==0&& vis[q.x][q.y]==0) {VIS[Q.X][Q.Y]=1; Q.step= P.step +1; pre[q.x][q.y].x=p.x; Pre[q.x][q.y].y=p.y; Q.push (q); } } }}intMain () { for(intI=0; i<5; i++) { for(intj=0; j<5; J + +) scanf ("%d",&Map[i][j]); } node ans=BFS (); intStep = ans.step, x = ans.x, y =Ans.y; for(inti=step-1; i>=0; i--) {path[i].x=pre[x][y].x; Path[i].y=pre[x][y].y; X=path[i].x; Y=path[i].y; } for(intI=0; i<step;i++) {printf ("(%d,%d) \ n", path[i].x, PATH[I].Y); } printf ("(4, 4) \ n"); return 0;}
View Code
Maze Problem---poj3984 (BFS, Output path problem)