HDU 1240 Asteroids! 三维空间BFS
2016-02-24 23:15
351 查看
Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4481 Accepted Submission(s): 2891
Problem Description
You’re in space.
You want to get home.
There are asteroids.
You don’t want to hit them.
Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 5 components:
Start line - A single line, “START N”, where 1 <= N <= 10.
Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:
‘O’ - (the letter “oh”) Empty space
‘X’ - (upper-case) Asteroid present
Starting Position - A single line, “A B C”, denoting the
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4481 Accepted Submission(s): 2891
Problem Description
You’re in space.
You want to get home.
There are asteroids.
You don’t want to hit them.
Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 5 components:
Start line - A single line, “START N”, where 1 <= N <= 10.
Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:
‘O’ - (the letter “oh”) Empty space
‘X’ - (upper-case) Asteroid present
Starting Position - A single line, “A B C”, denoting the
#include <iostream> #include <string> #include <cstring> #include <queue> using namespace std; struct node{ int x,y,z; int step; friend bool operator<(node a,node b) { return a.step>b.step; } }; int N; char edge[15][15][15]; int sta[15][15][15]; int sx,sy,sz,ex,ey,ez; int dirx[6]={1,-1,0,0,0,0}; int diry[6]={0,0,1,-1,0,0}; int dirz[6]={0,0,0,0,1,-1}; int mina; bool Judge(int x,int y,int z,int t) { if(x<0||x>=N||y<0||y>=N||z<0||z>=N||edge[x][y][z]=='X'||sta[x][y][z]<=t) return true; return false; } bool BFS() { priority_queue<node>Q; node cur,next; cur.x=sx; cur.y=sy; cur.z=sz; cur.step=0; sta[sx][sy][sz]=0; Q.push(cur); while(!Q.empty()) { cur=Q.top(); Q.pop(); if(cur.x==ex&&cur.y==ey&&cur.z==ez) { mina=cur.step; return true; } for(int i=0;i<6;i++) { next.x=cur.x+dirx[i]; next.y=cur.y+diry[i]; next.z=cur.z+dirz[i]; next.step=cur.step+1; if(next.x==ex&&next.y==ey&&next.z==ez) {Q.push(next); continue; } if(Judge(next.x,next.y,next.z,next.step)) continue; sta[next.x][next.y][next.z]=next.step; Q.push(next); } } return false; } int main(int argc, char *argv[]) { string s1,s2; while(cin>>s1) { //if(s1=="END") //cout<<11<<endl; //break; cin>>N; for(int k=0;k<N;k++) for(int i=0;i<N;i++) for(int j=0;j<N;j++) cin>>edge[k][i][j]; cin>>sx>>sy>>sz; cin>>ex>>ey>>ez; cin>>s2; if(sx==ex&&sy==ey&&sz==ez) { cout<<N<<" "<<0<<endl; } else { for(int k=0;k<N;k++) for(int i=0;i<N;i++) for(int j=0;j<N;j++) sta[i][j][k]=999; if(BFS()) { cout<<N<<" "<<mina<<endl; } else cout<<"NO ROUTE"<<endl; } } return 0; }
相关文章推荐
- hdu4283 You Are the One(区间dp,思考)
- PHP开发Apache服务器配置
- 帝国CMS 标签 总结1
- 帝国CMS标签 总结2
- Android中检测当前是否为主线程
- Spring之依赖注入
- JSP页面的三种include方式
- db link的使用
- 关于visual studio 2013生成exe文件找不到问题
- Exchange2010如何防止外部用户冒用内部用户地址给内部发邮件?
- php开发中的页面跳转方法总结
- IPC机制 基础知识
- Objective-C中字符串(NSString和NSMutableString)常用的方法
- swift基本语法(总结提炼版)之009 swift 之字符串
- Mysql安装
- AVAudioPlayer音频播放器-备用
- 快速排序算法
- 非常实用的PHP常用函数汇总
- 1047. 编程团体赛
- 进程管理