Labyrinth 树的直径加DFS
Input
The labyrinth is designed in such a way that there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is easy to find the right path connecting them.
Output
Sample Input
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
Sample Output
Maximum rope length is 0.
Maximum rope length is 8.
Hint
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
char arr[][];
int start_i,start_j;
int mark[][];
//int arr1[1500][1500];
int ans=;
int xx,yy;
int d[][]={{,},{,},{,-},{-,}};
void dfs(int x,int y,int step){
if(step>ans){
ans=step;//寻找距离x,y最远的点并记录下来
xx=x;
yy=y;
} for(int i=;i<;i++)
{
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&arr[dx][dy]=='.'&&mark[dx][dy]==){
mark[dx][dy]=;
dfs(dx,dy,step+);
mark[dx][dy]=;//回溯
}
}
}
int main()
{
int t;
cin>>t;
while(t--){ cin>>m>>n;//n行m列
for(int i=;i<n;i++){
scanf("%s",&arr[i]);
}
int j;
for(int i=;i<n;i++)
{
for(j=;j<m;j++){
if(arr[i][j]=='.'){
start_i=i;
start_j=j;
// cout<<i<<"_"<<j<<endl;
break;
}
}
if(j!=m)
break;
}
ans=;
memset(mark,,sizeof(mark));
mark[start_i][start_j]=;
dfs(start_i,start_j,);
memset(mark,,sizeof(mark));
dfs(xx,yy,);
printf("Maximum rope length is %d.\n",ans);
} return ;
}
Labyrinth 树的直径加DFS的更多相关文章
- 大臣的旅费---树的直径(dfs)
很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首都直接或者 ...
- Codeforces 592D - Super M - [树的直径][DFS]
Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...
- POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...
- HDU 4123 Bob’s Race 树的直径+单调队列
题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...
- NOI 2003 逃学的小孩 (树的直径)
[NOI2003 逃学的小孩] 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?"一 ...
- 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)
1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1034 Solved: 562[Submit][St ...
- 历届试题 大臣的旅费-(树的直径+dfs)
问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...
- POJ 1383 Labyrinth (bfs 树的直径)
Labyrinth 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/E Description The northern part ...
- poj 1383 Labyrinth【迷宫bfs+树的直径】
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
随机推荐
- hdu2492树状数组
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2492/ 题目大意:给定一个序列,求长度为三的子序列(a,b,c)使得a<b<c或a>b> ...
- CodeForces 196B Infinite Maze
Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Magenta Demos Magenta 实战代码
Magenta 实战代码 这个仓库包含了很多 Magenta 模型的实现.看 Magenta 库以及模型,看我们的主仓库:https://github.com/tensorflow/magenta A ...
- 区间dp暂时的理解
因为刚刚看了区间dp,所以写一下对区间dp的理解. 例题: 石子归并 51Nod - 1021 看了一篇博客,觉得他说得比较容易理解,所以再次重复一遍: 假如你是上帝,已经知道了1~n堆石子的最优解, ...
- java fork/join简单实践
我们知道,java8中有并行流,而并行流在后台的实现是通过fork/join池来完成的,例如: List<Integer> a = buildList(); List<Integer ...
- ionic中select下拉框点击无反应解决办法
两种解决办法: 1.在select外的div加入属性 data-tap-disabled=”true” 2.找到ionic.bundle.js文件 的下面这个函数,添加如图两行代码
- 使用vue-cli脚手架创建vue项目
使用vue-cli脚手架创建vue项目 首先,你已经安装了node. 使用vue-cli@2 创建项目 执行 命令: npm i -g vue-cli@2 //全局安装vue-cli@2脚手架 : v ...
- .Net 微服务架构技术栈的那些事
一.前言 大家一直都在谈论微服务架构,园子里面也有很多关于微服务的文章,前几天也有一些园子的朋友问我微服务架构的一些技术,我这里就整理了微服务架构的技术栈路线图,这里就分享出来和大家一起探讨学习,同时 ...
- C - Can you solve this equation? HDU - 2199(二分水题)
Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100; ...
- 1642: 【USACO】Payback(还债)
1642: [USACO]Payback(还债) 时间限制: 1 Sec 内存限制: 64 MB 提交: 190 解决: 95 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 &quo ...