Raiders of the Lost Ark

FJNU.1417

Description

In this famous film, renowned archeologist and expert in the occult, Dr. Indiana Jones, is hired by the U.S. Government to find the Ark of the Covenant (圣约柜), which is believed to still hold the ten commandments (十诫). Unfortunately, agents of Hitler are also after the Ark. Dr. Jones escapes from various close scrapes in a quest that takes him from Nepal to Cairo. Your task is to help him to find the lost Ark of the Covenant in the maze (迷宫) earlier than the enemy.

There are several barriers in the maze, Dr. Jones can bypass these barriers on foot or by jump .He can go on foot in four directions of east, south, west and north to reach a neighboring position. He can also jump cross a cell in the directions of southeast, southwest, northeast and northwest to reach a new position. The following figure shows Dr. Jones’s action where O denotes the original position and × denotes the new possible positions after one action.

Input
The first line of the input is an integer n (0<=n<=20), denoting the number of test cases. The following lines are the data of n test cases. The first line of each test case consists of two integer x and y (0<x<10, 0<y<10). The next x lines describe the maze matrix, which contains x rows and y columns. In the matrix, 'W' denotes the barrier, 'B' denotes the enterable position, ’S’ denotes the starting position, and ‘X’ denotes the position of the ark. There have and only have one ‘S’ and one ‘X’ in a maze matrix.

Output
For each test case, output one line. If there is a solution for the problem, output the number of the least steps to find the Ark. Otherwise output “NO ANSWER”.

Sample Input
3 3
SWB
BWB
XBB
5 4
BXWB
BBWS
BBWB
BBWB
BBBB
3 2
WX
WW
WS

Sample Output
2
2
NO ANSWER

Source

福建师范大学第三届程序设计比赛

My Program

#include < iostream >
#define  N 20
using   namespace  std;

int  main()
{
    
int map[N][N];
    
int queuex[N],queuey[N],front,rear;
    
int r,c,sx,sy,ex,ey;
    
int i,j,x,y,k;
    
char s;
    cin
>>k;
    
while(k--)
    
{
        cin
>>r>>c;
        
for(i=0;i<r;i++)
            
for(j=0;j<c;j++)
            
{
                cin
>>s;
                
if(s=='W')
                    map[i][j]
=-1;
                
else
                    map[i][j]
=0;
                
if(s=='S'){sx=i;sy=j;}
                
if(s=='X'){ex=i;ey=j;}
            }

        front
=0;rear=1;
        queuex[front]
=sx;queuey[front]=sy;

        
while(front<rear)
        
{
            x
=queuex[front];
            y
=queuey[front];
            
if(x==ex&&y==ey)
                
break;
            
if(x-1>=0&&y>=0&&map[x-1][y]==0)
            
{
                map[x
-1][y]=map[x][y]+1;
                queuex[rear]
=x-1;
                queuey[rear]
=y;
                rear
++;
            }

            
if(x+1<r&&y>=0&&map[x+1][y]==0)
            
{
                map[x
+1][y]=map[x][y]+1;
                queuex[rear]
=x+1;
                queuey[rear]
=y;
                rear
++;
            }

            
if(x>=0&&y-1>=0&&map[x][y-1]==0)
            
{
                map[x][y
-1]=map[x][y]+1;
                queuex[rear]
=x;
                queuey[rear]
=y-1;
                rear
++;
            }

            
if(x>=0&&y+1<c&&map[x][y+1]==0)
            
{
                map[x][y
+1]=map[x][y]+1;
                queuex[rear]
=x;
                queuey[rear]
=y+1;
                rear
++;
            }

            
if(x-2>=0&&y-2>=0&&map[x-2][y-2]==0)
            
{
                map[x
-2][y-2]=map[x][y]+1;
                queuex[rear]
=x-2;
                queuey[rear]
=y-2;
                rear
++;
            }

            
if(x+2<r&&y-2>=0&&map[x+2][y-2]==0)
            
{
                map[x
+2][y-2]=map[x][y]+1;
                queuex[rear]
=x+2;
                queuey[rear]
=y-2;
                rear
++;
            }

            
if(x-2>=0&&y+2<c&&map[x-2][y+2]==0)
            
{
                map[x
-2][y+2]=map[x][y]+1;
                queuex[rear]
=x-2;
                queuey[rear]
=y+2;
                rear
++;
            }

            
if(x+2<r&&y+2<c&&map[x+2][y+2]==0)
            
{
                map[x
+2][y+2]=map[x][y]+1;
                queuex[rear]
=x+2;
                queuey[rear]
=y+2;
                rear
++;
            }

            front
++;
        }

        
if(front==rear)
            cout
<<"NO ANSWER"<<endl;
        
else
            cout
<<map[ex][ey]<<endl;
    }

    
return 0;
}

 

