HDU 1240(Asteroids!) || POJ 2225(Asteroids!)

HDU 1240(Asteroids!) || POJ 2225(Asteroids!)Asteroids!Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2135 Accepted Submission(s): 1444 Prob

HDU

Asteroids!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2135    Accepted Submission(s): 1444

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
 
/*简单BFS
Author: shaorui
Date: 2012/10/5
链接地址: http://acm.hdu.edu.cn/showproblem.php?pid=1240
         http://poj.org/problem?id=2225
只不过是三维的,需注意的是输入的坐标问题
*/
#include<iostream>
#include<string>
#include<queue>
using namespace std;
#define maxn 10
bool visited[maxn][maxn][maxn];
int dir[][3] = {{0,-1,0},{1,0,0},{0,1,0},{-1,0,0},{0,0,1},{0,0,-1}};
int n,si,sj,sk,ei,ej,ek;
char maze[maxn][maxn][maxn];
struct node
{
    int x,y,z,t;
};
bool ok(int i,int j,int k)
{
    return i >= 0 && i < n && j >= 0 && j < n && k >= 0 && k < n && !visited[i][j][k] && maze[i][j][k] != 'X'; 
}
void BFS()
{
    node p,q;
    queue<node> que;
    p.x = si,p.y = sj,p.z = sk,p.t = 0;
    memset(visited,false,sizeof(visited));
    visited[si][sj][sk] = true;
    que.push(p);
    while(!que.empty())
    {
        p = que.front();
        que.pop();
        if(p.x == ei && p.y == ej && p.z == ek)
        {
            printf("%d %d\n",n,p.t);
            return;
        }
        for(int i = 0; i < 6; i++)
        {
            q.x = p.x+dir[i][0],q.y = p.y+dir[i][1],q.z = p.z+dir[i][2],q.t = p.t+1;
            if(ok(q.x,q.y,q.z))
                que.push(q),visited[q.x][q.y][q.z] = true;
        }
    }
    printf("NO ROUTE\n");
}
int main()
{
    //freopen("1002.txt","r",stdin);
    string s,ss;
    while(cin >> s >> n)
    {
        int i,j,k;
        for(k = 0; k < n; k++)            //HDU
             for(i = 0; i < n; i++)
                for(j = 0; j < n; j++)
                    cin >> maze[i][j][k];
        /*for(k = 0; k < n; k++)          //POJ
             for(j = 0; j < n; j++)
                for(i = 0; i < n; i++)
                    cin >> maze[i][j][k];*/
        cin >> si >> sj >> sk;
        cin >> ei >> ej >> ek;
        cin >> ss;
        BFS();
    }
    return 0;
}

 

今天的文章HDU 1240(Asteroids!) || POJ 2225(Asteroids!)分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/55763.html

(0)
编程小号编程小号
上一篇 2023-08-27 14:30
下一篇 2023-08-27 15:06

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注