hdu1240--asteriod(搜索)

来源:互联网 发布:淘宝商品怎么推广 编辑:程序博客网 时间:2024/06/08 03:26

Asteroids!

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


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 1O0 0 00 0 0ENDSTART 3XXXXXXXXXOOOOOOOOOXXXXXXXXX0 0 12 2 1ENDSTART 5OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXXXXXXXXXXXXXXXXXXXXXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0 0 04 4 4END
 

Sample Output
1 03 4NO ROUTE

题目大意是在一个N*N*N的三维空间内由一点是否能到另一点,如果能到达求出最短路径。
该题是典型的搜索题,用广搜可以直接找出最短路径,深搜则需要运用剪枝技巧,采用记忆化搜索的方法,否则会超时,
记忆化的大致思路是记下到达某一点的最小步数,如果到达此处步数大于了最小步数,则停止搜索,因为接下去的搜索情况一样,但是此时一定不是最短路径了,故不需要再搜索下去了。
广搜代码:
#include <iostream>#include <cstdio>#include <string.h>#include <math.h>using namespace std;const int MAX=20;struct point{int x,y,z;} dot[1050];char asteriod[MAX][MAX][MAX];int visit[MAX][MAX][MAX];int ans[MAX][MAX][MAX],N;int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};int bfs(int x1,int y1,int z1,int x2,int y2,int z2){int head,tail,xx,yy,zz;memset(visit,0,sizeof(visit));memset(ans,0,sizeof(ans));head=0,tail=1;visit[z1][x1][y1]=1;dot[0].x=x1;dot[0].y=y1;dot[0].z=z1;while(head<tail){x1=dot[head].x;y1=dot[head].y;z1=dot[head].z;if(x1==x2&&y1==y2&&z1==z2) return 1;for(int i=0;i<6;i++){xx=x1+dir[i][0];yy=y1+dir[i][1];zz=z1+dir[i][2];if(xx>=0&&xx<N&&yy>=0&&yy<N&&zz>=0&&zz<N&&(asteriod[zz][xx][yy]=='O')&&!(visit[zz][xx][yy])){dot[tail].x=xx;dot[tail].y=yy;dot[tail].z=zz;visit[zz][xx][yy]=1;ans[xx][yy][zz]=ans[x1][y1][z1]+1;tail++;}}head++;}return 0;}int main(){    char s[10];    while(~scanf("%s %d",s,&N))    {        int x1,x2,y1,y2,z1,z2;        for(int i=0; i<N; i++)            for(int j=0; j<N; j++)                scanf("%s",asteriod[i][j]);        scanf("%d%d%d",&x1,&y1,&z1);        scanf("%d%d%d",&x2,&y2,&z2);        if(!bfs(x1,y1,z1,x2,y2,z2)) printf("NO ROUTE\n");        else printf("%d %d\n",N,ans[x2][y2][z2]);        scanf("%s",s);    }    return 0;}深搜代码:#include <iostream>#include <cstdio>#include <cstdlib>#include <climits>#include <string.h>#include <math.h>#define MAXN 99999999//用深搜需剪枝,采用记忆化搜索,即记录最短距离。using namespace std;const int MAX=20;const int INF=0x3f3f3f3f;char asteriod[MAX][MAX][MAX];long long visit[MAX][MAX][MAX];int ans,N;int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};void dfs(int x1,int y1,int z1,int x2,int y2,int z2,int step){int xx,yy,zz;//if(x1<0||x1>=N||y1<0||y1>=N||z1<0||z1>=N) return;if(asteriod[z1][x1][y1]=='X') return ;if(x1==x2&&y1==y2&&z1==z2){if(step<ans) ans=step;return ;}if(visit[z1][x1][y1]>step)  visit[z1][x1][y1]=step;//此处即为记忆化,注意此处visit数组必须先赋一个无穷大,而用memset辅助时,一般采用0x3f3f3f3f作为无穷大,不能赋为int_max,那样会溢出,不符合memset的用法。else return ;for(int i=0;i<6;i++){xx=x1+dir[i][0];yy=y1+dir[i][1];zz=z1+dir[i][2];if(xx>=0&&xx<N&&yy>=0&&yy<N&&zz>=0&&zz<N){dfs(xx,yy,zz,x2,y2,z2,step+1);        }}return ;}int main(){    char s[10];    while(~scanf("%s %d",s,&N))    {        int x1,x2,y1,y2,z1,z2;        for(int i=0; i<N; i++)            for(int j=0; j<N; j++)                scanf("%s",asteriod[i][j]);//cout<<INF<<endl;memset(visit,INF,sizeof(visit));visit[0][0][1]=INF;//此处要注意,INF=0x3f3f3f3f,过大会变成负数,memset的具体用法可以百度。//cout<<visit[0][0][1]<<endl;        scanf("%d%d%d",&x1,&y1,&z1);        scanf("%d%d%d",&x2,&y2,&z2);        ans=MAXN;dfs(x1,y1,z1,x2,y2,z2,0);if(ans==MAXN) printf("NO ROUTE\n");        else printf("%d %d\n",N,ans);        scanf("%s",s);    }    return 0;}


下面另附一种模拟法,如果看懂题意的话,我们会发现在这个三维空间内,小行星带时一层一层的分布的,如果在起始点和终点之间有小行星层,则没有路径,否则最短路径既是空间内两点坐标之差绝对值的和。
代码如下:
#include <iostream>#include <cstdio>#include <string.h>#include <math.h>#define LIM 99999999using namespace std;const int MAX=20;char asteriod[MAX][MAX][MAX];int Xplace[MAX];int main(){    char s[10];    int N;    while(~scanf("%s %d",s,&N))    {        int flag=0,cnt=0,minn=0,x1,x2,y1,y2,z1,z2;        for(int i=0; i<N; i++)        {            for(int j=0; j<N; j++)                scanf("%s",asteriod[i][j]);            if(asteriod[i][0][0]=='X') Xplace[cnt++]=i;        }        scanf("%d%d%d",&x1,&y1,&z1);        scanf("%d%d%d",&x2,&y2,&z2);        for(int i=0; i<cnt; i++)        {            if((Xplace[i]>z1&&Xplace[i]<z2)||(Xplace[i]<z1&&Xplace[i]>z2))            {                flag=1;                break;            }        }        minn=fabs(x1-x2)+fabs(y1-y2)+fabs(z1-z2);        if(flag) printf("NO ROUTE\n");        else printf("%d %d\n",N,minn);        scanf("%s",s);    }    return 0;}


0 0
原创粉丝点击