地牢逃脱----DFS搜索最优解
输入描述:
- 每个输入包含 1 个测试用例。每个测试用例的第一行包含两个整数 n 和 m(1 <= n, m <= 50),表示地牢的长和宽。接下来的 n 行,每行 m 个字符,描述地牢,地牢将至少包含两个 '.'。接下来的一行,包含两个整数 x
0
- , y
0
- ,表示牛牛的出发位置(0 <= x0 < n, 0 <= y0 < m,左上角的坐标为 (0, 0),出发位置一定是 '.')。之后的一行包含一个整数 k(0 < k <= 50)表示牛牛合法的步长数,接下来的 k 行,每行两个整数 dx, dy 表示每次可选择移动的行和列步长(-50 <= dx, dy <= 50)
输出描述:
- 输出一行一个数字表示最坏情况下需要多少次移动可以离开地牢,如果永远无法离开,输出 -1。以下测试用例中,牛牛可以上下左右移动,在所有可通行的位置.上,地牢出口如果被设置在右下角,牛牛想离开需要移动的次数最多,为3次。
输出
- 3
- #include<iostream>
- #include<string.h>
- #include<stdio.h>
- #define MAXN 0x3f3f3f3f
- using namespace std;
- char chess[][]; //map
- int dir[][]; //步长
- bool vis[][]; //访问过
- int cost[][]; //最小距离
- int n, m, k;
- //求x、y到其他.的最小距离
- void DFS(int x, int y, int len)
- {
- //终止条件
- if (vis[x][y] && cost[x][y] <= len)
- return;
- cost[x][y] = len;
- vis[x][y] = true;
- for (int i = ; i < k; i++)
- {
- int xx = x + dir[i][];
- int yy = y + dir[i][];
- if (xx >= && xx < n && yy >= && yy < m && chess[xx][yy] == '.')
- {
- DFS(xx, yy, len + );
- }
- }
- }
- int main()
- {
- //初始化地图
- cin >> n >> m;
- for (int i = ; i < n; i++)
- {
- for (int j = ; j < m; j++)
- {
- cin >> chess[i][j];
- vis[i][j] = false;
- cost[i][j] = MAXN;
- }
- }
- //起点
- int x, y;
- cin >> x >> y;
- vis[x][y] = true;
- //输入步长
- cin >> k;
- for (int i = ; i < k; i++) {
- cin >> dir[i][] >> dir[i][];
- }
- //求出所有最小距离
- DFS(x, y, );
- int max = ;
- for (int i = ; i < n; i++)
- {
- for (int j = ; j < m; j++)
- {
- if (chess[i][j] == '.')
- {
- //不可达
- if (cost[i][j] == MAXN)
- {
- cout << - << endl;
- return ;
- }
- max = max < cost[i][j] ? cost[i][j] : max;
- }
- }
- }
- cout << max << endl;
- //system("pause");
- return ;
- }
地牢逃脱----DFS搜索最优解的更多相关文章
- hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- [ZOJ 1011] NTA (dfs搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1011 题目大意:在一棵树上,给你起始状态,问你能否到达终止状态. ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- hihocoder 1050 树中的最长路(动态规划,dfs搜索)
hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...
- sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)
Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...
- 蓝桥杯 历届试题 剪格子(dfs搜索)
历届试题 剪格子 时间限制:1.0s 内存限制:256.0MB 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |* || +--****--+ ||* | ** ...
- DFS搜索题素数环
素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
随机推荐
- python-memcached模块
memcache memcache介绍 memcache概念 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库 ...
- 显著水平alpha
http://blog.minitab.com/blog/adventures-in-statistics-2/understanding-hypothesis-tests:-significance ...
- Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...
- SpringMVC——<mvc:annotation-driven/>
会自动注 册RequestMappingHandlerMapping .RequestMappingHandlerAdapter 与 ExceptionHandlerExceptionResolver ...
- (字符串)ZigZag Conversion
[解析] 第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列: 发现所有行的重复周期都是 2 * nRows - 2 对于首行和末 ...
- Struts2获取Action中的数据
当我们用Struts2框架开发时,经常有要获取jsp页面的数据或者在jsp中获取后台传过来的数据(Action),那么怎么去获取自己想要的数据呢? 后台获取前端数据: 在java程序中生成要获取字段的 ...
- Win7 WPF程序无法接受外部拖拽
最近在WPF项目中遇到一个问题.虽然选择了AllowDrop = True,但是还是无法支持从外部拖拽文件到程序,倒是内部拖拽(如从一个列表拖拽到树)和从程序拖拽到外部可以. 解决过程 1.考虑是程序 ...
- dubbo 安装部署Windows
1 安装zookeeper 2 安装dubbo 1 下载源码 https://github.com/alibaba/dubbo 2 编译 mvn clean package install -D ...
- pdo 预处理
<?php /* * pdo 预处理sql */ $dsn = "mysql:dbname=0328;host=localhost"; $username = " ...
- HTML中      等6种空白空格