Asteroids!-裸的BFS

G - Asteroids!

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Submit Status

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 
/*
Author: 2486
Memory: 1452 KB		Time: 0 MS
Language: G++		Result: Accepted
*/
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=+5;
char maps[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
char op[10];
int n;
int a[10];
int dx[]={0,0,1,0,-1,0};
int dy[]={0,0,0,1,0,-1};
int dz[]={1,-1,0,0,0,0};
struct obje{
    int x,y,z,steps;
    obje(int x,int y,int z,int steps):x(x),y(y),z(z),steps(steps){}
    bool operator<(const obje &a)const{
        return steps>a.steps;
    }
};
void bfs(){
    priority_queue<obje>G;
    G.push(obje(a[0],a[1],a[2],0));
    while(!G.empty()){
        obje e=G.top();
        G.pop();
        if(e.x<0||e.y<0||e.z<0||e.x>=n||e.y>=n||e.z>=n||maps[e.x][e.y][e.z]=='X'||vis[e.x][e.y][e.z])continue;
        vis[e.x][e.y][e.z]=true;
        if(e.x==a[3]&&e.y==a[4]&&e.z==a[5]){
            printf("%d %d\n",n,e.steps);
            return;
        }
        for(int i=0;i<6;i++){
            int nx=e.x+dx[i];
            int ny=e.y+dy[i];
            int nz=e.z+dz[i];
            G.push(obje(nx,ny,nz,e.steps+1));
        }
    }
    printf("NO ROUTE\n");
}
int main(){
    #ifndef ONLINE_JUDGE//本博客有介绍其用处
    freopen("D://imput.txt","r",stdin);
    #endif // ONLINE_JUDGE
while(~scanf("%s%d",op,&n)){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
                scanf("%s",maps[i][j]);
        }
    }
    for(int i=5;i>=0;i--){
        scanf("%d",&a[i]);
    }
    scanf("%s",op);
    bfs();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 15:12:39

Asteroids!-裸的BFS的相关文章

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

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

HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids

普通的三维广搜,需要注意的是输入:列,行,层 #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #define M 11 using namespace std; int dir[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};//6个方向 int

HDU 1253-胜利大逃亡--裸三维BFS

G - 胜利大逃亡 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1253 Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-

HDU 1240 Asteroids! (三维BFS)

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

HDU 2225 Asteroids!(三维BFS)

Asteroids! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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) seri

杭电ACM1240——Asteroids!~~简单的BFS

这道题目,三维空间上的BFS,给你起点和终点,看能否找到一条路,O表示可以走,X表示不可以走!~ 理解了题目,就可以用队列来实现BFS来求解. 下面的是AC 的代码: #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; class data { public: int xyz; int count; }; char map

csu 1604 SunnyPig (bfs)

Description SunnyPig is a pig who is much cleverer than any other pigs in the pigpen. One sunny morning, SunnyPig wants to go out of the pigpen to date Mrs. Snail, his beloved. However, it’s terribly tough for a pig to go out of the pigpen because th

【bfs+优先队列】POJ2312-Battle City

[思路] 题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为2个单位”,这样题目就迎刃而解了.第一次碰到时可能不能很好把握,第二次基本就可以当作水题了. [错误点] 1.不能用裸的bfs.广搜的实际思想是将到达时间最短的放在队首,这样首次到达终点即为时间的最小值.通过砖墙的时间为两个单位,通过砖墙后可能不是时间最小值.用优先队列可以解决这一问题. 2.c++中memset初始化为memset(vis,0,sizeof(vis)),很多人可能会写成mems

【算法】BFS+哈希解决八数码问题

15拼图已经有超过100年; 即使你不叫这个名字知道的话,你已经看到了.它被构造成具有15滑动砖,每一个从1到15上,并且所有包装成4乘4帧与一个瓦块丢失.让我们把丢失的瓷砖"X"; 拼图的目的是安排瓷砖以便它们排序为: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15× 这里唯一合法经营是交流'X'与它共享一个边缘的瓷砖之一.作为一个例子,举动下列顺序解决了一个稍微加扰难题: 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5 6 7 8 5 6