
 题意:如上图
题意:如上图
 思路:核心思想,奇偶剪枝+可行性剪枝。
 if((ex + ey + sx + sy + T) % 2 == 1)
 cout << “NO” << endl;
5 5 6
...S.
XX.X.
..X..
..D.X
..X..YES
using namespace std;
const ll maxn = 1e2 + 5;
const ll minn = 1e9 + 5;
//ll a[maxn], b[maxn];
//bool vis[maxn];
int ans[maxn];
vector<int>v[maxn];
bool vis[maxn][maxn];
//bool vis[maxn][maxn][2];
bool flag = 0;
int sum = 0;
char a[105][105];
int n, m, T;
void dfs(int x, int y, int t) {
    if(a[x][y] == 'X' || flag || x < 0 || y < 0 || x >= n || y >= m || vis[x][y] || t > T)
        return ;
    if(t <= T && a[x][y] == 'D') {
        flag = true;
        return;
    }
    vis[x][y] = true;
    dfs(x, y + 1, t + 1);
    dfs(x, y - 1, t + 1);
    dfs(x - 1, y, t + 1);
    dfs(x + 1, y, t + 1);
    vis[x][y] = false;
    return ;
}
int main() {
    int sx, sy, ex, ey;
    cin >> n >> m >> T;
    for(int i = 0; i < n; i++)
        cin >> a[i];
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(a[i][j] == 'S') {
                    sx = i;
                    sy = j;
                } else if(a[i][j] == 'D') {
                    ex = i;
                    ey = j;
                }
       
        if((ex + ey + sx + sy + T) % 2 == 1)
            cout << "NO" << endl;
        else {
           dfs(sx, sy, 0);
            if(flag)
                cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    return 0;
}再附上杭电一道相同的题。
传送门 思路;hdu的是Ts恰好到达,所以t<=T改成t==T.
using namespace std;
const ll maxn = 1e2 + 5;
const ll minn = 1e9 + 5;
//ll a[maxn], b[maxn];
//bool vis[maxn];
int ans[maxn];
vector<int>v[maxn];
bool vis[maxn][maxn];
//bool vis[maxn][maxn][2];
bool flag = 0;
int sum = 0;
char a[105][105];
int n, m, T;
void dfs(int x, int y, int t) {
    if(a[x][y] == 'X' || flag || x < 0 || y < 0 || x >= n || y >= m || vis[x][y] || t > T)
        return ;
    if(t == T && a[x][y] == 'D') {
        flag = true;
        return;
    }
    vis[x][y] = true;
    dfs(x, y + 1, t + 1);
    dfs(x, y - 1, t + 1);
    dfs(x - 1, y, t + 1);
    dfs(x + 1, y, t + 1);
    vis[x][y] = false;
    return ;
}
int main() {
    int sx, sy, ex, ey;
    while(cin >> n >> m >> T, n || m || T) {
        memset(vis, 0, sizeof(vis));
        memset(a, 0, sizeof(a));
        flag = false;
        for(int i = 0; i < n; i++)
            cin >> a[i];
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(a[i][j] == 'S') {
                    sx = i;
                    sy = j;
                } else if(a[i][j] == 'D') {
                    ex = i;
                    ey = j;
                }
        if((ex + ey + sx + sy + T) % 2 == 1)
            cout << "NO" << endl;
        else {
            dfs(sx, sy, 0);
            if(flag)
                cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}
//abs(ex-sx)+abs(ey-sy)>T||                
                










