`

2013程序设计实现之广搜作业 B

阅读更多

/**

***= =最近懒得没有发表一篇文章,自觉羞愧...于是发表几篇小文以弥胡哥对我的期盼之情...

翻译一下: 一个房间,3维的,指定他的3个维度的大小,然后指定他的每个单位的内容(= =w 天哪poj你好烦),#代表该单位无法通过,.代表可以通过,S代表可怜的主人公所在位置,E代表光明的出口所在的位置。求该主人公能否逃脱,能在最少多少次的操作后逃脱(话说这题翻译过来是地下城...好吧,槽点略多)

描述
You are trapped in a 3D dungeon and need to find the quickest way out!
 The dungeon is composed of unit cubes which may or may not be filled with rock.
  It takes one minute to move one unit north, south, east, west, up or down.
   You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
输入
The input consists of a number of dungeons.
 Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon.
 A cell full of rock is indicated by a '#' and empty cells are represented by a '.'.
  Your starting position is indicated by 'S' and the exit by the letter 'E'.
  There's a single blank line after each level.
   Input is terminated by three zeroes for L, R and C.
输出
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!

 

样例输入
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!
*/
#include<iostream>
#include<string.h>
#include<string>
#include<queue>
using namespace std;
//

class Point{
      public:
             Point(int x,int y,int z){
                       this->x = x;
                       this->y = y;
                       this->z = z;
                       }
            
             bool equals(Point p){
                  return (x==p.x && y==p.y && z==p.z);
                  }
             int x;
             int y;
             int z;
      };
void test(int L,int R,int C){
     if(L<=30 && R<=30 && C<=30){//万恶的英语,题目limit to是<=的意思
            
            
                          int ROOM[L][R][C];
                          int NUMofS = 0;//S的个数
                          int NUMofE = 0;//E的个数
                          bool able = true;//此属性决定了该输入能否进行,包含了格式输出错误,个数错误等情况
                          int x0,y0,z0;//主人公的位置
                          int x1,y1,z1;  //出口的位置
                          string s;
                          memset(ROOM,0,sizeof(ROOM));//将所有的内容都赋予0值
                          for(int i = 0;i < L;i++){
                                  for(int j = 0;j < R;j++){
                                          cin>>s;
                                          if(s.length()>C)able = false;
                                          for(int k = 0;k < C;k++){
                                                  char ch = s.at(k);//通过输入的字符来决定该位置的内容值
                                                  switch(ch){
                                                                 case '#' :
                                                                      ROOM[i][j][k] = -1;//-1即无法通过
                                                                      break;
                                                                 case '.' ://可以通过,默认为0
                                                                      break;
                                                                 case 'S' :
                                                                      x0 = i;
                                                                      y0 = j;
                                                                      z0 = k;
                                                                      NUMofS++;
                                                                      if(NUMofS>1)
                                                                          able = false;
                                                                      break;
                                                                 case 'E' :
                                                                      x1 = i;
                                                                      y1 = j;
                                                                      z1 = k;
                                                                      NUMofE++;
                                                                      if(NUMofE>1)
                                                                          able = false;
                                                                      break;
                                                                 default :
                                                                      able = false;
                                                                 }
                                                  }
                                          }
                                  }
                                  if(able == false){//感觉题目设置的不严谨,因为输入格式错误的话,题目没有说明要怎么解决,不过经过多次的wa测试,上述情况应该输出无法通过的语句
                                          cout<<"Trapped!"<<endl;
                                          return;
                                          }
                                 
                                  Point from(x0,y0,z0);
                                  Point to(x1,y1,z1);
                                 
                                  queue<Point> p;
                                  p.push(from);
                                  while(!p.empty()){
                                           Point xp = p.front();
                                           p.pop();
                                           if(xp.equals(to)){//搜到了相同的点就输出
                                                cout<<"Escaped in "<<ROOM[to.x][to.y][to.z]<<" minute(s)."<<endl;
                                                return;
                                                }else{
                                                      for(int i = 0;i < 6;i++){
                                                              switch(i){
                                                                        case 0 ://x方向-1
                                                                             if(xp.x-1>=0 && !ROOM[xp.x-1][xp.y][xp.z]){
                                                                                          Point np(xp.x-1,xp.y,xp.z);
                                                                                          if(!np.equals(from)){
                                                                                              p.push(np);
                                                                                              ROOM[np.x][np.y][np.z] = ROOM[xp.x][xp.y][xp.z]+1;
                                                                                              }
                                                                                          }
                                                                                          break;
                                                                        case 1 ://x方向+1
                                                                             if(xp.x+1<L && !ROOM[xp.x+1][xp.y][xp.z]){
                                                                                          Point np(xp.x+1,xp.y,xp.z);
                                                                                          if(!np.equals(from)){
                                                                                              p.push(np);
                                                                                              ROOM[np.x][np.y][np.z] = ROOM[xp.x][xp.y][xp.z]+1;
                                                                                              }
                                                                                          }
                                                                                          break;
                                                                        case 2 ://y方向-1
                                                                              if(xp.y-1>=0 && !ROOM[xp.x][xp.y-1][xp.z]){
                                                                                          Point np(xp.x,xp.y-1,xp.z);
                                                                                          if(!np.equals(from)){
                                                                                              p.push(np);
                                                                                              ROOM[np.x][np.y][np.z] = ROOM[xp.x][xp.y][xp.z]+1;
                                                                                              }
                                                                                          }
                                                                                          break;
                                                                        case 3 ://y方向+1
                                                                             if(xp.y+1<R && !ROOM[xp.x][xp.y+1][xp.z]){
                                                                                          Point np(xp.x,xp.y+1,xp.z);
                                                                                          if(!np.equals(from)){
                                                                                              p.push(np);
                                                                                              ROOM[np.x][np.y][np.z] = ROOM[xp.x][xp.y][xp.z]+1;
                                                                                              }
                                                                                          }
                                                                                          break;
                                                                        case 4 ://z方向-1
                                                                             if(xp.z-1>=0 && !ROOM[xp.x][xp.y][xp.z-1]){
                                                                                          Point np(xp.x,xp.y,xp.z-1);
                                                                                          if(!np.equals(from)){
                                                                                              p.push(np);
                                                                                              ROOM[np.x][np.y][np.z] = ROOM[xp.x][xp.y][xp.z]+1;
                                                                                              }
                                                                                          }
                                                                                          break;
                                                                        case 5 ://z方向+1
                                                                             if(xp.z+1<C && !ROOM[xp.x][xp.y][xp.z+1]){
                                                                                          Point np(xp.x,xp.y,xp.z+1);
                                                                                          if(!np.equals(from)){
                                                                                              p.push(np);
                                                                                              ROOM[np.x][np.y][np.z] = ROOM[xp.x][xp.y][xp.z]+1;
                                                                                              }
                                                                                          }
                                                                                          break;
                                                                        default :
                                                                                cout<<"Trapped!"<<endl;
                                                                                return;
                                                                        }
                                                                       
                                                              }
                                                      }   
                                           }
                                      cout<<"Trapped!"<<endl; 
                          }
     }

int main(){
    int L,R,C;
    cin>>L>>R>>C;
    while(L&&R&&C){
                          test(L,R,C);
                          cin>>L>>R>>C;
                   }
    return 0;
    }

0
2
分享到:
评论

相关推荐

    WEB程序设计大作业

    在`WEB程序设计大作业`中,为了实现“界面漂亮”,我们需要使用CSS来定义元素的颜色、字体、大小、位置等视觉特性。可以创建一个独立的`style.css`文件,然后在HTML文件中通过`&lt;link&gt;`标签引入。此外,CSS还可以使用...

    web程序设计大作业+源代码

    《基于Web的实验报告系统设计与实现》 本系统是一个以学生在线提交实验报告和教师在线批阅实验报告为核心功能的Web应用。采用PHP作为主要开发语言,结合CSS和JavaScript技术,构建了一个交互性强、用户体验良好的...

    程序设计语言 实践之路 第3版

    《程序设计语言 实践之路 第3版》是一本深度探讨编程语言原理与实践的书籍,由Michael L. Scott撰写。本书旨在帮助读者理解各种编程语言的设计选择,以及这些选择如何影响程序的构造、效率和可维护性。通过阅读这...

    2013年春季学期《C++程序设计》作业题目及答案.docx

    。2013年春季学期《C++程序设计》作业题目及答案.docx

    B17020122-软件设计_南邮软件设计周作业_

    A档一、 分数统计(10)二、 打字程序(10)三、 文本编辑器(10)四、 加密(10)五、 进制转换器(10)B档一、 学生成绩核算系统的设计与实现(25)二、 模拟电信计费系统的设计与实现(25)

    《C++程序设计教程》(修订版)—设计思想与实现 钱能著 课后习题答案详解

    《C++程序设计教程》(修订版)是钱能教授的经典著作,专注于讲解C++语言的设计思想和实际应用。这本书不仅介绍了C++的基础语法,更深入地探讨了面向对象编程的概念,包括类、对象、继承、多态等核心概念。课后习题...

    Java程序设计习题集下载

    习题集内容覆盖面广,包括:Java言的基本常识、基本语法、面向对象的基本概念、数组、字符串、异常处理、文件和数据流、图形用户界面设计、小应用程序、线程、编程规范、网络程序设计、多媒体民图形学程序设计以及...

    完整版C语言程序设计TXT电子书

    C语言程序设计与实例TXT电子书 1 C语言概述 1.1 C语言的发展过程 1.2 当代最优秀的程序设计语言 1.3 C语言版本 1.4 C语言的特点 1.5 面向对象的程序设计语言 1.6 C和C++ 1.7 简单的C程序介绍 ...

    网络程序设计综合实验(聊天程序编写)

    1、分析典型网络聊天应用软件(如QQ、MSN等)的实现原理,模拟设计一套网络聊天应用程序,必须实现以下功能: ①按照C/S结构分别设计服务端程序和客户端程序; ②服务端通过图形用户界面实现对服务器的控制,负责...

    IBM PC汇编语言程序设计

    本书为清华大学计算机汇编语言程序设计课教材,主要阐述IBM PC及其兼容机汇编语言程序程序设计的方法和技术。全书共13章:第一、二章介绍基础知识;第三、四章说明IBM PC机的指令系统及包括伪操作在内的汇编语言程序...

    谭浩强C语言程序设计第五版详细答案

    《谭浩强C语言程序设计第五版》是学习C语言的经典教材,旨在帮助初学者掌握C语言编程的基础知识。本书不仅适用于大一学生,也适合自学者。在本章中,我们将深入探讨C语言程序设计的基本概念和核心知识点。 1.1 程序...

    MFC.Windows程序设计(第2版)修订版 高清pdf带源码

    MFC.Windows程序设计(第2版)修订版 高清pdf带源码 《MFC Windows程序设计》(第2版)(修订版)不仅扩展了已被认为是权威的关于Microsoft用于Windows API的功能强大的C++类库的阐述,还新增了有关COM、OLE和ActiveX的...

    张玉生《C语言程序设计》双色版 C语言程序设计理论教材习题参考答案.pdf

    张玉生编写的《C语言程序设计》双色版是一本针对初学者的C语言理论教材,它包括了C语言的基础知识、语法结构、数据类型、控制结构、函数、指针、数组、字符串等核心技术内容。该教材不仅适合自学,同时也适合作为...

    Windows程序设计_第5版珍藏版.pdf

    ### Windows程序设计_第5版珍藏版.pdf #### 知识点概览: 1. **书籍概述**:《Windows程序设计(第5版 珍藏版)》是一部经典的Windows编程指南,自出版以来已经帮助近50万名Windows程序员入门并成长为技术专家。 2...

    嵌入式系统的微模块化程序设计-实用状态图C/C++实现

    有关状态机设计方面的书籍,我这里隆重推荐一本:《Practical Statecharts in C/C++ Quantum Programming for Embedded Systems》,中文名叫做《嵌入式系统的微模块化程序设计-实用状态图C/C++实现》,北航出版的,...

    POSIX多线程程序设计.pdf

    《POSIX多线程程序设计》深入描述了IEEE的开放系统接口标准——POSIX线程,通常称为Pthreads标准。本书首先解释了线程的基本概念,包括异步编程、线程的生命周期和同步机制;然后讨论了一些高级话题,包括属性对象、...

    AutoCAD完全应用指南—AutoLISP DCL Visual LISP程序设计篇(随书光盘)

    第1篇(第1~10章)为autollsp程序设计基础篇,主要介绍了autollsp的基本结构、语法、功能函数、对象属性、循环、判断式、子程序、选择集、符号表、读文件以及写文件等autolisp程序设计的相关知识与技巧。第2篇(第11章...

    基于微信小程序点餐系统的设计与实现(含word论文)

    《基于微信小程序点餐系统的设计与实现》 随着信息技术的快速发展,互联网已深入人们生活的各个领域,其中,餐饮行业的数字化转型尤为明显。微信小程序作为移动应用的一种轻量化形式,为餐饮业提供了便捷的线上点餐...

    windows程序设计的源码(完整的)

    通过学习这部分源码,开发者可以掌握如何动态加载和修改应用程序资源,以实现更灵活的应用程序设计。 每个章节的".ace"文件,很可能是用特定的压缩格式存储的源代码文件。解压后,开发者可以逐行阅读代码,跟随作者...

Global site tag (gtag.js) - Google Analytics