• zoj 2110 dfs+步长


    dfs搜索。

    搜索位置=D,而且步长=T, return, 可以逃出,输出“YES”。否则,不能逃出,输出“NO”。

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string>
     4 #include<string.h>
     5 #define N 9
     6 using namespace std;
     7 char map[N][N];
     8 int flag[N][N];
     9 int m,n,t;
    10 int Si,Sj;
    11 int Di,Dj;
    12 bool escape;
    13 int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}} ;  // 方向是上右下左的顺时针
    14 void dfs(int x,int y,int cnt)
    15 {
    16     int xx,yy,i;
    17     if( x==Di && y== Dj && cnt==t)
    18     {
    19         escape=1;
    20         return ;
    21     }
    22     for(i=0;i<4;i++)
    23     {
    24         xx=x+dir[i][0];
    25         yy=y+dir[i][1];
    26         if(xx<0 || xx>=m ||yy<0 || yy>=n) continue;
    27         if(!flag[xx][yy])
    28         {
    29             flag[xx][yy]=1;
    30             dfs(xx,yy,cnt+1);
    31             if(escape) return ;
    32             flag[xx][yy]=0;
    33 
    34         }
    35     }
    36     return ;
    37 }
    38 int main()
    39 {
    40    while(cin>>m>>n>>t && m && n && t)
    41    {
    42         int i,j;
    43         escape=0;
    44         for(i=0;i<m;i++)
    45         {
    46             for(j=0;j<n;j++)
    47             {
    48                 cin>>map[i][j];
    49                 if(map[i][j]=='S')
    50                 {
    51                     Si=i, Sj=j;
    52                 }
    53                 else if(map[i][j] == 'D')
    54                 {
    55                     Di=i, Dj=j;
    56                     flag[i][j]=0;
    57                 }
    58 
    59                 else if(map[i][j] == '.')
    60                     flag[i][j]=0;
    61                 else
    62                     flag[i][j]=1;
    63             }
    64         }
    65         flag[Si][Sj]=1;
    66         dfs(Si,Sj,0);
    67         if(escape) cout<<"YES"<<endl;
    68         else cout<<"NO"<<endl;
    69    }
    70     return 0 ;
    71 }
  • 相关阅读:
    使用python三方库xlrd解析excel数据
    testng之listener
    使用Junit实现批量运行
    gojs绘流程图
    sqlserver
    Android学习笔记之 android:collapseColumns ,android:shrinkColumns 和stretchColumns
    myBatis oracle 与mysql自增问题
    Oracle总结
    Oracle 树操作(select…start with…connect by…prior)
    Oracle 获取当前日期及日期格式
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3574195.html
Copyright © 2020-2023  润新知