#include<iostream>
#include<cstring>
using namespace std;
int Move[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
char map[9][9];
int N, M, T; bool judge;int sx, sy, ex, ey;
bool in(int a, int b) {
return a > 0 && a <= N && b > 0 && b <= M&&map[a][b]!='X';
}//判断点在范围内
void dfs(int sx,int sy,int time){
if (time == T && sx == ex && sy == ey) judge = 1;
if (judge) return;
for (int i = 0; i < 4; i++) {
int nex = sx + Move[i][0], ney = sy + Move[i][1];
if (in(nex, ney)) {
map[nex][ney] = 'X';
dfs(nex, ney, time + 1);
map[nex][ney] = '.';
}
}
}
int main() {
int wall;
while (cin >> N>>M>>T) {
if (N == 0 && M == 0 && T == 0) break;
wall = 0;
judge = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
cin >> map[i][j];
if (map[i][j] == 'S') { sx = i; sy = j; }
if (map[i][j] == 'D') { ex = i; ey = j; }
if (map[i][j] == 'X') wall++;
}
}
int temp = T - abs(sx - ex) - abs(sy - ey);
if (T >= N * M - wall || temp < 0) {
cout << "NO" << endl; continue;
}
else {
map[sx][sy] = 'X';
dfs(sx, sy, 0);
if (judge)cout << "YES" << endl;
else cout << "NO" << endl;
}
}
}
第一个cout后面漏了continue
以及dfs前需要将map[sx][sy]='X'容易遗漏
|