HDU 1010 Tempter of the Bone (DFS+奇偶剪枝,一个起点一个终点)
2017-07-21 11:33
471 查看
Description
给定一个图的起点begin和终点end以及所需要的时间,X不能通过,
.可以通过,
S是开始坐标,
D结束坐标,问是否恰好在T秒到达终点。
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:‘X’: a block of wall, which the doggie cannot enter;
‘S’: the start point of the doggie;
‘D’: the Door; or
‘.’: an empty block.
The input is terminated with three 0’s. This test case is not to be processed.
Output
For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.Sample Input
4 4 5S.X.
..X.
..XD
….
3 4 5
S.X.
..X.
…D
0 0 0
Sample Output
NOYES
Solution
DFS+奇偶剪枝奇偶剪枝:根据题目,doggie必须在第t秒到达门口。也就是需要走t-1步。设doggie开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+abs(ey-y)为偶数,则abs(ex-x)和abs(ey-y)奇偶性相同,所以需要走偶数步;
当abs(ex-x)+abs(ey-y)为奇数时,则abs(ex-x)和abs(ey-y)奇偶性不同,到达目标位置就需要走奇数步。先判断奇偶性再搜索可以节省很多时间,不然的话容易超时。t-sum为到达目标位置还需要多少步。因为题目要求doggie必须在第t秒到达门的位置,所以(t-step)和abs(ex-x)+abs(ey-y)的奇偶性必然相同。因此temp=(t-step)-abs(ex-x)+abs(ey-y)必然为偶数。
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <ctime> #include <vector> using namespace std; #define ONLINE_JUDGE const int maxn = 11000; int N, M, T, dx, dy; char m[maxn][10]; bool visit[maxn][10]; bool ANS; int x[4] = {0, 0, -1, 1}; int y[4] = {-1, 1, 0, 0}; void init() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { m[i][j] = '.'; visit[i][j] = false; } } ANS = false; } void dfs(int i, int j, int step) { int temp1 = abs(i - dx) - abs(j - dy); int temp2 = T - step; if (abs(temp1 - temp2) % 2 == 1 || temp2 < 0) { return; } if (m[i][j] == 'D' && step == T) { ANS = true; return; } if (ANS) { return; } for (int ii = 0; ii < 4; ii++) { int nx = i + x[ii]; int ny = j + y[ii]; if (!visit[nx][ny] && nx >= 1 && nx <= N && ny >= 1 && ny <= M && m[nx][ny] != 'X') { visit[nx][ny] = true; dfs(nx, ny, step + 1); visit[nx][ny] = false; } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); long _begin_time = clock(); #endif while (~scanf("%d%d%d", &N, &M, &T) && (N || M || T)) { init(); int xx, yy, num_x = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { // scanf("%c", &m[i][j]); cin >> m[i][j]; if (m[i][j] == 'S') { xx = i; yy = j; } else if (m[i][j] == 'D') { dx = i; dy = j; } else if (m[i][j] == 'X') { num_x++; } } } if (N * M - num_x < T + 1) { printf("NO\n"); continue; } visit[xx][yy] = true; dfs(xx, yy, 0); if (ANS) printf("YES\n"); else printf("NO\n"); } #ifndef ONLINE_JUDGE long _end_time = clock(); printf("time = %ld ms\n", _end_time - _begin_time); #endif return 0; }
相关文章推荐
- hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone dfs搜索 奇偶剪枝 解题报告
- hdu-1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDU 1010 Tempter of the Bone (DFS+奇偶剪枝)
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu-1010 Tempter of the Bone(DFS之奇偶剪枝)
- HDU 1010-Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu 题目1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)
- 4000 HDU 1010 - Tempter of the Bone(DFS 奇偶剪枝)
- 【dfs+奇偶剪枝】HDU_1010_Tempter of the Bone
- 【HDU 1010】Tempter of the Bone(DFS,奇偶剪枝)
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone (dfs+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)