HDU1240

来源:互联网 发布:煎饼侠 知乎 编辑:程序博客网 时间:2024/05/22 10:44

三维BFS


#include <iostream>

#include <stdio.h>
#include <string.h>
#include <queue>


using namespace std;


struct node
{
    int x,y,z;
    int step;
} st,end;




int n,res;
char maps[15][15][15];
int move[6][3] = {{0,1,0}, {0,-1,0}, {-1,0,0}, {1,0,0}, {0,0,1}, {0,0,-1}};


bool check(node now)
{
    if(now.x >= 0 && now.x < n && now.y >= 0 && now.y < n && now.z >= 0 && now.z < n)
     return true;
    return false;
}


int bfs()
{
    node temp,now;


    queue<node>que;
    while(!que.empty())
        que.pop();


    st.step = 0;
    maps[st.x][st.y][st.z] = 'X';
    que.push(st);


    if( st.x == end.x && st.y == end.y && st.z == end.z)
          return res;




    while(!que.empty())
    {
        temp = que.front();
        que.pop();

        for( int i = 0; i<6; i++)
        {
            now.x = temp.x + move[i][0];
            now.y = temp.y + move[i][1];
            now.z = temp.z + move[i][2];


        if(now.x == end.x && now.y == end.y && now.z == end.z)
          return temp.step + 1;


          if(check(now))
          {
              if(maps[now.x][now.y][now.z] == 'O')
               {
                   now.step = temp.step + 1;
                   //cout<<now.x<<" x "<< now.y<<" Y "<<now.z<<" Z "<<endl;
                   maps[now.x][now.y][now.z] = 'X';
                   que.push(now);
               }
          }


        }
    }
    return -1;
}


int main()
{


    int i,j;
    char s[8],e[8];
    while(scanf("%s%d",s,&n)!= EOF)
    {
        getchar();
        for( int i = 0; i<n; i++)
         {
             for( int j = 0; j<n; j++)
               scanf("%s", maps[i][j]);
            getchar();         //一定要注意
         }


      scanf("%d%d%d",&st.x, &st.y, &st.z);
      scanf("%d%d%d",&end.x, &end.y, &end.z);


      getchar();    //一定要注意
      gets(e);


      res = 0;
      res = bfs();
      if(res >= 0)
       printf("%d %d\n",n,res);
      else
       printf("NO ROUTE\n");


    }


    return 0;


}
0 0
原创粉丝点击