错误原因:将图形四周都设置为墙的时候,位置设置错,将图像内的设置为墙了
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
char Line[8][8];
int Ti[8][8];
struct node {
int x;
int y;
int t;
};
int X[] = {0, 0, 1, -1};
int Y[] = {1, -1, 0, 0};
void Show() {
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 5; ++j) {
cout << Line[i][j];
}
cout << endl;
}
}
bool dfs(node ch, int x2, int y2) {
//cout << "-----" << endl;
//cout << ch.t << " " << ch.x << " " << ch.y << endl;
node temp;
bool flag;
//Show();
if (ch.t < 0 || (ch.t == 0 && ch.x != x2 && ch.y != y2)) {
//cout << "111" << endl;
return false;
}
for (int i = 0; i < 4; ++i) {
temp.x = ch.x + X[i];
temp.y = ch.y + Y[i];
temp.t = ch.t - 1;
if (temp.t < 0) {
//cout << "222" << endl;
return false;
}
if (temp.x == x2 && temp.y == y2) {
if (temp.t == 0)
return true;
}
if (Line[temp.x][temp.y] == '.') {
//cout << "(" << temp.x << " " << temp.y << " " << temp.t << ")" << endl;
Line[temp.x][temp.y] = 'X';
flag = dfs(temp, x2, y2);
Line[temp.x][temp.y] = '.';
if (flag)
return true;
}
}
return false;
}
int main() {
int N, M, T;
int x1, y1, x2, y2;
while (cin >> N >> M >> T) {
if (N == 0 && M == 0 && T == 0) {
break;
}
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= M; ++j) {
cin >> Line[i][j];
Ti[i][j] = 0;
if (Line[i][j] == 'S') {
x1 = i;
y1 = j;
} else if (Line[i][j] == 'D') {
x2 = i;
y2 = j;
}
}
}
for (int i = 0; i < N; ++i) {
Line[i][0] = 'X';
Line[i][M + 1] = 'X';
}
for (int i = 0; i <= M; ++i) {
Line[0][i] = 'X';
Line[N + 1][i] = 'X';
}
node ch;
ch.x = x1;
ch.y = y1;
ch.t = T;
stack<node> Nodes;
Nodes.push(ch);
if ((T - abs(ch.x - x2) - abs(ch.y - y2)) % 2 != 0) {
cout << "NO" << endl;
continue;
}
bool flag = dfs(ch, x2, y2);
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}