胜利大逃亡

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21759    Accepted Submission(s): 8538

Problem Description
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.
魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.
 
Input
输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample
Input中的迷宫描述,它表示的就是上图中的迷宫)
特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.
Output
 
对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1. 
Sample Input
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
 
Sample Output
 
11

/*-----------------------------------------------------------------------

转自http://www.cnblogs.com/yuyixingkong/p/3245297.html

代码是我的..

本题是搜索题,从囚徒出发,搜索到出口的最短路线的距离值(或者反向,由出口开始,搜索到囚徒的位置的距离值),容易想到的是宽度优先搜索,首先将囚徒的点放入先进先出的一个队列,并记录走过的时间(或距离),以后不断地

(1)将队列中先进的元素推出,还原为实际储存点后,

(2)六向搜索通路点,并将搜索到的通路点放进先进先出队列。

(3)重复(1),(2),直到搜索到目标点(出口)或队列为空为止,前者只要搜索得的值低于题目给出的T值,就输出此值,其余情况输出-1;

-------------------------------------------------------------------------

本题目有几个大坑:

1,门有可能是墙壁;(墙壁不可出,但是一开始人站的地方是墙居然又可以)

2,题目给出的某些CASE的T值小于囚徒到门口的空间最短距离(A+B+C-3)。不要忘记-1的情况

3,开始可以是墙,不影响,

掉进去会使用时增多,如果你的搜索效率再不高的,就很可能超时了。

----------------------------------------------------------------------------------*/

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253

这道题没什么特别难的地方,只是有几个bug要注意一下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#include<queue>
#define INF 0x7ffffff
#define MAXN 51
using namespace std;
const double eps=1e-10;
int m[MAXN][MAXN][MAXN];
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct node
{
    int a;
    int b;
    int c;
    int n;
};
int vis[MAXN][MAXN][MAXN];
int x,y,z;
int a,b,c;
int n;
int nn;
bool ok()
{
    if(x>=0&&x<a&&y>=0&&y<b&&z>=0&&z<c)
        return 1;
    else return 0;
}
int BFS()
{
    queue<struct node> route;
    struct node tem;
    struct node buf;
    buf.a=0;
    buf.b=0;
    buf.c=0;
    buf.n=0;
    vis[0][0][0]=1;
    route.push(buf);
    while(!route.empty()) {
        //cout<<11111<<endl;
        buf=route.front();
        route.pop();
        if(buf.a==a-1&&buf.b==b-1&&buf.c==c-1) {
            return buf.n;
        }
        if(buf.n>n){
            return -1;
        }
        //nn=buf.n+1;
        for(int i=0; i<6; i++){
            x=buf.a+dir[i][0];
            y=buf.b+dir[i][1];
            z=buf.c+dir[i][2];
            if(ok()&&m[x][y][z]==0&&vis[x][y][z]==0) {
                if(abs(x-a+1)+abs(y-a+1)+abs(z-c+1)+buf.n>n)//很棒的剪枝思路,见大坑2
                    continue;
                vis[x][y][z]=1;
                tem.a=x;
                tem.b=y;
                tem.c=z;
                tem.n=buf.n+1;
                route.push(tem);
            }
        }
    }
    return -1;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data2.in", "r", stdin);
    #endif
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int t;
    int res=0;
    cin>>t;
    while(t--){
        a=b=c=x=y=z=0;
        nn=0;
        memset(vis,0,sizeof(vis));
        cin>>a>>b>>c>>n;
        for(int i=0;i<a;i++){
            for(int j=0;j<b;j++){
                for(int k=0;k<c;k++){
                    cin>>m[i][j][k];
                }
            }
        }
        cout<<BFS()<<endl;
    }
} /*
5
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
3 3 4 20
1 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 1
3 3 4 10
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
3 3 4 21
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 1 0
0 1 1 0
0 1 1 0
*/ /*
11
11
-1
-1
-1
*/

还有一道一模一样的题目,在此也一并贴出

Asteroids!

 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 114 Accepted Submission(s): 91
Problem Description
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.
 

Input
Input to this problem will consist of a (non-empty) series of up
to 100 data sets. Each data set will be formatted according to the
following description, and there will be no blank lines separating data
sets.

A single data set has 5 components:

Start line - A single line, "START N", where 1 <= N <= 10.

Slice
list - A series of N slices. Each slice is an N x N matrix representing
a horizontal slice through the asteroid field. Each position in the
matrix will be one of two values:

'O' - (the letter "oh") Empty space

'X' - (upper-case) Asteroid present

Starting
Position - A single line, "A B C", denoting the <A,B,C>
coordinates of your craft's starting position. The coordinate values
will be integers separated by individual spaces.

Target Position -
A single line, "D E F", denoting the <D,E,F> coordinates of your
target's position. The coordinate values will be integers separated by
individual spaces.

End line - A single line, "END"

The
origin of the coordinate system is <0,0,0>. Therefore, each
component of each coordinate vector will be an integer between 0 and
N-1, inclusive.

The first coordinate in a set indicates the column. Left column = 0.

The second coordinate in a set indicates the row. Top row = 0.

The third coordinate in a set indicates the slice. First slice = 0.

Both the Starting Position and the Target Position will be in empty space.

 

Output
For each data set, there will be exactly one output set, and there will be no blank lines separating output sets.

A
single output set consists of a single line. If a route exists, the
line will be in the format "X Y", where X is the same as N from the
corresponding input data set and Y is the least number of moves
necessary to get your ship from the starting position to the target
position. If there is no route from the starting position to the target
position, the line will be "NO ROUTE" instead.

