给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。

输入描述:

  1. 每个输入包含 1 个测试用例。每个测试用例的第一行包含两个整数 n m1 <= n, m <= 50),表示地牢的长和宽。接下来的 n 行,每行 m 个字符,描述地牢,地牢将至少包含两个 '.'。接下来的一行,包含两个整数 x

0

  1. , y

0

  1. ,表示牛牛的出发位置(0 <= x0 < n, 0 <= y0 < m,左上角的坐标为 0, 0),出发位置一定是 '.')。之后的一行包含一个整数 k0 < k <= 50)表示牛牛合法的步长数,接下来的 k 行,每行两个整数 dx, dy 表示每次可选择移动的行和列步长(-50 <= dx, dy <= 50

输出描述:

  1. 输出一行一个数字表示最坏情况下需要多少次移动可以离开地牢,如果永远无法离开,输出 -1。以下测试用例中,牛牛可以上下左右移动,在所有可通行的位置.上,地牢出口如果被设置在右下角,牛牛想离开需要移动的次数最多,为3次。
示例1

输入

复制

  1. 3 3
  2. ...
  3. ...
  4. ...
  5. 0 1
  6. 4
  7. 1 0
  8. 0 1
  9. -1 0
  10. 0 -1

输出

复制

  1. 3
  1. #include<iostream>
  2. #include<string.h>
  3. #include<stdio.h>
  4. #define MAXN 0x3f3f3f3f
  5. using namespace std;
  6.  
  7. char chess[][]; //map
  8. int dir[][]; //步长
  9. bool vis[][]; //访问过
  10. int cost[][]; //最小距离
  11. int n, m, k;
  12.  
  13. //求x、y到其他.的最小距离
  14. void DFS(int x, int y, int len)
  15. {
  16. //终止条件
  17. if (vis[x][y] && cost[x][y] <= len)
  18. return;
  19. cost[x][y] = len;
  20. vis[x][y] = true;
  21. for (int i = ; i < k; i++)
  22. {
  23. int xx = x + dir[i][];
  24. int yy = y + dir[i][];
  25. if (xx >= && xx < n && yy >= && yy < m && chess[xx][yy] == '.')
  26. {
  27. DFS(xx, yy, len + );
  28. }
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. //初始化地图
  35. cin >> n >> m;
  36. for (int i = ; i < n; i++)
  37. {
  38. for (int j = ; j < m; j++)
  39. {
  40. cin >> chess[i][j];
  41. vis[i][j] = false;
  42. cost[i][j] = MAXN;
  43. }
  44. }
  45. //起点
  46. int x, y;
  47. cin >> x >> y;
  48. vis[x][y] = true;
  49. //输入步长
  50. cin >> k;
  51. for (int i = ; i < k; i++) {
  52. cin >> dir[i][] >> dir[i][];
  53. }
  54. //求出所有最小距离
  55. DFS(x, y, );
  56. int max = ;
  57. for (int i = ; i < n; i++)
  58. {
  59. for (int j = ; j < m; j++)
  60. {
  61. if (chess[i][j] == '.')
  62. {
  63. //不可达
  64. if (cost[i][j] == MAXN)
  65. {
  66. cout << - << endl;
  67. return ;
  68. }
  69. max = max < cost[i][j] ? cost[i][j] : max;
  70. }
  71. }
  72. }
  73. cout << max << endl;
  74. //system("pause");
  75. return ;
  76. }

地牢逃脱----DFS搜索最优解的更多相关文章

  1. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. [ZOJ 1011] NTA (dfs搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...

  3. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  4. hihocoder 1050 树中的最长路(动态规划,dfs搜索)

    hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...

  5. sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)

    Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...

  6. 蓝桥杯 历届试题 剪格子(dfs搜索)

    历届试题 剪格子 时间限制:1.0s   内存限制:256.0MB 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |* || +--****--+ ||* | ** ...

  7. DFS搜索题素数环

    素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...

  8. poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】

    题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...

  9. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

随机推荐

  1. python-memcached模块

    memcache memcache介绍 memcache概念 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库 ...

  2. 显著水平alpha

    http://blog.minitab.com/blog/adventures-in-statistics-2/understanding-hypothesis-tests:-significance ...

  3. Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

    Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...

  4. SpringMVC——<mvc:annotation-driven/>

    会自动注 册RequestMappingHandlerMapping .RequestMappingHandlerAdapter 与 ExceptionHandlerExceptionResolver ...

  5. (字符串)ZigZag Conversion

    [解析] 第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列: 发现所有行的重复周期都是 2 * nRows - 2 对于首行和末 ...

  6. Struts2获取Action中的数据

    当我们用Struts2框架开发时,经常有要获取jsp页面的数据或者在jsp中获取后台传过来的数据(Action),那么怎么去获取自己想要的数据呢? 后台获取前端数据: 在java程序中生成要获取字段的 ...

  7. Win7 WPF程序无法接受外部拖拽

    最近在WPF项目中遇到一个问题.虽然选择了AllowDrop = True,但是还是无法支持从外部拖拽文件到程序,倒是内部拖拽(如从一个列表拖拽到树)和从程序拖拽到外部可以. 解决过程 1.考虑是程序 ...

  8. dubbo 安装部署Windows

    1 安装zookeeper 2 安装dubbo    1 下载源码 https://github.com/alibaba/dubbo 2 编译 mvn clean package install -D ...

  9. pdo 预处理

      <?php /* * pdo 预处理sql */ $dsn = "mysql:dbname=0328;host=localhost"; $username = " ...

  10. HTML中&nbsp; &ensp; &emsp; &thinsp;等6种空白空格