传送门:QAQ
题意:问是否能在固定步数内到达终点
思路:简单的深搜,因为限定步数较少,加上奇偶路径判断即可(读入方式有点BUG,用%c一个个读就wa了)。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int
char ch[10][10];
int vis[10][10];
int dx[5] = { 1,0,-1,0 };
int dy[5] = { 0,1,0,-1 };
int X1, Y1;
int xx, yy;
int n, m, t;
int flag;
int fla;
void dfs(int x, int y, int tt) {
if (tt>t) {
//printf("%d %d %d************\n", x, y,tt);
return;
}
if (ch[x][y] == 'D') {
if (tt == t)
fla = 1;
return;
}
for (int i = 0; i < 4; i++) {
int x2 = x + dx[i];
int y2 = y + dy[i];
if (x2 >= 0 && x2 < n&&y2 >= 0 && y2 < m&&ch[x2][y2] != 'X' && !vis[x2][y2]) {
vis[x2][y2] = 1;
dfs(x2, y2, tt + 1);
vis[x2][y2] = 0;
if (fla)
return;
}
}
}
int main(void) {
while (scanf("%d%d%d", &n, &m, &t) != EOF) {
flag = 0;
fla = 0;
memset(vis, 0, sizeof(vis));
if (n == 0 && m == 0 && t == 0) {
break;
}
for (int i = 0; i < n; i++) {
scanf("%s", &ch[i]);
for (int z = 0; z < m; z++) {
if (ch[i][z] == 'D') {
X1 = i;
Y1 = z;
}
if (ch[i][z] == 'S') {
xx = i;
yy = z;
}
}
}
if ((abs(X1 - xx) + abs(Y1 - yy)) % 2 != t % 2) {
printf("NO\n");
continue;
}
vis[xx][yy] = 1;
dfs(xx, yy, 0);
if (fla) {
printf("YES\n");
}
else
printf("NO\n");
}
return 0;
}