博客
关于我
题目:HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
阅读量:336 次
发布时间:2019-03-04

本文共 3497 字,大约阅读时间需要 11 分钟。

为了解决这个问题,我们需要判断狗狗是否能够在指定的时间T到达迷宫中的门。迷宫中的每个位置可以用0或1表示,奇偶性不同的位置相邻。狗狗只能在特定时间T到达门,且每秒只能移动一次,不能重复经过任何一个格子。

方法思路

  • 问题分析:狗狗需要在特定时间T到达门,且路径必须符合奇偶性条件。奇偶性剪枝方法可以帮助我们判断是否能到达目标位置。起点和终点的奇偶性决定了狗狗是否能在奇数或偶数步到达目标位置。

  • 广度优先搜索(BFS):使用BFS遍历迷宫,记录每个位置和步数,确保路径不重复。同时,检查每一步是否满足奇偶性条件。

  • 奇偶性检查:起点和终点的奇偶性决定了狗狗到达终点的步数必须是奇数或偶数。BFS中每一步生成新状态时,检查是否满足奇偶性条件。

  • 解决代码

    #include 
    #include
    using namespace std;int n, m, t;char maze[11][11]; // 1-based indexingint startx, starty, endx, endy;bool flag = false;int dx[] = {0, 0, 1, -1};int dy[] = {1, -1, 0, 0};void dfs(int x, int y, int step) { if (flag) return; if (x == endx && y == endy && step == t) { flag = true; return; } if (step > t) return; for (int k = 0; k < 4; ++k) { int nx = x + dx[k]; int ny = y + dy[k]; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; if (maze[nx][ny] == 'X') continue; int required_step = step + 1; int start_odd = (startx + starty) % 2; int end_odd = (endx + endy) % 2; if ((start_odd == end_odd && (required_step % 2 != 0)) || (start_odd != end_odd && (required_step % 2 == 0))) { continue; } if (visited[nx][ny][required_step]) continue; visited[nx][ny][required_step] = true; dfs(nx, ny, required_step); }}int main() { while (true) { cin >> n >> m >> t; if (n == 0 && m == 0 && t == 0) break; for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= m; ++j) { maze[i][j] = s[j-1]; if (maze[i][j] == 'S') { startx = i; starty = j; } else if (maze[i][j] == 'D') { endx = i; endy = j; } } } int start_odd = (startx + starty) % 2; int end_odd = (endx + endy) % 2; bool found = false; bool visited[n+1][m+1][t+2]; for (int x = 1; x <= n; ++x) { for (int y = 1; y <= m; ++y) { for (int step = 0; step <= t; ++step) { visited[x][y][step] = false; } } } queue
    > q; q.push({startx, starty, 0}); visited[startx][starty][0] = true; while (!q.empty()) { auto current = q.front(); q.pop(); int x = current.first, y = current.second, step = current.third; if (x == endx && y == endy && step == t) { found = true; break; } for (int k = 0; k < 4; ++k) { int nx = x + dx[k]; int ny = y + dy[k]; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; if (maze[nx][ny] == 'X') continue; int required_step = step + 1; if ((start_odd == end_odd && (required_step % 2 != 0)) || (start_odd != end_odd && (required_step % 2 == 0))) { continue; } if (visited[nx][ny][required_step]) continue; visited[nx][ny][required_step] = true; q.push({nx, ny, required_step}); } } if (found) { cout << "YES"; } else { cout << "NO"; } }}

    代码解释

  • 读取输入:处理每个测试用例,读取迷宫布局,找到起点和终点。
  • 奇偶性检查:计算起点和终点的奇偶性,判断狗狗是否能在奇数或偶数步到达终点。
  • BFS遍历:使用队列进行BFS,记录每个位置和步数,检查是否满足奇偶性条件和路径限制。
  • 判断结果:如果找到符合条件的路径,输出“YES”;否则,输出“NO”。
  • 转载地址:http://ccnh.baihongyu.com/

    你可能感兴趣的文章
    PHP加密与安全的最佳实践
    查看>>
    PHP加速器eaccelerator导致php-fpm进程卡死原因分析
    查看>>
    PHP区分 企业微信浏览器 | 普通微信浏览器 | 其他浏览器
    查看>>
    php原生代码怎么连表查询,PHP tp5中使用原生sql查询代码实例
    查看>>
    PHP去掉转义符
    查看>>
    php去除字符串开头或末尾的字符(例如逗号)
    查看>>
    php反射api
    查看>>
    PHP反射ReflectionClass、ReflectionMethod 入门教程
    查看>>
    PHP反射机制
    查看>>
    php取当天的最后一秒_Docker快速搭建PHP开发环境详细教程
    查看>>
    php取绝对值
    查看>>
    PHP变量内容的获取
    查看>>
    php各种常用的算法
    查看>>
    php各种缓存策略对比
    查看>>
    RabbitMQ高级特性 - 消息分发(限流、负载均衡)
    查看>>
    php后台“爬虫”模拟登录第三方系统
    查看>>
    php后台的在控制器中就可以实现阅读数增加
    查看>>
    php命令行生成项目结构
    查看>>
    php命名空间
    查看>>
    PHP命名空间带来的干扰
    查看>>