zoukankan      html  css  js  c++  java
  • NYOJ 353 3D dungeon 【bfs】

    题意:给你一个高L长R宽C的图形。每个坐标都能够视为一个方格。你一次能够向上。下。左,右,前,后任一方向移动一个方格, 可是不能向有#标记的方格移动。

    问:从S出发能不能到达E,假设能请输出最少的移动次数。

    策略:简单的深搜。

    注意:由于是求最少的移动次数。所以要从全部能到达的中选出最少的。

    代码:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    #define INF 0x3f3f3f3f
    char map[35][35][35];
    int ans, l, r, c;
    const int dirx[6] = {1, -1, 0, 0, 0, 0};
    const int diry[6] = {0, 0, 1, -1, 0, 0};
    const int dirz[6] = {0, 0, 0, 0, 1, -1};
    struct node{
    	int x, y, z;
    	int step;
    };
    node st, en;
    queue<node > q;
    char s[10000];
    bool vis[35][35][35];
    
    int limit(node s){
    	return (s.x<l&&s.x>=0&&s.y>=0&&s.y<r&&s.z>=0&&s.z<c&&map[s.x][s.y][s.z] != '#');
    }
    
    int match(node s){
    	if(s.x==en.x&&s.y==en.y&&s.z==en.z) return 1;
    	else return 0;
    }
    void bfs(){
    	memset(vis, 0, sizeof(vis));
    	ans = INF;  //初始化最大
    	int i;
    	q.push(st);
    	//map[st.x][st.y][st.z] = '#';
    	vis[st.x][st.y][st.z] = 1;
    	while(!q.empty()){
    		node temp = q.front();
    		
    		for(i = 0; i < 6; i ++){
    			node temp2;
    			temp2.x = temp.x+dirx[i];
    			temp2.y = temp.y+diry[i];
    			temp2.z = temp.z+dirz[i];
    			temp2.step = temp.step+1;
    			//printf("%d %d %d %d..", temp2.x, temp2.y, temp2.z, temp2.step);
    			if(limit(temp2)&&!vis[temp2.x][temp2.y][temp2.z]){
    				if(match(temp2)){
    					ans = ans<temp2.step?

    ans:temp2.step; //要bfs完所有的 } else{ q.push(temp2); vis[temp2.x][temp2.y][temp2.z] = 1; } } } // if(ans) break; q.pop(); } if(ans == INF) printf("Trapped! "); else printf("Escaped in %d minute(s). ", ans); } int main(){ int i, j, k; while(scanf("%d%d%d", &l, &r, &c), l||r||c){ memset(map, 0, sizeof(map)); for(i = 0; i < l; i ++){ for(j = 0; j < r; j ++){ scanf("%s", map[i][j]); for(k = 0; k <c; k ++){ if(map[i][j][k] == 'S'){ st.x = i; st.y = j; st.z = k; st.step = 0; } if(map[i][j][k] == 'E'){ en.x =i; en.y = j; en.z = k; en.step = 0; } } } gets(s); } bfs(); } return 0; }



  • 相关阅读:
    Pika的设计及实现
    高性能网络编程
    C语言的结构体
    消息队列库——ZeroMQ
    Diffie-Hellman密钥交换算法
    mysql-proxy 读写分离
    位运算
    分布式学习之一:事务
    Redis Cluster
    SpringBoot整合ActiveMQ,看这篇就够了
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7223687.html
Copyright © 2011-2022 走看看