标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1010
DFS
太弱, 改了好几个小时才改好。
可能出现的问题:
1.TLE:剪枝 N * M - walls > T、(T - (abs(x - x_end) + abs(y - y_end)) - t) 为奇数时, 无解(所谓的奇偶剪枝)
2.输入数据的行末可能有莫名空格……
然后上代码:
#include <iostream> #include <algorithm> using namespace std; char map[10 + 2][10 + 2]; int N, M, T, x_e, y_e; bool ok, vis[10 + 2][10 + 2]; void dfs(int x, int y, int dist) { int temp = T - dist - abs(x - x_e)-abs(y - y_e); if(!dist && (temp < 0||temp & 1)) return; if(ok) return ; if(dist >= T) { if(x == x_e && y == y_e) ok = true; return ; } else { if((map[x - 1][y] != ‘X‘) && !vis[x - 1][y]) {vis[x - 1][y] = true; dfs(x - 1, y, dist + 1); vis[x - 1][y] = false;} if((map[x][y - 1] != ‘X‘) && !vis[x][y - 1]) {vis[x][y - 1] = true; dfs(x, y - 1, dist + 1); vis[x][y - 1] = false;} if((map[x][y + 1] != ‘X‘) && !vis[x][y + 1]) {vis[x][y + 1] = true; dfs(x, y + 1, dist + 1); vis[x][y + 1] = false;} if((map[x + 1][y] != ‘X‘) && !vis[x + 1][y]) {vis[x + 1][y] = true; dfs(x + 1, y, dist + 1); vis[x + 1][y] = false;} } } int main() { while(scanf("%d%d%d%*c", &N, &M, &T) != EOF && N && M && T) { int x, y, Wall = 0; ok = false; for(int i = 1; i <= M; i++) map[0][i] = map[N + 1][i] = ‘X‘, vis[0][i] = vis[N + 1][i] = true; for(int i = 1; i <= N; i++) map[i][0] = map[i][M + 1] = ‘X‘, vis[i][0] = vis[i][M + 1] = true; for(int i = 1; i <= N; i++) { for(int r = 1; r <= M; r++) { scanf("%c", &map[i][r]); if(map[i][r] == ‘S‘) x = i, y = r; if(map[i][r] == ‘D‘) x_e = i, y_e = r; if(map[i][r] == ‘X‘) Wall++; vis[i][r] = false; } scanf("%*c"); } vis[x][y] = true; if(N * M - Wall >= T) dfs(x, y, 0); if(ok) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/QQ-1615160629/p/5399296.html