zoukankan      html  css  js  c++  java
  • 三维搜索

    hud 1240 Asteroids!.cpp

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<stack>
    #include<cstdio>
    #include<queue>
    #include<map>
    #include<vector>
    #include<set>
    using namespace std;
    const int maxn=1010;
    const int INF=0x3fffffff;
    typedef long long LL;
    //三维BFS
    struct node{
    	int x,y,z;
    	int num;
    };
    int dis[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};
    int n;
    int sx,sy,sz,ex,ey,ez;
    int mp[12][12][12];
    int step[12][12][12];
    int tot=0;
    void bfs(int z,int x,int y){
    	node a;
    	a.x=sx;a.y=sy;a.z=sz;
    	a.num=0;
    	step[sz][sx][sy]=0;
    	queue<node> q;
    	q.push(a);
    	while(!q.empty()){
    		node u=q.front();
    		q.pop();
    		//先判断
    		if(u.x==ex&&u.z==ez&&u.y==ey){
    			tot=u.num;
    			return;
    		} 
    		for(int i=0;i<6;i++){
    			int xx=u.x+dis[i][0];
    			int yy=u.y+dis[i][1];
    			int zz=u.z+dis[i][2];
    			if(xx>=0&&xx<n&&yy>=0&&yy<n&&zz>=0&&zz<n&&mp[zz][xx][yy]!=INF){
    				if(step[zz][xx][yy]>step[u.z][u.x][u.y]+1){
    					step[zz][xx][yy]=step[u.z][u.x][u.y]+1;
    					mp[zz][xx][yy]=INF;
    					node next;
    					next.x=xx;next.y=yy;next.z=zz;
    					next.num=u.num+1;
    					q.push(next);
    				}
    			}
    		}
    	}
    }
    int main(){
    	char st[21],ed[21],op;
    	while(scanf("%s %d",st,&n)!=EOF){
    		for(int i=0;i<n;i++){
    			for(int j=0;j<n;j++){
    				for(int z=0;z<n;z++){
    					cin>>op;
    					if(op=='X') mp[i][j][z]=INF;
    					if(op=='0') mp[i][j][z]=1;
    					step[i][j][z]=INF;
    				}
    			}
    		}
    		cin>>sx>>sy>>sz>>ex>>ey>>ez;
    		cin>>ed;
    		tot=0;
    		bfs(sz,sx,sy);
    		if(step[ez][ex][ey]!=INF){
    			cout<<n<<" "<<tot<<endl;
    		}
    		else cout<<"NO ROUTE"<<endl;
    	}
    	
    	
    return 0;
    }
    

      

  • 相关阅读:
    Oracle FGA审计记录的清理步骤
    UVa11488-Hyper Prefix Sets(trie树)
    配置Log4j(非常具体)
    poj1190生日蛋糕
    zju1610Count the Colors
    【例9.3】求最长不下降序列
    P1364 医院设置
    P1629 邮递员送信
    P1476 休息中的小呆
    P1330 封锁阳光大学
  • 原文地址:https://www.cnblogs.com/shirlybaby/p/12390912.html
Copyright © 2011-2022 走看看