• HDU 1240 Asteroids!【BFS】


    题意:给出一个三维的空间,给出起点和终点,问是否能够到达终点

    和上一题一样,只不过这一题的坐标是zxy输入的,

    因为题目中说的是接下来的n行中分别是由n*n的矩形组成的,所以第一个n该是Z坐标,n*n的矩形为底面,为x,y坐标

    -----还是注意输入的方式---

    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    #include<algorithm> 
    #include<queue>
    #define maxn 55 
    using namespace std;
    int map[maxn][maxn][maxn],vis[maxn][maxn][maxn];
    int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
    int l,flag;
    char str[10005];
    struct node
    {
    	int x,y,z;
    	int step;
    } st,en;
    queue<node> q;
    void bfs(int x,int y,int z)
    {  
    	node now,next;
    	while(!q.empty()) q.pop();//调用前清空队列 
    	now.x=x;now.y=y;now.z=z;now.step=0;
    	q.push(now);
    	vis[x][y][z]=1;
    	while(!q.empty())
    	{
    		now=q.front();q.pop();	
    		for(int i=0;i<6;i++)
    		{
    			next.x=now.x+dir[i][0];
    			next.y=now.y+dir[i][1];
    			next.z=now.z+dir[i][2];
    			if(map[next.x][next.y][next.z]&&!vis[next.x][next.y][next.z])
    			{				
    				vis[next.x][next.y][next.z]=1;
    				next.step=now.step+1;//步数加1之后再如队列,因为 搞反 这个wa了好几次 
    				q.push(next);
    				if(next.x==en.x&&next.y==en.y&&next.z==en.z)
    				{
    					flag=1;
    					en.step=next.step;
    					return;
    				}
    			}			
    		}
    	}
    	return;
    }
    int main()
    {
    	char ch;
    	int i,j,k;
    	while(cin>>str>>l)
    	{	
    	    flag=0;
    		memset(map,0,sizeof(map));
    		memset(vis,0,sizeof(vis));
    		for( i=0;i<l;i++)
    		{
    			for( j=0;j<l;j++)
    			{
    				for(k=0;k<l;k++)
    				{
    					cin>>ch;
    					if(ch=='O') map[j][k][i]=1;
    				}
    			}
    		}
    		cin>>st.x>>st.y>>st.z;
    		cin>>en.x>>en.y>>en.z;
    		cin>>str;
    		if(st.x==en.x&&st.y==en.y&&st.z==en.z)
    		printf("%d 0
    ",l);
    		else
    		{
    			bfs(st.x,st.y,st.z);
    		if(flag) printf("%d %d
    ",l,en.step);
    		else printf("NO ROUTE
    ");
    		}		
    	}	
    }
    

      go------go------

  • 相关阅读:
    数据结构排序算法之堆排序
    整数划分
    二叉树的递归与非递归
    螺旋数组
    【图的DFS】图的DFS非递归算法
    Docker实战部署应用——Tomcat
    Docker实战部署应用——Redis
    Docker实战部署应用——MySQL5.7
    Docker其他操作:查看内部细节、IP、删除容器
    Docker数据目录相关操作
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4288527.html
Copyright © 2020-2023  润新知