YOYO's Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

一个N*N的地图,我们的目标是从s点出发到达e点。
有两种走法,一种是步行,可以走到当前位置上下左右方向的任一格,
另一种是跳跃,可以走到当前位置的西南、东南、西北、东北的一格。
求到终点最少需要几步。


【粗略分析】

简单的走迷宫问题,
在原先的基础上增加了跳跃项,即增加了4个可能走的位置,
用DFS搜索到终点位置即退出。

【C++源代码】

#include < iostream >
#define  N 20
using   namespace  std;

int  main()
{
    
int map[N][N];
    
int queuex[N],queuey[N],front,rear;
    
int r,c,sx,sy,ex,ey;
    
int i,j,x,y,k;
    
char s;
    cin
>>k;
    
while(k--)
    
{
        cin
>>r>>c;
        
for(i=0;i<r;i++)
            
for(j=0;j<c;j++)
            
{
                cin
>>s;
                
if(s=='W')
                    map[i][j]
=-1;
                
else
                    map[i][j]
=0;
                
if(s=='S'){sx=i;sy=j;}
                
if(s=='X'){ex=i;ey=j;}
            }

        front
=0;rear=1;
        queuex[front]
=sx;queuey[front]=sy;

        
while(front<rear)
        
{
            x
=queuex[front];
            y
=queuey[front];
            
if(x==ex&&y==ey)
                
break;
            
if(x-1>=0&&y>=0&&map[x-1][y]==0)
            
{
                map[x
-1][y]=map[x][y]+1;
                queuex[rear]
=x-1;
                queuey[rear]
=y;
                rear
++;
            }

            
if(x+1<r&&y>=0&&map[x+1][y]==0)
            
{
                map[x
+1][y]=map[x][y]+1;
                queuex[rear]
=x+1;
                queuey[rear]
=y;
                rear
++;
            }

            
if(x>=0&&y-1>=0&&map[x][y-1]==0)
            
{
                map[x][y
-1]=map[x][y]+1;
                queuex[rear]
=x;
                queuey[rear]
=y-1;
                rear
++;
            }

            
if(x>=0&&y+1<c&&map[x][y+1]==0)
            
{
                map[x][y
+1]=map[x][y]+1;
                queuex[rear]
=x;
                queuey[rear]
=y+1;
                rear
++;
            }

            
if(x-2>=0&&y-2>=0&&map[x-2][y-2]==0)
            
{
                map[x
-2][y-2]=map[x][y]+1;
                queuex[rear]
=x-2;
                queuey[rear]
=y-2;
                rear
++;
            }

            
if(x+2<r&&y-2>=0&&map[x+2][y-2]==0)
            
{
                map[x
+2][y-2]=map[x][y]+1;
                queuex[rear]
=x+2;
                queuey[rear]
=y-2;
                rear
++;
            }

            
if(x-2>=0&&y+2<c&&map[x-2][y+2]==0)
            
{
                map[x
-2][y+2]=map[x][y]+1;
                queuex[rear]
=x-2;
                queuey[rear]
=y+2;
                rear
++;
            }

            
if(x+2<r&&y+2<c&&map[x+2][y+2]==0)
            
{
                map[x
+2][y+2]=map[x][y]+1;
                queuex[rear]
=x+2;
                queuey[rear]
=y+2;
                rear
++;
            }

            front
++;
        }

        
if(front==rear)
            cout
<<"NO ANSWER"<<endl;
        
else
            cout
<<map[ex][ey]<<endl;
    }

    
return 0;
}

【注意事项】

※ 记得有可能是NO ANSWER..

【点评】

没啥好说的……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值