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 ...
随机推荐
- java两数相乘基础算法
下面是别人给我的代码: package com.bootdo; public class Test { public static void main(String[] args) { System. ...
- HDU - 1999 不可摸数,快速求因子和
题意:定义s[m]为m内的因子的和,给定一个n,判断是否有s[m]==n,若没有,则是不可摸数. 思路:首先要打表求出s[m]的值,标记这些出现过的值. 打表求因子和: for(int i=1;i&l ...
- pthread_rwlock_t
一.读写锁 读写锁实际是一种特殊的自旋锁,它把对共享资源的访问者划分成读者和写者,读者只对共享资源进行读访问,写者则需要对共享资源进行写操作. 读操作可以共享,写操作是排他的,可以有多个在读(与 CP ...
- [leetcode] 树(Ⅱ)
All questions are simple level. Construct String from Binary Tree Question[606]:You need to construc ...
- G 树的难题
时间限制 : 10000 MS 空间限制 : 165536 KB 评测说明 : 1s,128m 问题描述 给出一个无根树.树有N个点,边有权值.每个点都有颜色,是黑色.白色.灰色这三种颜色之一,称 ...
- 《Three.js 入门指南》3.1.1 - 基本几何形状 - 平面(PlaneGeometry)
3.1 基本几何形状 平面(PlaneGeometry) 说明: 这里的平面(PlaneGeometry)其实是一个长方形,而不是数学意义上无限大小的平面. 构造函数: THREE.PlaneGeom ...
- 1078 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- HTTP Session例子
HTTP协议是“一次性单向”协议.服务端不能主动连接客户端,只能被动等待并答复客户端请求.客户端连接服务端,发出一个HTTP Request,服务端处理请求,并且返回一个HTTP Response给客 ...