因为能走的是四个方向,所以就可以将图中的点分为奇数点和偶数点,不同性质的点一定是在奇数步到达,相同性质的是在偶数点到达,做完这个剪枝,基本上就不会超时了

然后题中明确说明走过的点不能再走

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#define MAX 11

using namespace std;

bool used[MAX][MAX];
int mp[MAX][MAX];
int n,m,t;
int sx,sy ,ex, ey;
bool ans;

int dx[] = { 0 , 1 , 0 , -1 , 0};
int dy[] = { 0 , 0 , 1 , 0 , -1};

void dfs ( int x , int y , int h = 0 )
{
    if ( ans ) return;
    if ( h > t ) return;
    int temp = abs (x-ex) + abs (y-ey) - (t-h);
    if ( temp > 0 || (temp&1) ) return;
    if ( mp[x][y] == 2 && h ==  t )
    {
        ans = true;
        return;
    }
    for ( int i = 1 ; i <= 4 ; i++ )
    {
        int u = x+dx[i] , v = y + dy[i];
        if ( !mp[u][v] ) continue;
        if ( used[u][v] ) continue;
        used[u][v] = true;
        dfs ( u , v , h+1 );
        used[u][v] = 0;
    }
}


int main ( )
{
    while ( ~scanf ( "%d%d%d" , &n , &m , &t ) )
    {
        if ( !n && !m && !t ) break;
        char s[50];
        ans = false;
        memset ( mp , 0 , sizeof ( mp ));
        memset ( used , 0 , sizeof ( used ) );
        for ( int i = 1; i <= n ; i++ )
        {
            scanf ( "%s" , s+1 ); 
            for ( int j = 1 ; j <= m ;j++ )
                if ( s[j]=='S' ) sx = i , sy = j,mp[i][j] = 1;
                else if ( s[j]=='.' ) mp[i][j] = 1;
                else if ( s[j]=='D' ) mp[i][j] = 2, ex = i , ey = j;
        }
        used[sx][sy] = true;
        dfs ( sx , sy );
        if ( ans ) puts ( "YES" );
        else puts ( "NO" );
    }
}