zoukankan      html  css  js  c++  java
  • 【POJ】[1383]Labyrinth

    Labyrinth
    Time Limit: 2000MS   Memory Limit: 32768K

    Description

    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



    依照树的直径的思路

    先找出任意一点的最远点u,再找出u的最远点v,u-v距离即使最远距离,这题同样按照这种思路进行搜索

    不过虽然题目说容易栈溢出,但是提交时也没有这种错误情况


    #include<stdio.h>
    #include<string.h>
    int max,tn,tm;
    int W,H;
    int flag[1200][1200];
    char map[1200][1200];
    int tmove[4]= {1,-1,0,0};
    void dfs(int n,int m,int cnt) {
    	if(max<cnt) {
    		max=cnt,tn=n,tm=m;
    	}
    	for(int i=0; i<4; i++) {
    		int moven=n+tmove[i],movem=m+tmove[(i+2)%4];
    		if(moven>=0&&moven<H&&movem>=0&&movem<W&&map[moven][movem]=='.'&&!flag[moven][movem]) {
    			flag[moven][movem]=1;
    			dfs(moven,movem,cnt+1);
    		}
    	}
    }
    int main() {
    	int T;
    	scanf("%d",&T);
    	while(T--) {
    		scanf("%d %d",&W,&H);
    		getchar();
    		for(int i=0; i<H; i++) {
    			for(int j=0; j<W; j++) {
    				scanf("%c",&map[i][j]);
    				if(map[i][j]=='.') {
    					tn=i;
    					tm=j;
    				}
    			}
    			getchar();
    		}
    		max=0;
    		memset(flag,0,sizeof(flag));
    		flag[tn][tm]=1;
    		dfs(tn,tm,0);
    		memset(flag,0,sizeof(flag));
    		flag[tn][tm]=1;
    		dfs(tn,tm,0);
    		printf("Maximum rope length is %d.
    ",max);
    	}
    	return 0;
    }
    

  • 相关阅读:
    How To Change Database Name
    Oracle备份与恢复案例
    Linux 下安装 Oracle9i
    在 Linux x86 上安装 Oracle 数据库 10g_1
    在 Linux x86 上安装 Oracle 数据库 10g_2
    Oracle9i数据库DataGuard实施及维护手册2
    在 Linux x86 上安装 Oracle 数据库 10g_3
    流程企业(钢铁企业)的制造执行系统
    理解和使用Oracle分析工具LogMiner
    Oracle9i数据库Data Guard实施及维护手册 1
  • 原文地址:https://www.cnblogs.com/BoilTask/p/12569438.html
Copyright © 2011-2022 走看看