Uva 10452 - Marcus

 1 #include <iostream>
 2 #define EOF (-1)
 3 
 4 using namespace std;
 5 
 6 char Path[12][100];
 7 int Step[7]={0};//记录走过的足迹
 8 char example[8] = {'@','I','E','H','O','V','A','#'};
 9 
10 void Dfs(int n,int m,int ex);
11 int main(){
12     int line,m,n;
13     string Commands[3]={"forth","left","right"};
14     //freopen("D:\\t.txt","r",stdin);
15     while((cin>>line)&&(line!=EOF)){
16         for(int i = 0;i < line;i++){
17             cin>>n>>m;//输入鹅卵石路的宽m,长n
18             for(int j=0;j<n;j++){
19                 for(int k=0;k<m;k++){
20                     cin>>Path[j][k];
21                 }
22             }
23 
24                 for(int q = 0;q < m;q++){
25                     if(example[0] == Path[n-1][q]){
26                         Dfs(n-1,q,0);
27                         break;
28                     }
29                 }
30             for(int t = 0;t < 7;t++){
31                 cout<<Commands[Step[t]];
32                 if(t<6)cout <<" ";
33             }
34             cout<<endl;
35         }
36     }
37     return 0;
38 }
39 
40 void Dfs(int n,int m,int ex)
41 {
42     if( (Path[n-1][m] == example[ex+1]) && (ex<7)) {
43         Step[ex] = 0;
44         Dfs(n-1,m,ex+1);
45     }
46     if( (Path[n][m-1] == example[ex+1]) && (ex<7)){
47         Step[ex] = 1;
48         Dfs(n,m-1,ex+1);
49     }
50     if( (Path[n][m+1] == example[ex+1]) && (ex<7)){
51         Step[ex] = 2;
52         Dfs(n,m+1,ex+1);
53     }
54 }

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

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

 
 
 
 

转载于:https://www.cnblogs.com/ohxiaobai/p/4047187.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值