博客
关于我
题目: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/

    你可能感兴趣的文章
    mysql_real_connect 参数注意
    查看>>
    mysql_secure_installation初始化数据库报Access denied
    查看>>
    MySQL_西安11月销售昨日未上架的产品_20161212
    查看>>
    Mysql——深入浅出InnoDB底层原理
    查看>>
    MySQL“被动”性能优化汇总
    查看>>
    MySQL、HBase 和 Elasticsearch:特点与区别详解
    查看>>
    MySQL、Redis高频面试题汇总
    查看>>
    MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
    查看>>
    mysql一个字段为空时使用另一个字段排序
    查看>>
    MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
    查看>>
    MYSQL一直显示正在启动
    查看>>
    MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
    查看>>
    MySQL万字总结!超详细!
    查看>>
    Mysql下载以及安装(新手入门,超详细)
    查看>>
    MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
    查看>>
    MySQL不同字符集及排序规则详解:业务场景下的最佳选
    查看>>
    Mysql不同官方版本对比
    查看>>
    MySQL与Informix数据库中的同义表创建:深入解析与比较
    查看>>
    mysql与mem_细说 MySQL 之 MEM_ROOT
    查看>>
    MySQL与Oracle的数据迁移注意事项,另附转换工具链接
    查看>>