A move can only be
in one of the six basic directions: up, down, left, right, forward,
back. Phrased more precisely, a move will either increment or decrement a
single component of your current position vector by 1.

 

Sample Input
START 1
O
0 0 0
0 0 0
END
START 3
XXX
XXX
XXX
OOO
OOO
OOO
XXX
XXX
XXX
0 0 1
2 2 1
END
START 5
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
0 0 0
4 4 4
END
 

Sample Output
1 0
3 4
NO ROUTE
 

 
Source
South Central USA 2001
 

Recommend
zf

下面是AC代码

#include <string.h>
#include <stdio.h>
#include <queue>
using namespace std; char map[][][];
int vis[][][];
int n;
int sx,sy,sz;
int ex,ey,ez;
int tx[] = {,-,,,,};
int ty[] = {,,,-,,};
int tz[] = {,,,,,-}; struct node
{
int x,y,z,step;
}; int check(int x,int y,int z)
{
if(x< || y< || z< || x>=n || y>=n || z>=n || vis[x][y][z] )
return ;
return ;
} int bfs(int x,int y,int z)
{
int i;
queue<node> Q;
node a,next;
a.x = x;
a.y = y;
a.z = z;
a.step = ;
vis[x][y][z] = ;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop();
if(a.x == ex && a.y == ey && a.z == ez)
return a.step;
for(i = ;i<;i++)
{
next = a;
next.x+=tx[i];
next.y+=ty[i];
next.z+=tz[i];
if(check(next.x,next.y,next.z))
{
next.step++;
vis[next.x][next.y][next.z] = ;
Q.push(next);
}
}
}
return -;
} int main()
{
char s[];
int i,j,k;
while(~scanf("%s%d",s,&n))
{
for(i = ;i<n;i++)
for(j = ;j<n;j++)
scanf("%s",map[i][j]);
scanf("%d%d%d%d%d%d",&sx,&sy,&sz,&ex,&ey,&ez);
scanf("%s",s);
int ans = bfs(sx,sy,sz);
if(ans>=)
printf("%d %d\n",n,ans);
else
printf("NO ROUTE\n");
} return ;
}

HDU1253--胜利大逃亡--HDU1240--Asteroids!--简单三维BFS的更多相关文章

  1. HDU1253 胜利大逃亡 BFS

    胜利大逃亡 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  2. HDU1253 胜利大逃亡(BFS) 2016-07-24 13:41 67人阅读 评论(0) 收藏

    胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示 ...

  3. HDU1253 胜利大逃亡 (BFS)

      胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  4. HDU-1253 胜利大逃亡 (BFS)

    此题可以做为三维深搜模板题.. 胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  5. HDU1253:胜利大逃亡

    传送门 题意 逃离迷宫 分析 用优先队列和队列都可以,但是我vis数组写在取队列首节点就MLE了,放在放入节点的地方就ac了,看来是一种固定写法,在放入节点的地方判断,可以防止放入无效点到队列,防止队 ...

  6. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  7. 胜利大逃亡(续)(状态压缩bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. hdu1253胜利大逃亡(城堡的怪物太狠,主角难免天天逃亡)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1253/ 其实就是二维扩展到三维了,增加了搜索方向,其他的没什么不同. 代码如下: #include<bit ...

  9. Hdu 1429 胜利大逃亡(续) (bfs+状态压缩)

    这道题的钥匙只有10个,可以压成二进制 这里有有句非常关键的话 (k & door[x][y]) == door[x][y] 一开始以为只要(k & door[x][y]) ==1就可 ...

  10. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. FZU 1893 内存管理 模拟

    比赛的时候队友要做这道题…… 他没做出来自己也被误导了…… 也算是个教训 自己还是要有自己的思路…… 又是模拟题…… 网上都是用vector做的 我最近才会stl 怎么会用那么高大上的的东西…… 强力 ...

  2. ural 1355. Bald Spot Revisited(数的素因子划分)

    1355. Bald Spot Revisited Time limit: 1.0 secondMemory limit: 64 MB A student dreamt that he walked ...

  3. 关于oracle数据库(8)查询2

    筛选数据,直接加where条件,并且and,或者or 使用rownum获取前N条数据 select * from 表名 where rownum <= 数字; 如:获取前5条数据 select ...

  4. mysql创建计算列

    mysql> create table t(id int auto_increment not null,c1 int,c2 int,c3 int as (c1+c2),primary key( ...

  5. 将MyEclipse中的项目导入到Eclipse中报错的解决放法

    1.导入项目后,会报红色感叹号, 将项目右键,点击红色框.

  6. finally语句包含return的情况

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  7. stm32按键识别

    刚写了一个关于stm32单片机的按键识别的程序.目的,同时识别多个按键,并且不浪费cpu的时间. 关于去抖动,以前以为是在按键的时候,手会抖动.通过程序验证,这个确实是误解.这个应该是防止意外干扰.以 ...

  8. PHP signal 信号

    最早写php时,发现在终端执行一个php文件,会一直等待程序执行完成以后,终端才能继续下面的操作,若不小心按了下Ctrl+C会导致php程序退出,闭避免这种情况发生,将会使用php的系统编程,即sig ...

  9. Activity LauchMode启动模式(转载)

    转载于:http://www.cnblogs.com/plokmju/p/android_ActivityLauncherMode.html 在一个Android应用中,不可避免的会包含多个Activ ...

  10. MFC通过ODBC连接Mysql程序

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 MFC通过ODBC连接 ...