zoukankan      html  css  js  c++  java
  • poj 2251 Dungeon Master (bfs)

    刷水题好爽!但是别真跟xcy说的一样,把rp都给用完了...

    三维空间的bfs,只是搜索的时候多了两个方向而已。

    code:

    #include<cstdio>
    #include<cstring>
    bool vis[31][31][31] ;
    int lve[6] = {10000, -1} ;
    int row[6] = {0, -10010} ;
    int col[6] = {00, -1100} ;
    bool flag ;
    int a, b, c ;
    struct node{
        int x ;
        int y ;
        int z ;
        int step ;
    }q[1000000] ;
    node s, e ;
    bool in(node t){
        if(t.x>=0&&t.x<a&&t.y>=0&&t.y<b&&t.z>=0&&t.z<c) return true ;
        return false ;
    }
    void bfs(){
        int h, r ;
        h = 0 ;
        r = 1 ;
        q[0] = s ;
        int j=0 ;
        while(r>h){
            node p = q[h++] ;
            for(int i=0; i<6; i++){
                node temp = p ;
                temp.x += lve[i] ;
                temp.y += row[i] ;
                temp.z += col[i] ;
                if(in(temp)&&!vis[temp.x][temp.y][temp.z]){
                    if(temp.x==e.x&&temp.y==e.y&&temp.z==e.z){
                        printf("Escaped in %d minute(s).\n", temp.step+1) ;
                        flag = true ;
                        return ;
                    }
                    vis[temp.x][temp.y][temp.z] = true ;
                    temp.step ++ ;
                    q[r++] = temp ;
                }
            }
        }
    }
    int main(){
        int i, j, k ;
        char str[31] ;
        while(~scanf("%d%d%d", &a, &b, &c)&&(a+b+c)){
            memset(vis, falsesizeof(vis)) ;
            for(i=0; i<a; i++){
                for(j=0; j<b; j++){
                    getchar() ;
                    scanf("%s", str) ;
                    for(k=0; k<c; k++){
                        if(str[k]=='S'){
                            s.x = i ;
                            s.y = j ;
                            s.z = k ;
                            s.step = 0 ;
                            vis[i][j][k] = true ;
                        }else if(str[k]=='E'){
                            e.x = i ;
                            e.y = j ;
                            e.z = k ;
                        }else if(str[k]=='#')
                            vis[i][j][k] = true ;
                    }
                }
            }
            flag = false ;
            bfs() ;
            if(!flag)   printf("Trapped!\n") ;
        }
        return 0 ;

  • 相关阅读:
    activiti 清库脚本(转)
    T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)
    Chrome firefox ie等浏览器空格(&nbsp;)兼容问题
    activiti5.22整合modeler时出错TypeError: Cannot read property 'split' of undefined
    activiti如何获取当前节点以及下一步路径或节点(转)
    Activiti5 添加/查询审批批注(审批意见)
    Django中多对多关系的orm表设计
    css背景雪碧图等
    Django图书管理系统(前端对有外键的数据表增删改查)
    Django图书管理系统(前端对数据库的增删改查)
  • 原文地址:https://www.cnblogs.com/xiaolongchase/p/2353510.html
Copyright © 2011-2022 走看看