poj 2251(广搜求两点之间的距离)

题目链接:http://poj.org/problem?id=2251

Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16682   Accepted: 6491

Description

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?

Input

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.

Output

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!

Sample Input

3 4 5
S....
.###.
.##..
###.#

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

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

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

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Source

Ulm Local 1997

题解:(1)这道题比较简单,但是比较繁琐,要注意的细节比较多,=。=

(2)广搜用的队列,求两点之间的距离一定是最短的~

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<string>
#include<stack>
#include<cmath>
#include<cctype>
#include<iostream>
#include<set>
#include<algorithm>
#include<ctime>
#include<vector>
using namespace std;
#define judge(x,y,z) !vis[x][y][z]&&map[x][y][z]!='#'&&x>=1&&x<=l&&y>=1&&y<=m&&z>=1&&z<=n

//设置方向数组优化广搜
int xx[]={0,0,0,0,1,-1};
int yy[]={-1,1,0,0,0,0};
int zz[]={0,0,-1,1,0,0};

bool vis[35][35][35];
char map[35][35][35];//地图
int l,m,n;

//设置结构体
struct node
{
    int x,y,z;
    int step;
};

int  BFS(int x,int y,int z)
{
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node u;
    u.x=x;
    u.y=y;
    u.z=z;
    u.step=0;

    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        if(map[u.x][u.y][u.z]=='E')return u.step;
        q.pop();
        for(int i=0;i<6;i++)
        {
            node v;
            v.x=u.x+xx[i];
            v.y=u.y+yy[i];
            v.z=u.z+zz[i];

            if(judge(v.x,v.y,v.z))
            {
                vis[v.x][v.y][v.z]=true;
                v.step=u.step+1;
                q.push(v);
            }
        }
    }
    return 0;
}
int main()
{
    while(cin>>l>>m>>n)
    {
        memset(map,0,sizeof(map));
        if(!m&&!n&&!l)break;
        int sx,sy,sz;
        for(int i=1;i<=l;i++)
          for(int j=1;j<=m;j++)
           for(int k=1;k<=n;k++)
           {
             cin>>map[i][j][k]; //这里注意一下:表示第i层第j行第k列
             if(map[i][j][k]=='S')
             {
                sx=i;
                sy=j;
                sz=k;
             }
           }

            int step=BFS(sx,sy,sz);

        if(step)printf("Escaped in %d minute(s).\n",step);
        else printf("Trapped!\n");
    }
    return 0;
}
时间: 2024-09-30 11:46:11

poj 2251(广搜求两点之间的距离)的相关文章

利用结构类型的相关知识计算两点之间的距离

#include<stdio.h>#include<stdlib.h>#include<math.h> struct point{ /*点的结构类型名*/ float x; /*横坐标*/ float y; /*纵坐标*/ }; struct point readPoint(); /*函数原型声明*/float distance(struct point p1,struct point p2);/*主函数*/ int main(void){ struct point a

2D和3D空间中计算两点之间的距离

自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary> /// 3维中如何计算两点之间的距离 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// &l

openlayer3计算两点之间的距离

openlayer3计算两点之间的距离 对应的openlayers的版本为3.7. 主要用的接口是ol.Sphere.haversineDistance([x1,y1],[x2,y2]): 4326坐标系中计算两点距离的方式为: var wgs84Sphere = new ol.Sphere(6378137); wgs84Sphere.haversineDistance(C1,C2); 示例为: var wgs84Sphere = new ol.Sphere(6378137); wgs84Sph

两点之间的距离

import javax.swing.JOptionPane; public class Distance { public static void main(String[] args){ String input = JOptionPane.showInputDialog(null,"请输入x1的坐标:","对话框",JOptionPane.QUESTION_MESSAGE); String input1 = JOptionPane.showInputDialo

使用友元函数计算两点之间的距离

#include <iostream> #include <cmath> using namespace std; class CPoint//点类 { private: double x;//横坐标 double y;//纵坐标 public: //使用初始化表初始化数据成员 CPoint(double xx=0,double yy=0):x(xx),y(yy){} //定义友元函数用于计算两点之间的距离 friend double Distance(CPoint &p1

sql server2008根据经纬度计算两点之间的距离

--通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.490295,106.486654,29.615467, 106.581515 (@LatBegin1 varchar(128), @LngBegin1 varchar(128),@location varchar(128)) Returns real AS BEGIN --转换location字段,防止字段

JAVA实现求一点到另两点连线的距离,计算两点之间的距离

直接上代码 /** *计算两点之间距离 */ public static double getDistance(Point start,Point end){ double lat1=start.getX().doubleValue(); double lat2=end.getX().doubleValue(); double lon1=start.getY().doubleValue(); double lon2=end.getY().doubleValue(); return Math.sq

武汉科技大学ACM :1006: 零起点学算法25——求两点之间的距离

Problem Description 输入平面坐标系中2点的坐标,输出它们之间的距离 Input 输入4个浮点数x1 y1 x2 y2,分别是点(x1,y1) (x2,y2)的坐标(多组数据) Output 输出它们之间的距离,保留2位小数(每组数据一行) Sample Input 1 0 2 0 Sample Output 1.00 1 #include<stdio.h> 2 3 #include<math.h> 4 5 6 7 int main() 8 9 { 10 11 f

mysql计算经纬度两点之间的距离(转载)

DELIMITER $$DROP FUNCTION IF EXISTS `fun_distance`$$ CREATE FUNCTION `fun_distance`(lat1 float,lng1 float,lat2 float,lng2 float) RETURNS floatBEGINset @num=2 * 6378137*ASIN(SQRT(POW(SIN(PI()*(lat1-(lat2))/360),2)+ COS(PI()*lat1/180)*COS(lat2*PI()/180