I - 树的直径 POJ - 1383
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
AC代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int c,r,dx,dy;
int ans;
char a[1005][1005];
int b[1005][1005];
int mx[4] = {0,0,1,-1};
int my[4] = {1,-1,0,0};
void dfs(int x,int y,int t)
{
b[x][y]=1;
if(ans<t)
{
dx=x;
dy=y;
ans=t;
}
int xx,yy,i;
for(i=0;i<4;i++)
{
xx=x+mx[i];
yy=y+my[i];
if(xx>-1&&xx<r&&yy>-1&&yy<c&&!b[xx][yy]&&a[xx][yy]!='#')
dfs(xx,yy,t+1);
}
b[x][y]=1;
}
int main()
{
int t,i,j,sx,sy;
scanf("%d",&t);
while(t--)
{
int l=0;
ans=0;
memset(b,0,sizeof(b));
scanf("%d %d",&c,&r);
for(i=0;i<r;i++)
{
scanf("%s",a[i]);
for(j=0;j<c&&l==0;j++)
if(a[i][j]=='.')
{
l=1;
sx=i;
sy=j;
}
}
dfs(sx,sy,0);
memset(b,0,sizeof(b));
dfs(dx,dy,0);
printf("Maximum rope length is %d.\n",ans);
}
}
我的代码:
1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5
6 using namespace std;
7
8 int r, c;
9 char a[1005][1005];
10 int num[1005][1005];
11 int n_step, max_step, nx, ny;
12 int dx[4] = {0, 1, 0, -1};
13 int dy[4] = {1, 0, -1, 0};
14
15 int dfs(int x, int y, int step)
16 {
17 n_step = step;
18 a[x][y] = '#';
19 for(int i = -1; i <= 1; i++)
20 {
21
22 max_step = max(max_step, n_step);
23 nx = x + dx[i];
24 ny = y + dy[i];
25 if(nx > 0 && nx <= r && ny > 0 && ny <= c && a[nx][ny] == '.')
26 {
27 n_step++;
28 dfs(nx, ny, n_step);
29 }
30 }
31 return max_step;
32 }
33
34 int main()
35 {
36 int t;
37
38 scanf("%d", &t);
39 while(t--)
40 {
41 n_step = 0;
42 max_step = 0;
43 scanf("%d %d", &r, &c);
44 getchar();
45 memset(num, 0, sizeof(num));
46 for(int i = 0; i < max(r, c) + 1; i++)
47 {
48 a[0][i] = '#';
49 a[i][0] = '#';
50 a[r+1][i] = '#';
51 a[i][c+1] = '#';
52 }
53
54 //cout << r << c <<endl;
55 for(int i = 1; i <= r; i++)
56 {
57 for(int j = 1; j <= c; j++)
58 {
59 scanf("%c", &a[i][j]);
60 }
61 getchar();
62 }
63
64 for(int i = 1; i <= r; i++)
65 {
66 for(int j = 1; j <= c; j++)
67 {
68 if(a[i][j] == '.')
69 {
70 //cout << "++" << i << j << endl;
71 int ans = dfs(i, j, 0);
72 printf("%d\n", ans);
73 break;
74 }
75 }
76 }
77
78
79 }
80 return 0;
81 }
I - 树的直径 POJ - 1383的更多相关文章
- 树的直径 poj 2631
树的直径:从随意一点出发,BFS找到最远的距离,然后在从该点出发BFS找到最远的距离 #include <iostream> #include <algorithm> #inc ...
- poj 1383 Labyrinth【迷宫bfs+树的直径】
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
- POJ 1383 Labyrinth (bfs 树的直径)
Labyrinth 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/E Description The northern part ...
- POJ 1383题解(树的直径)(BFS)
题面 Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4997 Accepted: 1861 Descript ...
- 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花
求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
随机推荐
- MySQL 事务的隔离级别
转载:https://developer.aliyun.com/article/743691?accounttraceid=80d4fddb3dc64b97a71118659e106221tozz 简 ...
- 如何用css写一个带斜切角、有边框又有内外阴影的按钮呢?
如果有一天,UI设计师丢过来一张UI稿,上面有这样一个带有斜切角.有边框还有内外阴影的按钮,你会怎么实现呢?第一反应切图?可是按钮内容.大小都是可变的,那得切多少图啊~Canvas?SVG?No,no ...
- 心脏滴血(CVE-2014-0160)检测与防御
用Nmap检测 nmap -sV --script=ssl-heartbleed [your ip] -p 443 有心脏滴血漏洞的报告: ➜ ~ nmap -sV --script=ssl-hear ...
- windows本地连接虚拟机上的ubuntu的redis,以及无法连接解决方法(redisDesktopManager Jedis详细步骤)
一.环境 1.ubuntu20.04 . redis 5.0.7 在ubuntu上下载redis,执行命令 sudo apt install redis 2.redisDesktopManager下载 ...
- Hi3559AV100外接UVC/MJPEG相机实时采图设计(一):Linux USB摄像头驱动分析
下面将给出Hi3559AV100外接UVC/MJPEG相机实时采图设计的整体流程,主要实现是通过V4L2接口将UVC/MJPEG相机采集的数据送入至MPP平台,经过VDEC.VPSS.VO最后通过HD ...
- 我的2019年总结和一些2020年的flag
我的2019年总结和一些2020年的flag 前言 2019年在我的人生中注定是里程碑的一年,主要是我毕业了.本篇总结主要聊一些2019年经历的事以及对于自己2020年的期待 1. 再见2019 1. ...
- WPF 基础 - 绘画 1) 线段、矩形、圆弧及填充色
1. 绘画 1.1 图形类型 Line X1.Y1.X2.Y2,Stroke,StrokeThickness Rectangle 矩形 Ellipse 椭圆 Polygon 多边形(自动闭合) Pol ...
- HYSBZ 1734 二分
传送门 题面: 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1 ...
- python-实现顺序栈
7 class sqstack(object): 8 def __init__(self,maxsize): 9 self.maxsize = maxsize 10 self.stackElem = ...
- python程序控制结构
一:顺序结构 顺序结构的程序是指程序中的所有语句都是按照书写顺序逐一执行的,但是顺序结构的程序功能有限. 二:选择结构 选择结构也称为分支结构,用于处理在程序中出现两条或更多执行路径可供选择的情况.选 ...