• Uva 10452


     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解决。

    Donghua University
  • 相关阅读:
    纯快排
    Quick Sort(快速排序)
    归并排序 ALDS1_5_B:Merge Sort
    单调栈(POJ2559)
    Sequence
    Codeforces Round #503
    度度熊学队列
    Always Online hdu 6350
    Hills And Valleys
    Problem G. Depth-First Search
  • 原文地址:https://www.cnblogs.com/ohxiaobai/p/4047187.html
Copyright © 2020-2023  润新知