hdu 1240 Asteroids!
来源:互联网 发布:电视直播软件tv版 编辑:程序博客网 时间:2024/06/06 03:37
主题思想: 最短xx ,首先应该想到BFS,因为一般的搜索题都是二维的。
这题是第一次三维的,主要是,三维方向变多了,从4个变成了6个,
int dir[6][3]{ 1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1};
AC 代码:
#include <iostream>#include<cstdio>#include<string>#include<cstring>#include<queue>using namespace std;const int maxn=15;bool visited[maxn][maxn][maxn];char g[maxn][maxn][maxn];int dir[6][3]{ 1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1};int n;struct Point{ int x,y,z; int step; Point(){ } Point(int xx,int yy,int zz){ x=xx;y=yy;z=zz; step=0; }};bool eq(Point a,Point b){ if(a.x==b.x&&a.y==b.y&&a.z==b.z) return true; else return false;}int BFS(int sx,int sy,int sz,int ex,int ey,int ez){ Point start=Point(sx,sy,sz); start.step=0; Point ep=Point(ex,ey,ez); ep.step=0; int ans=0; if(eq(start,ep))return 0; queue<Point> q; while(!q.empty())q.pop(); memset(visited,false,sizeof(visited)); q.push(start); visited[sx][sy][sz]=true; Point now; Point next; int xx,yy,zz; while(!q.empty()){ now=q.front(); q.pop(); for(int i=0;i<6;i++){ xx=now.x+dir[i][0]; yy=now.y+dir[i][1]; zz=now.z+dir[i][2]; if(xx==ex&&yy==ey&&ez==zz){ return now.step+1; } if(visited[xx][yy][zz]||xx>=n||xx<0||yy>=n||yy<0||zz>=n||zz<0||g[zz][xx][yy]=='X')continue; next=Point(xx,yy,zz); visited[xx][yy][zz]=true; next.step=now.step+1; q.push(next); } } return -1;}int main(){ string s; int sx,sy,sz; int ex,ey,ez; while(cin>>s){ if(s=="START"){ } scanf("%d",&n); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>s; for(int k=0;k<n;k++){ g[i][j][k]=s[k]; } } } scanf("%d%d%d",&sx,&sy,&sz); scanf("%d%d%d",&ex,&ey,&ez); cin>>s; int ans=0; ans=BFS(sx,sy,sz,ex,ey,ez); if(ans==-1)printf("NO ROUTE\n"); else printf("%d %d\n",n,ans); } return 0;}
阅读全文
0 0
- hdu 1240 Asteroids!
- HDU-1240-Asteroids!
- hdu 1240 Asteroids!
- hdu 1240 Asteroids!
- Hdu 1240 Asteroids!
- hdu 1240 Asteroids!
- hdu 1240 Asteroids!
- HDU 1240 Asteroids!
- hdu 1240 Asteroids BFS
- HDU 1240 Asteroids!
- hdu 1240 Asteroids!
- hdu-1240-Asteroids!
- HDU 1240 Asteroids!
- hdu 1240 Asteroids!bfs
- Asteroids! HDU 1240
- HDU 1240 Asteroids!(BFS)
- hdu 1240 Asteroids!(BFS)
- HDU 1240 Asteroids!
- mysql-5.7.19-winx64 免安装版配置方法
- 多分支
- zookeeper伪集群启动失败失败:Invalid config, exiting abnormally
- 中国黑客六道:用代码制作两只乌龟养养
- Hibernate框架快速入门
- hdu 1240 Asteroids!
- Python3 爬虫学习(一):urllib库的使用及简单的爬取
- 周中训练笔记2
- 密码学基础知识(学习笔记)
- Block-sparse RPCA 数学推导(最详细版)
- 计算 h-index (2)
- 次短路
- 获取list集合中重复的元素
- 网易 合唱团