首页 > ACM题库 > HDU-杭电 > HDU 1240 Asteroids!-BFS-[解题报告] C++
2013
12-04

HDU 1240 Asteroids!-BFS-[解题报告] C++

Asteroids!

问题描述 :

You’re in space.
You want to get home.
There are asteroids.
You don’t want to hit them.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

1 0
3 4
NO ROUTE

到比较拿手的搜索题,嗯,毕竟做的最多的就是搜索了.

BFS,三维而已

#include <iostream>    
#include <queue>
using namespace std;
char map[11][11][11];
int dir[6][3]={-1,0,0,0,1,0,1,0,0,0,-1,0,0,0,1,0,0,-1};
struct point
{
    int x;
    int y;
    int z;
    int step;
}sta,end1;
int n;
bool islegal(int x,int y,int z)
{
    return x>=0&&x<n&&y>=0&&y<n&&z>=0&&z<n&&map[z][x][y]!='X';
}
void BFS()
{
    queue<point> Q;
    sta.step = 0;
    Q.push(sta);
    while(!Q.empty())
    {
        point now = Q.front();
        Q.pop();
        if(now.x==end1.x&&now.y==end1.y&&now.z==end1.z)
        {
            cout<<n<<" "<<now.step<<endl;
            return;
        }
        for(int i=0;i<6;i++)
        {
            int nextX = now.x + dir[i][0];
            int nextY = now.y + dir[i][1];
            int nextZ = now.z + dir[i][2];
            if(islegal(nextX,nextY,nextZ))
            {
                point next = {nextX,nextY,nextZ,now.step+1};
                map[nextZ][nextX][nextY]='X';
                Q.push(next);
            }
        }
    }
    cout<<"NO ROUTE"<<endl;
}

int main(int argc, const char *argv[])
{
    //freopen("input.txt","r",stdin);
    char a[10];

    while(cin>>a>>n)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                cin>>map[i][j];
            }
            cin>>sta.x>>sta.y>>sta.z;
            cin>>end1.x>>end1.y>>end1.z;
            cin>>a;
            BFS();
    }
    return 0;
}

 


,
  1. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧

  2. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }

  3. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。