1. 程式人生 > >Labyrinth (樹的直徑和dfs)

Labyrinth (樹的直徑和dfs)

The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. The ACM have found that two of the hooks must be connected by a rope that runs through the hooks in every block on the path between the connected ones. When the rope is fastened, a secret door opens. The problem is that we do not know which hooks to connect. That means also that the neccessary length of the rope is unknown. Your task is to determine the maximum length of the rope we could need for a given labyrinth.

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers C and R (3 <= C,R <= 1000) indicating the number of columns and rows. Then exactly R lines follow, each containing C characters. These characters specify the labyrinth. Each of them is either a hash mark (#) or a period (.). Hash marks represent rocks, periods are free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks are blocks sharing a common side. We cannot walk diagonally and we cannot step out of the labyrinth. 
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

Your program must print exactly one line of output for each test case. The line must contain the sentence "Maximum rope length is X." where Xis the length of the longest path between any two free blocks, measured in blocks.

Sample Input

2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######

Sample Output

Maximum rope length is 0.
Maximum rope length is 8.

Hint

Huge input, scanf is recommended. 
If you use recursion, maybe stack overflow. and now C++/c 's stack size is larger than G++/gcc

題意就是能走到最大距離

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int sx,sy,maxn;
char a[1006][1006];		int n,m;
int d[4][2]={1,0,0,1,-1,0,0,-1};
int vis[1006][1006];
void dfs(int x,int y,int num)
{
	if(num>maxn)
	{
		sx=x;sy=y;maxn=num;
	}
	for(int i=0;i<4;i++)
	{
		int dx=x+d[i][0],dy=y+d[i][1];
		if(dx>=0&&dy>=0&&dx<m&&dy<n&&a[dx][dy]=='.'&&vis[dx][dy]==0)
		{
			vis[dx][dy]=1;
			dfs(dx,dy,num+1);
			vis[dx][dy]=0;
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
sx=-1,sy=-1;maxn=0;
		memset(vis,0,sizeof(vis));
		scanf("%d %d",&n,&m);
		for(int i=0;i<m;i++)
		scanf("%s",a[i]);
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(a[i][j]=='.')
				{
					dfs(i,j,0);break;
				}
			}
			if(sx!=-1) break;
		}
		dfs(sx,sy,0);
		printf("Maximum rope length is %d.\n",maxn);
	}
	return 0;
}

相關推薦

Labyrinth 直徑dfs

The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of the

poj 1655 Balancing Act的重心+dfs

poj 1655 Balancing Act(樹的重心+dfs) Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1…N. Deleting any node from the tree yields a

poj 1383 Labyrinth直徑詳解,簡單明瞭!

Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4031 Accepted: 1516 Description The northern part of the Pyram

BZOJ4888 Tjoi2017異或狀數組

ons ont \n name include bit online ger ++   化為前綴和相減。考慮每一位的貢獻。則需要快速查詢之前有幾個數和當前數的差在第k位上為1。顯然其與更高位是無關的。於是用BIT維護後k位的數的出現次數,瞎算一算即可。 // luogu-

java 藍橋杯 湊算式 兩種方法 暴力dfs

這個算式中A~I代表1~9的數字,不同的字母代表不同的數字。 比如: 6+8/3+952/714 就是一種解法, 5+3/1+972/486 是另一種解法。 這個算式一共有多少種解法? 注意:你提交應該是個整數,不要填寫任何多餘的內容或說明性文字。 解題: 這個題還是沒什麼好說

每日一題——圖的遍歷BFS DFS

題目描述 從鍵盤接收有向圖的頂點集,弧集,建立有向圖,並完成下列任務: (1)計算結點的出度、入度以及度; (2) 從第一個頂點出發,求一個深度優先遍歷序列; (3) 從第一個頂點頂點出發,求一個廣度優先遍歷序列。 注意:以使用者輸入各個頂點

7-6 列出連通集 25 分bfsdfs

給定一個有N個頂點和E條邊的無向圖,請用DFS和BFS分別列出其所有的連通集。假設頂點從0到N−1編號。進行搜尋時,假設我們總是從編號最小的頂點出發,按編號遞增的順序訪問鄰接點。 輸入格式: 輸入第1行給出2個整數N(0<N≤10)和E,分別是圖的頂點數和邊數。隨後E行,每行給出一條邊

java 藍橋杯 湊算式 兩種方法 暴力dfs

這個算式中A~I代表1~9的數字,不同的字母代表不同的數字。 比如: 6+8/3+952/714 就是一種解法, 5+3/1+972/486 是另一種解法。 這個算式一共有多少種解法? 注意:你提

圖的基本演算法BFSDFS

圖是一種靈活的資料結構,一般作為一種模型用來定義物件之間的關係或聯絡。物件由頂點(V)表示,而物件之間的關係或者關聯則通過圖的邊(E)來表示。 圖可以分為有向圖和無向圖,一般用G=(V,E)來表示圖。經常用鄰接矩陣或者鄰接表來描述一副圖。 在圖的基本演算法中

R語言-決策演算法C4.5CART的實現

決策樹演算法的實現: 一、C4.5演算法的實現 a、需要的包:sampling、party library(sampling) library(party) sampling用於實現資料分層隨機抽樣,構造訓練集和測試集。 party用於實現決策樹演算法 另外,還可以設定隨

瀋陽網路賽 J題 Ka Chang 狀陣列+dfs序+分塊思想

Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, you need to handle QQ operations. The

css八--單位

整數 段落 設置方法 tro rgb 間距 border 距離 lin 顏色值 在網頁中的顏色設置是非常重要,有字體顏色(color)、背景顏色(background-color)、邊框顏色(border)等,設置顏色的方法也有很多種: 1、英文命令顏色 前面幾個小節中經常

Xml一基本語法約束

字符 一行 語言 oca cas 書寫 xsd ati 寫法 XML:eXtensible Markup Language 可擴展標記語言 version="1.0"     * 可擴展:所有的標簽都是自定義的。     * 功能:數據存儲     * 配置文件  

hdu 5975---Aninteresting game狀數組

owb form target pla rst phy ace empty creat 題目鏈接 Problem Description Let’s play a game.We add numbers 1,2...n in increasing or

Java的幾個有用小Util函數日期處理http

content lex .get get sta mmd 第幾天 service ret /** * 依據日期返回當前日期是一年的第幾天 * @param date * @return */ public stat

HDU 1166 敵兵布陣 狀數組

ota get 無奈 程序 數據 memset others 正整數 cas 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 敵兵布陣 Time Limit: 2000/1000 MS (Java/Others

ASP.NET MVC Bundles 用法說明打包javascriptcss

http debug模式 尋找 所有 ida jquery-ui content end eas 本文主要介紹了ASP.NET MVC中的新功能Bundles,利用Bundles可以將javascript和css文件打包壓縮,並且可以區分調試和非調試,在調試時不進行壓縮,以

LA 4329狀數組

tdi string 註意 每一個 方法 long 結果 ios pac 算法競賽入門經典 p197 題目大意: 一條大街上住著n個乒乓球愛好者。常常比賽切磋技術。每一個人都有一個不同的技能值a[i]。每場比賽須要3個人:兩名選手,一名裁判。他們有個奇怪的

python學習筆記第十一節叠代其他

技術分享 img 余數 商品列表 步長 取數 sorted函數 學習 四舍五入 yiled 面相過程編程 造好內容,統一發給下面 上面這個是傻瓜版 內置函數 print(divmod(10,3)) 將10除以3,顯示商和余數 enumerate函數,顯示元素及它

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting狀數組

property void example ++ ger imu sorting 一次 base Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input