地牢逃脱----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 ...
随机推荐
- bootstrap实现菜单定位
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8&quo ...
- Appium移动端自动化测试之应用操作详解(四)
应用操作篇 1.1).安装应用 desired_caps = { 'platformName': 'Android', 'platformVersion': '5.0.0.0', 'deviceNam ...
- 打造一套UI与后台并重.net通用权限管理系统
一.前言 从进行到软件开发这个行业现在已经有几年了,在整理出这个套开发框架之前自己做了不少重复造轮子的事.每次有新的项目总是要耗费不少时间在UI.权限和系统通用模块上面,自己累得要死,老板还骂没效率. ...
- 浅谈delphi创建Windows服务程序与窗体实现交互
我想实现的功能是创建一个服务程序,然后在服务Start时动态创建一个窗体Form,然后把Form缩小时变成TrayIcon放在Windows托盘上. 我在服务程序的OnStart事件中写到 Start ...
- .net Reflection(反射)- 一
Reflection 反射需要引用 using System.Reflection; 命名空间. 通过 Assembly 类的 Load( ); 加载指定的 程序集 Assembly 是不能被实例化 ...
- 声明函数指针、回调函数、函数对象------c++程序设计基础、编程抽象与算法策略
声明函数指针 #include<iostream> using namespace std; double a(double aa) { return aa; } int main() { ...
- 861. Score After Flipping Matrix
We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...
- memcached装、启动和卸载
1.下载相关软件: 下载地址:http://download.csdn.net/download/wangshuxuncom/8249501: 2.解压获取到的压缩文件,将得到一个名为“memcach ...
- 在虚拟机中连接oracle数据库报错ORA-12154,其他服务器连接无问题
在一台服务器上使用sqlplus登录oracle数据库,cmd->sqlplus->name/passwd@orcl2登录某个数据库用户,提示ORA-12154.使用当前服务器的PLSQL ...
- 微信小程序HTTPS - cenos apache 下安装SSL证书
1.yum install mod_ssl 2.接下来,我们需要创建一个新目录,我们将存储服务器密钥和证书 mkdir /root/ssl 3.vi /etc/httpd/conf.d/ssl.con ...