zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    3.1 创建模型-实体属性
    3. 创建模型
    2.1 DbContext
    2. EF Core 如何显示执行的SQL语句
    1.1 为现有数据库生成实体模型
    1. EF Core 概述
    【2020-08-01】人生十三信条
    【一句日历】2020年8月
    【2020-07-31】一个像我一样精力充沛的孩子
    【2020-07-30】强大内心是自己的义务
  • 原文地址:https://www.cnblogs.com/ohxiaobai/p/4047187.html
Copyright © 2011-2022 走看看