小夥的博客

恩,慢慢加油,慢慢积累

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

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;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21899803/article/details/51064943
文章标签: acm bfs 搜索
个人分类: 搜索
想对作者说点什么? 我来说一句

动态规划DP结题报告

2010年08月10日 86KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