UVa 10452 - Marcus

#include <iostream>
#define EOF (-1)

using namespace std;

char Path[12][100];
int Step[7]={0};//记录走过的足迹
char example[8] = {'@','I','E','H','O','V','A','#'};

void Dfs(int n,int m,int ex);
int main(){
    int line,m,n;
    string Commands[3]={"forth","left","right"};
    //freopen("D:\\t.txt","r",stdin);
    while((cin>>line)&&(line!=EOF)){
        for(int i = 0;i < line;i++){
            cin>>n>>m;//输入鹅卵石路的宽m,长n
            for(int j=0;j<n;j++){
                for(int k=0;k<m;k++){
                    cin>>Path[j][k];
                }
            }

                for(int q = 0;q < m;q++){
                    if(example[0] == Path[n-1][q]){
                        Dfs(n-1,q,0);
                        break;
                    }
                }
            for(int t = 0;t < 7;t++){
                cout<<Commands[Step[t]];
                if(t<6)cout <<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

void Dfs(int n,int m,int ex)
{
    if( (Path[n-1][m] == example[ex+1]) && (ex<7)) {
        Step[ex] = 0;
        Dfs(n-1,m,ex+1);
    }
    if( (Path[n][m-1] == example[ex+1]) && (ex<7)){
        Step[ex] = 1;
        Dfs(n,m-1,ex+1);
    }
    if( (Path[n][m+1] == example[ex+1]) && (ex<7)){
        Step[ex] = 2;
        Dfs(n,m+1,ex+1);
    }
}


原来递归运算时陷入了死循环,加上“ex>7”的判定,就好了;
输入的时候注意m,n(我是新手(*^__^*) )

题目意思很简单,就是沿着一个路径走到底就行,用dfs解决。

 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值