zoj 2110 骨头的诱惑(Tempter of the Bone) DFS

地址  https://vjudge.net/problem/ZOJ-2110

 

 

 

初始看就是个简单的DFS 或者BFS

后来卡了半天 才发现 是恰好T秒达到.......

做了剪枝 修改了成功退出的判断条件为 == T 然后ac了

复制代码
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <string>
 5 #include <memory.h>
 6 
 7 
 8 using namespace std;
 9 
10 int n, m, t;
11 
12 const int MAX_SIZE = 90;
13 
14 char G[MAX_SIZE][MAX_SIZE];
15 
16 int currx, curry;
17 
18 int addx[] = { 0,0,1,-1 };
19 int addy[] = { 1,-1,0,0 };
20 
21 int endX;
22 int endY;
23 
24 
25 
26 int Dfs(int x, int y, int limit)
27 {
28     int count = 0;
29 
30     if (x < 0 || x >= n || y < 0 || y >= m) {
31         return 0;
32     }
33     if (G[x][y] == 'X')
34         return 0;
35     if (G[x][y] == 'D' && limit == t)
36         return 1;
37     if (limit > t)
38         return 0;
39 
40     int temp = (t - limit) - abs(x - endX) - abs(y - endY);
41     
42     if (temp < 0 )
43         return 0;
44 
45 
46     for (int i = 0; i < 4; i++) {
47         int newx = addx[i] + x;
48         int newy = addy[i] + y;
49         char oldV = G[x][y];
50         G[x][y] = 'X';
51         if (Dfs(newx, newy, limit + 1) == 1)
52             return 1;
53         G[x][y] = oldV;
54     }
55 
56     return 0;
57 }
58 
59 
60 int main()
61 {
62     while (scanf("%d%d%d", &n, &m, &t)) {
63         if (n == m && m== t && t == 0) 
64             break;
65 
66         memset(G, 0, sizeof G);
67 
68         for (int i = 0; i < n; i++)
69             for (int j = 0; j < m; j++) {
70                 cin >> G[i][j];
71                 if (G[i][j] == 'S') {
72                     currx = i;
73                     curry = j;
74                 }
75                 else if (G[i][j] == 'D') {
76                     endX = i;
77                     endY = j;
78                 }
79             }
80         if (Dfs(currx, curry, 0) != 0)
81             printf("YES\n");
82         else
83             printf("NO\n"); 
84     }
85 
86     return 0;
87 }
View Code
复制代码

 

posted on   itdef  阅读(459)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2016-03-18 boost asio 学习(四)使用strand将任务排序
2016-03-18 boost asio 学习(三)post与dispatch

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示