标签:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; int flag, sx, sy, dx, dy, num; int n, m, t; char map[10][10]; int is[10][10]; int Dx[4] = {-1,0,1,0}; int Dy[4] = {0,-1,0,1}; void dfs(int nowx,int nowy,int num) { int nextx, nexty; if(flag == 1) return; if(nowx == dx && nowy == dy && num == t) { flag = 1; return; } int min = abs(nowx - dx) + abs(nowy - dy); if(min > t - num || (min + t - num ) % 2 != 0) return; for(int i = 0; i < 4; i++) { nextx = nowx + Dx[i]; nexty = nowy + Dy[i]; if(nextx >= 0 && nextx < m && nexty >= 0 && nexty < n && !is[nextx][nexty] && map[nextx][nexty] != ‘X‘) { is[nextx][nexty] = 1; dfs(nextx, nexty, num + 1); is[nextx][nexty] = 0; } } } int main() { //freopen("aaa.txt", "r", stdin); while(scanf("%d %d %d", &m, &n, &t)) { if(n ==0 && m == 0 && t == 0) break; int d = 0; for(int i = 0; i < m; i++) { scanf("%s", map[i]); for(int j = 0; j < n; j++) { if(map[i][j]==‘S‘) { sx = i; sy = j; } if(map[i][j] == ‘D‘) { dx = i; dy = j; } if(map[i][j] == ‘X‘) d++; } } if(n * m - num - 1 < t) { printf("NO\n"); continue; } flag = 0; memset(is, 0, sizeof(is)); is[sx][sy] = 1; dfs(sx, sy, 0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5401967.html