当前位置: 代码迷 >> 综合 >> HDU--1240--Asteroids--三维迷宫模板题
  详细解决方案

HDU--1240--Asteroids--三维迷宫模板题

热度:64   发布时间:2023-12-12 06:18:26.0

题目描述:https://vjudge.net/problem/HDU-1240/origin;

思路:bfs()模板题目;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring> 
#include<queue>
using namespace std;
const int maxa=15;
int map[maxa][maxa][maxa],vis[maxa][maxa][maxa];
int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,-1,0,0,0,0};
int n;
int qx,qy,qz,zx,zy,zz;//起点和终点 
struct node{int xx,yy,zz;int step;
};
void bfs(int x,int y,int z){memset(vis,0,sizeof vis);queue<node>Q;node now,next;now.zz=z;now.xx=x;now.yy=y;now.step=0;vis[z][x][y]=1;Q.push(now);while(!Q.empty()){now=Q.front();Q.pop();if(now.zz==zz&&now.yy==zy&&now.xx==zx){printf("%d %d\n",n,now.step);return ;}for(int i=0;i<6;i++){next.xx=now.xx+dx[i];next.yy=now.yy+dy[i];next.zz=now.zz+dz[i];next.step=now.step+1;if(map[next.zz][next.xx][next.yy]&&!vis[next.zz][next.xx][next.yy]){vis[next.zz][next.xx][next.yy]=1;Q.push(next);}}}printf("NO ROUTE\n");
}
int main(){char s[20],c;while(~scanf("%s%d",s,&n)){memset(map,0,sizeof map);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){getchar();for(int k=1;k<=n;k++){scanf("%c",&c);if(c=='O')	map[i][j][k]=1;//可移动的点哦 else if(c=='X')	map[i][j][k]=0;}}scanf("%d%d%d%d%d%d",&qx,&qy,&qz,&zx,&zy,&zz);qx++;qy++;qz++;zx++;zy++;zz++;getchar();gets(s);bfs(qx,qy,qz);}return 0;
}