UVA 10452 Marcus, help!( DFS )

Problem I

Marcus, help!

Input: standard input

Output: standard output

Time Limit: 2 Seconds

"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

[email protected]

5 4

JA#X

JVBN

XOHD

DQEM

[email protected]

Sample Output

forth forth right right forth forth forth

right forth forth left forth forth right


Problem-setter: Juergen Werner, Albert Einstein University of ULM, Germany

题目大意:

给你一个矩阵,只能按照IEHOVA的顺序走,输出轨迹的方向。@为起始点,#为终止点。

解题思路:

DFS,注意是从下往上走,i是减一的。

代码:

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

const int maxN=10;
const char letter[8]={'@','I','E','H','O','V','A','#'};
const string str0[3]={"left","forth","right"};
int dirX[3]={-1,0,1},n,m;
int dirY[3]={0,-1,0};//i是减不是加。
string mymap[maxN];

bool cmp(int di,int dj){
    if(di>=0&&di<n&&dj>=0&&dj<m) return true;
    return false;
}

void dfs(int i,int j,int depth){
    for(int k=0;k<3;k++){
        int di=i+dirY[k],dj=j+dirX[k];
        if(cmp(di,dj)&&mymap[di][dj]==letter[depth]){
            cout<<str0[k];
            if(depth<=6) cout<<" ";
            dfs(di,dj,depth+1);
        }
    }
}

int main(){
    int t,pi,pj;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++){
            cin>>mymap[i];
            for(int j=0;j<m;j++){
                if(mymap[i][j]==letter[0]) pi=i,pj=j;
            }
        }
        dfs(pi,pj,1);
        cout<<endl;
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

const int maxN=10;
const char letter[8]={'@','I','E','H','O','V','A','#'};
const string str0[3]={"left","forth","right"};
int dirX[3]={-1,0,1},n,m;
int dirY[3]={0,-1,0};//i是减不是加。
string mymap[maxN];

bool cmp(int di,int dj){
    if(di>=0&&di<n&&dj>=0&&dj<m) return true;
    return false;
}

void dfs(int i,int j,int depth){
    for(int k=0;k<3;k++){
        int di=i+dirY[k],dj=j+dirX[k];
        if(cmp(di,dj)&&mymap[di][dj]==letter[depth]){
            cout<<str0[k];
            if(depth<=6) cout<<" ";
            dfs(di,dj,depth+1);
        }
    }
}

int main(){
    int t,pi,pj;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++){
            cin>>mymap[i];
            for(int j=0;j<m;j++){
                if(mymap[i][j]==letter[0]) pi=i,pj=j;
            }
        }
        dfs(pi,pj,1);
        cout<<endl;
    }
    return 0;
}
时间: 2024-10-27 12:15:31

UVA 10452 Marcus, help!( DFS )的相关文章

UVa 10452 - Marcus

题目:贪食蛇,在迷宫中,按照路径"@IEHOVA#"行走,每次只能走到相邻格子(左.右.前三个): 解一定存在,输出行走路径. 分析:搜索.简单搜索,安路径行走即可,走的时候注意不能选择回头方向. 说明:只走8步,计算量好小. #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> char map[101][101]; char run[] = &

UVa 572 Oil Deposits(DFS)

 Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

uva 12253 - Simple Encryption(dfs)

题目链接:uva 12253 - Simple Encryption 题目大意:给定K1,求一个12位的K2,使得KK21=K2%1012 解题思路:按位枚举,不且借用用快速幂取模判断结果. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const ll ite=(1<<20)-1; ll N; /* l

Uva LA6450 Social Advertising DFS

You have decided to start up a new social networking company. Other existing popular social networksalready have billions of users, so the only way to compete with them is to include novel features noother networks have.Your company has decided to ma

UVa 208 消防车(dfs+剪枝)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=144 题意:给出一个n个结点的无向图以及某个结点k,按照字典序从小到大顺序输出从1到结点k的所有路径. 思路:如果直接矩阵深搜的话是会超时的,所以我们可以从终点出发,将与终点相连的连通块保存起来,这样dfs深搜时可以剪枝掉一些到达不了的点.只要解决了这个,dfs就是小问题. 这道题还有点坑的

UVA 1103 Ancient Messages (DFS)

起初学习dfs的时候 看不懂这个题目  回过头来今天看的时候思路相对比较清晰了.但还是想不到怎么处理 查询有多少个空白洞.并且一个图中还有好多个象形字符.网上思路是 在周围扩展一圈0,使得文字之外的所有0 都连通,一次dfs标记所有文字之外的0为-1.然后遍历图,发现1就沿着有1 的路径dfs2,在这个过程中,如果发现0,那么必然是文字内部的空洞,此时把空洞dfs 置为-1,标记以后不再走.那么发现几次0就有几个空洞在文字中.那么每一次的dfs2,就处理了一个象形文字.cnt的值就是空洞的个数即

UVA - 1103Ancient Messages(dfs)

UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study texts written in ancient languages.One such language, used in Egypt more than 3000 years ago, is based on characters called hieroglyphs.Figure C.1 shows

UVa 572 油田(DFS求连通块)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=513 终于开始接触图了,恩,开始接触DFS了,这道题就是求连通分量,比较简单. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int m, n; //记录连通块的数量 6 cha