• A计划(bfs,水题)


    A计划

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 27218    Accepted Submission(s): 6833


    Problem Description
    可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
    现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
     
    Input
    输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
     
    Output
    如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
     
    Sample Input
    
    
    1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
     
    Sample Output
    
    
    YES
     

    思路:这个题是一个简单的bfs()注意一些细节,不多说直接上代码

    #include<queue>
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    char map[2][15][15];
    int vis[2][15][15];
    struct edge{
    	int x,y,z,cost;
    	edge(int _x,int _y,int _z,int _cost){
    		x = _x;
    		y = _y;
    		z = _z;
    		cost = _cost;
    	}
    };
    int n,m,tt;
    int x1,y1,z1;
    int k[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    int bfs(){
    	queue<edge>que;
    	memset(vis,0,sizeof(vis));
    	que.push(edge(x1,y1,z1,0));
    	vis[x1][y1][z1] = 1;
    	while(!que.empty()){
    		edge e = que.front();que.pop();
    		for(int i = 0;i<4;i++){
    			edge ee = e;
    			ee.y+=k[i][0];
    			ee.z+=k[i][1];
    			ee.cost++;
    			if(!vis[ee.x][ee.y][ee.z]&&ee.y>=0&&ee.y<n&&ee.z>=0&&ee.z<m&&map[ee.x][ee.y][ee.z]!='*'){
    				vis[ee.x][ee.y][ee.z] = 1;
    				if(map[ee.x][ee.y][ee.z] == 'P') return ee.cost;
    				else if(map[ee.x][ee.y][ee.z] == '.'){
    					que.push(ee);
    					//printf("locate: %d %d %d %d
    ",ee.x,ee.y,ee.z,ee.cost);
    				}
    				else if(map[ee.x][ee.y][ee.z] == '#'){
    					ee.x = (ee.x + 1)%2;
    					//printf("cc %d
    ",ee.x);
    					if(map[ee.x][ee.y][ee.z] != '*' && !vis[ee.x][ee.y][ee.z]){
    						//注意这一点,因为下面这一点,wa了一天 
    						if(map[ee.x][ee.y][ee.z] == 'P') return ee.cost;
    						else{
    							vis[ee.x][ee.y][ee.z] = 1;
    							que.push(ee);
    						}
    						//printf("lllocate: %d %d %d %d
    ",ee.x,ee.y,ee.z,ee.cost);
    					}
    				}
    			}
    		}
    	}return -1;
    }
    int main()
    {
    	int t;
    	cin>>t;
    	while(t--) 
    	{
    		scanf("%d %d %d",&n,&m,&tt);
    		getchar();
    		for(int i = 0;i<2;i++){
    			for(int j = 0;j<n;j++){
    				for(int k = 0;k<m;k++){
    					scanf("%c",&map[i][j][k]);
    					if(map[i][j][k] == 'S') {
    						x1 = i;
    						y1 = j;
    						z1 = k;
    					}
    					if(i==1 && map[i][j][k]=='#' && map[(i+1)%2][j][k] == '#'){
    						map[i][j][k] = map[(i+1)%2][j][k] = '*';
    					}
    				}
    				getchar();
    			}
    			if(i!=1) getchar();
    		}
    		int ans = bfs();
    		if(ans == -1 ||ans>tt) printf("NO
    ");
    		else printf("YES
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    ClickHouse 监控及备份 (三)ClickHouse 配置
    ClickHouse 监控及备份 (二)Prometheus&Grafana 的安装
    ClickHouse 监控及备份 (一)ClickHouse 监控概述
    ClickHouse 高级(八)运维(1)常见问题排查
    ClickHouse 高级(七)MaterializeMySQL 引擎
    ClickHouse 高级(六)物化视图
    使用Iperf调整网络
    WinForm中DataGridView的使用(四)
    数据库设计经验总结
    WinForm使用Label控件模拟分割线(竖向)
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11746048.html
Copyright © 2020-2023  润新知