POJ1383 两次搜索求最长 FLY

原题链接

poj1383链接

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

Source

Central Europe 1999

应该是比较经典的求最长路径

我其实也有点搞不懂bfs和dfs的用法和区别,可能也是刚入门

DFS求解

#include
#include
#include
using namespace std;

int N,M,u,v,ans;                                 //M是列N是行    u,v记录第一次搜索的最后点   ans记录路径长度
int dir[4][2]={1,0,0,1,-1,0,0,-1};
char mad[1200][1200];                        //输入字符迷宫时需要
bool vis[1200][1200];                         //作为标记数组,标记数组内每个点是否搜索过。因为只与0,1有关,bool相对于int要好一

void dfs(int x,int y,int res){                     
	for(int i=0;i<4;i++){          
		int nx=dir[i][0]+x;
		int ny=dir[i][1]+y;                        //向四周搜索,为什么1
		if(nx>=0&&nx=0&&ny=ans){
		u=x;v=y;ans=res;          //更新数据,ans会变两次,第一次为第一次搜索的长度,第二次为输出结果
	}
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&M,&N);
		for(int i=0;i

1. dir[4][2]=
|1 |0 |
|-1|0 |
| 0|-1|
|0 | 1|
所以会有for(int i=0;i<4;i++){
int nx=dir[i][0]+x;
int ny=dir[i][1]+y;
2.关于二维(字符)数组的输入问题
3.dfs

POJ1383 两次搜索求最长 FLY_第1张图片

BFS求解(比较复杂,但也是一个模板)

#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f                     //解释见 1
const int MAX=1e9+7;
char mapp[1010][1010];               
int vist[1010][1010];
int dist[4][2]={1,0,-1,0,0,1,0,-1};

struct node
{
    int x,y;
    int step;
};                                               //结构体,祥见 2
int t,c,r;
bool check(int x,int y)
{
    if(x<0||x>=c||y<0||y>=r||vist[x][y]) return 0;
    return 1;
}                                                 //判定
int strx,stry,endx,endy,ans;                  //第一次搜索开始点,  结束点(第二次开始搜索点) ,路径

void bfs(int x,int y)
{
    memset(vist,0,sizeof vist);
    queue q;
    node head,next;
    head.x=x,head.y=y,head.step=0;
    q.push(head);
    vist[head.x][head.y]=1;
    ans=0;                                            //准备
    while(!q.empty())
    {
        head=q.front();
        q.pop();                                        //出队
        if(head.step>ans)
        {
            ans=head.step;
            endx=head.x;
            endy=head.y;
        }                                        //更新点的信息
        for(int i=0;i<4;i++)
        {
            next.x=head.x+dist[i][0];
            next.y=head.y+dist[i][1];
            if(check(next.x,next.y)&&mapp[next.x][next.y]=='.')
            {
                next.step=head.step+1;
                vist[next.x][next.y]=1;
                q.push(next);
            }                                                        // 搜素
        }
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&r,&c);
        for(int i=0;i

1.为何程序员喜欢将INF设置为0x3f3f3f3f?
2.结构体(声明、初始化、内存对齐、如何传参)
3.bfs
POJ1383 两次搜索求最长 FLY_第2张图片

有很多借鉴的,也有原创的,归于原创吧,不然不知咋办

就到这吧.嘿嘿?
原码 ,这样简洁

#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int MAX=1e9+7;
char mapp[1010][1010];
int vist[1010][1010];
int dist[4][2]={1,0,-1,0,0,1,0,-1};

struct node
{
    int x,y;
    int step;
};
int t,c,r;
bool check(int x,int y)
{
    if(x<0||x>=c||y<0||y>=r||vist[x][y]) return 0;
    return 1;
}
int strx,stry,endx,endy,ans;
void bfs(int x,int y)
{
    memset(vist,0,sizeof vist);
    queue q;
    node head,next;
    head.x=x,head.y=y,head.step=0;
    q.push(head);
    vist[head.x][head.y]=1;
    ans=0;
    while(!q.empty())
    {
        head=q.front();
        q.pop();
        if(head.step>ans)
        {
            ans=head.step;
            endx=head.x;
            endy=head.y;
        }
        for(int i=0;i<4;i++)
        {
            next.x=head.x+dist[i][0];
            next.y=head.y+dist[i][1];
            if(check(next.x,next.y)&&mapp[next.x][next.y]=='.')
            {
                next.step=head.step+1;
                vist[next.x][next.y]=1;
                q.push(next);
            }
        }
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&r,&c);
        for(int i=0;i
#include
#include
#include

using namespace std;
int N,M,u,v,ans;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
char mad[1200][1200];
bool vis[1200][1200];
void dfs(int x,int y,int res){
	for(int i=0;i<4;i++){
		int nx=dir[i][0]+x;
		int ny=dir[i][1]+y;
		if(nx>=0&&nx=0&&ny=ans){
		u=x;v=y;ans=res;
	}
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&M,&N);
		for(int i=0;i

怎末说呢,两个差不多,差的就是些写法,bfs中看着长,其实有一些多余了,可以再简洁些,但是也可以多了解些写法。

你可能感兴趣的:(算法题目)