UVA 10452 Marcus

38 篇文章 0 订阅
12 篇文章 0 订阅

“First, the breath of God.
Only the penitent man will pass.
Second, the Word of God,
Only in the footsteps of God will he proceed.
Third, the Path of God,
Only in the leap from the lion’s head will he prove his worth.”
(taken from the movie “Indiana Jones and the Last Crusade”, 1989)
To get to the grail, Indiana Jones needs to pass three challenges. He successfully masters the first one and steps up to the second. A cobblestone path lies before him, each cobble is engraved with a letter. This is the second challenge, the Word of God, the Name of God. Only the cobbles having one of the letters “IEHOVA” engraved can be stepped on safely, the ones having a different letter will break apart and leave a hole.
Unfortunately, he stumbles and falls into the dust, and the dust comes into his eyes, making it impossible for him to see. So he calls for Marcus Brody and asks Marcus to tell him in which direction to go to safely reach the other side of the cobblestone path. Because of the dust in his eyes, Indy only can step “forth” to the stone right in front of him or do a side-step to the stone on the “left” or the “right” side of him. These (‘forth’, ‘left’, ‘right’) are also the commands Marcus gives to him.
Input
The first line of the input contains a single number indicating the number of test cases that follow. Each test case starts with a line containing two numbers m and n (2 ≤ m,n ≤ 8), the length m and the width n of the cobblestone path. Then follow m lines, each containing n characters (‘A’ to ‘Z’, ‘@’, ‘#’), the engravement of the respective cobblestone. Indy’s starting position is marked with the ‘@’ character in the last line, the destination with the character ‘#’ in the first line of the cobblestone path. Each of the letters in ‘IEHOVA’ and the characters ‘@’ and ‘#’ appear exactly once in each test case. There will always be exactly one path from Indy’s starting position via the stones with the letters ‘IEHOVA’ engraved on (in that order) to the destination. There will be no other way to safely reach the
destination.
Output
For each test case, output a line with the commands Marcus gives to Indy so that Indy safely reaches the other side. Separate two commands by one space character.
Sample Input
2
6 5
PST#T
BTJAS
TYCVM
YEHOF
XIBKU
N@RJB
5 4
JA#X
JVBN
XOHD
DQEM
T@IY
Sample Output
forth forth right right forth forth forth
right forth forth left forth forth right

题意:
给出一个由字母和@,#组成的地图,@表示起点,#表示终点,只有left,right,forth三个方向可以走,而且必须按照“IEHOVA”的顺序走才安全,输出从起点走到终点的过程(最后无空格)

#include <iostream>
using namespace std;
char map[10][10];
int dx[]={0,-1,0};//列出左,右,上三个方向
int dy[]={1,0,-1};
int m,n,pos=0;//pos记录当前该走letter中的哪一个字母
string letter="IEHOVA#";//储存安全的路线顺序
void initial(int m,int n)
{
    for(int k=0;k<m;k++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>map[k][j];
        }
    }
}
void dfs(int x,int y)
{
    char ch;
    int nx,ny;
    ch=letter[pos];
    if(ch=='\n')//表示已走到终点
        return;
    else
    {
    for(int i=0;i<3;i++)
    {
        nx=x+dx[i];
        ny=y+dy[i];
        if(nx>=0&&nx<m&&ny>=0&&ny<n&&map[nx][ny]==ch)
        {
            if(i==0)
                cout<<"right";
            if(i==1)
                cout<<"forth";
            if(i==2)
                cout<<"left";
            pos++;
            if(pos<7)
                cout<<" ";//输出格式
            dfs(nx,ny);
        }
    }
    }
}
int main()
{
    int T,j;
    cin>>T;
    while(T--)
    {
        pos=0;//每次都初始化为0
        cin>>m>>n;
        initial(m,n);
        for(j=0;j<n;j++)
        {
            if(map[m-1][j]=='@')//找到起点
            {
                dfs(m-1,j);//开始搜索
                cout<<endl;
                break;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值