HDU1240-detailed explanation of the subject meaning (3D BFS), hdu1240-bfs

Source: Internet
Author: User

HDU1240-detailed explanation of the subject meaning (3D BFS), hdu1240-bfs

Enter start n to indicate that the number of layers of the cube is N, and each layer is an NxN square .. It is actually a cube of NxNxN, Which is input layer by layer.

After entering the cube, enter the coordinates of the start and end points.

The output is the number of steps that output N first and then the shortest path. If the destination cannot be reached, no route is output.

Pitfall point: the start point and end point coordinates do not match the cube we entered.

#include <cstdio>#include <cstring>#include <iostream>#include <queue>#include <string>#define M 11using namespace std;struct node{    int x,y,z;    int t;};int dir[6][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};int N;int sa,sb,sc,ea,eb,ec;char maze[M][M][M];bool judge(int x,int y,int z){    if(x<0||x>=N||y<0||y>=N||z<0||z>=N){            return true;    }    if(maze[x][y][z]=='X')  {            return true;    }    return 0;}int bfs(){    queue<node> myque;    node now,next;    now.x=sa;    now.y=sb;    now.z=sc;    now.t=0;    myque.push(now);    if(now.x==eb&&now.y==ec&&now.z==ea) {        return now.t;    }    while(!myque.empty())    {        now=myque.front();        myque.pop();        for(int i=0;i<6;i++)        {            next.x=now.x+dir[i][0];            next.y=now.y+dir[i][1];            next.z=now.z+dir[i][2];            next.t=now.t+1;            if(judge(next.x,next.y,next.z)) {                continue;            }            if(next.x==ea&&next.y==eb&&next.z==ec) {                return next.t;            }            maze[next.x][next.y][next.z]='X';            myque.push(next);        }    }    return -1;}int main(){   string s;   while(cin>>s,scanf("%d",&N)!=EOF){       for(int i=0;i<N;i++)        {            for(int j=0;j<N;j++)            {                for(int k=0;k<N;k++)                {                    cin>>maze[i][j][k];                }            }       }       cin>>sb>>sc>>sa>>eb>>ec>>ea>>s;       int anw;       anw=bfs();       if(anw!=-1)      printf("%d %d\n",N,anw);       else if(anw==-1) printf("NO ROUTE\n");   }}



What is the [BFS] Mark of Beijing Foreign Studies University?

Beijing foreign studies university

In gossip girl, what is BFS when B and S are "BFS" in the first episode of the first season? Is it an abbreviation?

Should it be BFF?

B And S are always friends of best friend forever

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.