poj-3984-Maze problem-bfs (wide search)-handwriting queue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.