hdu 1240 poj 2225 Asteroids! 三维bfs 解题报告

原创 2016年04月05日 15:12:41

Asteroids!

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


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
 

分析:杭电的数据比较弱,刚开始看错题目了,也交对了,poj上wa了好几次,输入那有坑,scanf(a[k][j][i]) 不能是a[i][j][k]

代码:

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

struct node
{
    int c,x,y,step;
};

char start[50],map[12][12][12];
int vis[12][12][12],no,ans,sc,sx,sy,ec,ex,ey;
int fx[6][3]={1,0,0,0,1,0,0,0,1,-1,0,0,0,-1,0,0,0,-1};

int check(node p)
{
    if(p.c<0||p.x<0||p.y<0||p.c>=no||p.x>=no||p.y>=no)return 0;
    if(vis[p.c][p.x][p.y])return 0;
    if(map[p.c][p.x][p.y]=='X')return 0;
    return 1;
}

int bfs()
{
    queue<node>Q;
    node t,p;
    t.c=sc;
    t.x=sx;
    t.y=sy;
    t.step=0;
    Q.push(t);
    while(!Q.empty())
    {
        t=Q.front();
        Q.pop();
        if(t.c==ec&&t.x==ex&&t.y==ey)return t.step;
        for(int i=0;i<6;i++)
        {
            p.c=t.c+fx[i][0];
            p.x=t.x+fx[i][1];
            p.y=t.y+fx[i][2];
            p.step=t.step+1;
            if(check(p))
            {
                Q.push(p);
                vis[p.c][p.x][p.y]=1;
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%s %d",start,&no)!=EOF)
    {
        getchar();
        memset(vis,0,sizeof(vis));
        /*
        for(int i=0;i<no;i++)
            for(int j=0;j<no;j++)
                scanf("%s",map[i][j]);*/
        for(int i=0;i<no;i++)
        for(int j=0;j<no;j++){
            for(int k=0;k<no;k++)
                scanf("%c",&map[k][j][i]);
            getchar();
        }

        scanf("%d%d%d%d%d%d",&sc,&sx,&sy,&ec,&ex,&ey);
        scanf("%s",start);
        ans=bfs();
        if(ans==-1)
            printf("NO ROUTE\n");
        else printf("%d %d\n",no,ans);
    }
    return 0;
}


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

相关文章推荐

POJ2225 Asteroids! (三维的BFS求最短路)

 Asteroids! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3017   ...

HDU 2225 Asteroids!(三维BFS)

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

Hdu1240 Asteroids!(三维BFS)

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

ACM-三维BFS之Asteroids!——hdu1240

ACM 三维BFS Asteroids! hdu1240

hdu 1240 Asteroids! (三维bfs)

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

hdu1240 Asteroids!(三维bfs水)

天啦一道超水的题居然被我看错输入顺序WA到现在,差点连死的心都有了。。。 总之A了就好,A了就好,A了就好。。。。 #include #include #include #include #i...

HDU1240 Asteroids!-题目意思详解( 三维BFS)

HDU1240详解

HDU解题报告——1240

三维走迷宫,起点和终点可以在

POJ-3041-Asteroids 解题报告

二分图
  • JZQT_T
  • JZQT_T
  • 2014年11月11日 22:50
  • 518

解题报告 之 POJ3041 Asteroids

解题报告 之 POJ3041 Asteroids 最大流 最小点覆盖 二分图匹配 Bessie wants to navigate her spaceship through a dangerous...
  • maxichu
  • maxichu
  • 2015年04月28日 10:16
  • 365
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1240 poj 2225 Asteroids! 三维bfs 解题报告
举报原因:
原因补充:

(最多只允许输入30个字)