POJ 2935 BFS

Source: Internet
Author: User

Give the matrix of 6*6, the starting point, the end point, altogether three walls, the wall will not intersect.

Minimum step to the end of the starting point to ensure a solution

Whether there is a wall that corresponds to each move judgment


#include "stdio.h" #include "string.h" #include "queue" using namespace Std;const int dir[4][2]={{1,0},{-1,0},{0,1},{0,- 1}};int wall[10][10][10][10];int s_x,s_y,e_x,e_y;int used[10][10];struct node{int x, y;};    void pri (int x,int y) {if (used[x][y]==0) return;        if (used[x][y]==1) {pri (x-1,y);    printf ("S");        } if (used[x][y]==2) {pri (x+1,y);    printf ("N");        } if (used[x][y]==3) {pri (x,y-1);    printf ("E");        } if (used[x][y]==4) {pri (x,y+1);    printf ("W");    }}void BFs () {queue<node>q;    Node Cur,next;    int i;    cur.x=s_x;    cur.y=s_y;    Q.push (cur);    memset (used,-1,sizeof (used));    used[cur.x][cur.y]=0;        while (!q.empty ()) {Cur=q.front ();        Q.pop ();            for (i=0;i<4;i++) {next.x=cur.x+dir[i][0];            NEXT.Y=CUR.Y+DIR[I][1];            if (next.x<=0 | | next.x>6 | | next.y<=0 | | next.y>6) continue; if (i==0 &&            Wall[cur.x][cur.y-1][cur.x][cur.y]==1) continue;            if (i==1 && wall[next.x][next.y-1][next.x][next.y]==1) continue;            if (i==2 && wall[cur.x-1][cur.y][cur.x][cur.y]==1) continue;            if (i==3 && wall[next.x-1][next.y][next.x][next.y]==1) continue;            if (used[next.x][next.y]!=-1) continue;            used[next.x][next.y]=i+1;            Q.push (next);                if (next.x==e_x && next.y==e_y) {pri (NEXT.X,NEXT.Y);                printf ("\ n");            return;    }}}}int Main () {int i,j,x1,x2,y1,y2,temp;        while (scanf ("%d%d", &s_y,&s_x)!=eof) {if (s_x+s_y==0) break;        scanf ("%d%d", &e_y,&e_x);        memset (wall,0,sizeof (wall));            for (i=1;i<=3;i++) {scanf ("%d%d%d%d", &y1,&x1,&y2,&x2);                if (x1==x2) {if (y1>y2) {temp=y1; y1=y2; y2=temp;} FoR (j=y1;j<y2;j++) wall[x1][j][x1][j+1]=1;                } else {if (x1>x2) {temp=x1; x1=x2; x2=temp;}            for (j=x1;j<x2;j++) wall[j][y1][j+1][y1]=1;    }} BFS (); } return 0;}


POJ 2935 BFS

Related Article

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.