Title Link: http://poj.org/problem?id=3984
This is originally a template problem, but the teacher to go can not use the STL in the queue, have their own hand-written solution, ORZ .... Watch someone else's blog learn, new skills get ...
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue > #include <map> #include <stack> #include <set> #include <vector> #include <algorithm> #define LL Long longusing namespace Std;int map[10][10];int last=0,total=1; Total is the overall queue element, last is the pioneer tag; int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};struct node{int x,y,pre;} Q[30];bool Isok (int x,int y)//judgment time inside the maze, decide when to continue searching; {if (x<0| | y<0| | x>4| | y>4| | Map[x][y]) return false; else return 1;} void print (int i)//custom output function, call recursion, use recursive principle can easily output from backward; {if (q[i].pre!=-1) {//pioneer = 1-bit starting point; Print (q[ I].PRE); printf ("(%d,%d) \ n", q[i].x,q[i].y); }}void BFS (int x,int y) {q[last].x=x; Q[last].y=y; Q[last].pre=-1; Starting point, the pioneer Mark is-1; while (last<total) {//Determines whether the queue is empty; for (int i=0;i<4;i++) {//four direction search; int a=q[last].x+dir[i][0]; int b=q[last].y+dir[i][1]; if (Isok (b)) {//cout<<a<< ' <<b<<endl; Map[a][b]=1; Q[total].x=a; Q[total].y=b; Q[total].pre=last; record pioneer; total++; Queue;} if (a==4&&b==4) {print (last); }} last++; }}int Main () {for (int. i=0;i<5;i++) {for (int j=0;j<5;j++) {scanf ("%d", &map[i][j]) ; }} printf ("(0, 0) \ n"); BFS (0,0); printf ("(4, 4) \ n"); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
poj-3984-Maze problem-bfs (wide search)-handwriting queue