ACM/ICPC's bfs-simple obstacle Maze problem (POJ2935)

Source: Internet
Author: User

The topic is really simple, the idea is easy to come out, the difficulty lies in the record of the obstacle, is the BFS Maze question very classic topic.

Poj2935-basic Wall Maze

Test instructions: 6*6 chessboard, there are three walls, the shortest path from a given initial point to a given endpoint, the output of one of the shortest paths in the same length.

  Problems: BFS will not say, for the record of the obstacles, my idea is to each point to record the situation of the parties upward. For example, in the east there are obstacles on the sides of the barrier to add an array of directions, to record the east side of the left point and the west of the right point of the barrier, in the BFS expansion in this direction, add a layer of cycle to determine if there is a barrier to the line, the time is not expensive, the worst time is less than O (

  

1 //simple Obstacle Maze problem2 //three wall, 6*6 checkerboard, the shortest path from a given initial point to a given endpoint, outputting either of the same shortest paths3 //the difficulty lies in the representation of the obstruction-an array of blocking direction records that can be increased for each point4 //memory:168k time:0ms5#include <iostream>6#include <cstring>7#include <cstdio>8 using namespacestd;9 Ten #defineMAX 7 One  A structPoint { -     intSize//number of obstruction directions -     intblock[3];//Obstruction Direction the     BOOLV//has been visited - }board[max][max]; -  - structState { +     intx, y; -     intFa//record a previous node +     CharD//log to the next node direction A}q[max*max +1]; at  - intSX, SY, ex, EY; - intmov[4][2] = { {0,1}, {1,0}, {0,-1}, {-1,0} };//Cardinal - Chard[5] ="ESWN";//Cardinal -  - voidGet_block () in { -     intx1, y1, x2, y2; toscanf"%d%d%d%d", &y1, &x1, &y2, &x2); +     if(X1 >x2) Swap (x1, x2); -     if(Y1 >y2) swap (y1, y2); the      while(y1 = = y2 && x1++ < x2)//Vertical Barrier *     { $board[x1][y1].block[board[x1][y1].size++] =0;Panax Notoginseng         inttx = x1 + mov[0][0]; -         intty = y1 + mov[0][1]; theboard[tx][ty].block[board[tx][ty].size++] =2; +     } A      while(x1 = = x2 && y1++ < y2)//Horizontal Barrier the     { +board[x1][y1].block[board[x1][y1].size++] =1; -         inttx = x1 + mov[1][0]; $         intty = y1 + mov[1][1]; $board[tx][ty].block[board[tx][ty].size++] =3; -  -     } the } - Wuyi //Recursive output the voidoutput (State t) - { Wu     if(T.FA) { - output (Q[t.fa]); Aboutprintf"%c", T.D); $     } - } -  - voidBFS () A { +memset (Q,0,sizeof(q)); the     intFront =1, tail =2; -q[front].x =SX; $Q[FRONT].Y =Sy; theBOARD[SX][SY].V =true; the      while(Front <tail) the     { the         intx =q[front].x; -         inty =q[front].y; in          for(inti =0; I <4; i++) the         { the             BOOLFlag =true;//you can move in the current direction . About              for(intj =0; J < Board[x][y].size; J + +) the             { the                 if(i = =Board[x][y].block[j]) the                 { +Flag =false; Break; -                 } the             }Bayi              the             if(flag)//can go forward the             { - State T; -T.x = x + mov[i][0]; theT.y = y + mov[i][1]; theT.D =D[i]; theT.FA =Front; the                 if(T.x = = Ex && T.y = = ey)//Destination -                 { the output (t); theprintf"\ n"); the                     return;94                 } the                 if(T.x >0&& T.x < MAX && T.y >0&& T.y < MAX &&!board[t.x][t.y].v) the                 { theBOARD[T.X][T.Y].V =true;98q[tail++] =T; About                 } -             }101         }102front++;103     }104 } the 106 intMain ()107 {108      while(SCANF ("%d%d", &sy, &AMP;SX), Sy &&SX)109     { thescanf"%d%d", &ey, &ex);111  thememset (board,0,sizeof(board));113          for(inti =0; I <3; i++) the Get_block (); the  the         //if (sx = = Ex && sy = = ey)117         //printf ("\ n");118         //Else119 BFS (); -     }121 122     return 0;123}

ACM/ICPC's bfs-simple obstacle Maze problem (POJ2935)

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.