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<iostream>
#include<cstring>
#include<cstdio>
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<N&&ny>=0&&ny<M&&vis[nx][ny]==0&&mad[nx][ny]=='.'){       //判断是否符合条件,符合标记为1,再次搜索下一个符合的
			vis[nx][ny]=1;                                                                                               
			dfs(nx,ny,res+1);              //每搜索到一个点加一
		}
	}
	if(res>=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<N;i++){
			scanf("%s",&mad[i]);
		}                                                //按行输入迷宫,只要是按行输入结尾%s, 也可以一个个输,但比较麻烦详见2
		memset(vis,0,sizeof(vis));
		ans=0;
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				if(mad[i][j]=='.'){
					dfs(i,j,0);
				}
			}
		}
		memset(vis,0,sizeof(vis));                       //重新初始化,再次dfs
		dfs(u,v,0);
		cout<<"Maximum rope length is "<<ans<<'.'<<endl;
	}
	return 0;
}

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

在这里插入图片描述

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>

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<node> 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<c;i++) scanf("%s",mapp[i]);
        for(int i=0;i<c;i++)
        {
            for(int j=0;j<r;j++)
            {
                if(mapp[i][j]=='.') {
                    strx=i,stry=j;
                    break;
                }
            }
        }
        bfs(strx,stry);
        bfs(endx,endy);
        printf("Maximum rope length is %d.\n",ans);

    }
    return 0;
}

1.为何程序员喜欢将INF设置为0x3f3f3f3f?
2.结构体(声明、初始化、内存对齐、如何传参)
3.bfs
在这里插入图片描述

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

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>

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<node> 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<c;i++) scanf("%s",mapp[i]);
        for(int i=0;i<c;i++)
        {
            for(int j=0;j<r;j++)
            {
                if(mapp[i][j]=='.') {
                    strx=i,stry=j;
                    break;
                }
            }
        }
        bfs(strx,stry);
        bfs(endx,endy);
        printf("Maximum rope length is %d.\n",ans);

    }
    return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>

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<N&&ny>=0&&ny<M&&vis[nx][ny]==0&&mad[nx][ny]=='.'){
			vis[nx][ny]=1;
			dfs(nx,ny,res+1);
		}
	}
	if(res>=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<N;i++){
			scanf("%s",&mad[i]);
		}
		memset(vis,0,sizeof(vis));
		ans=0;
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				if(mad[i][j]=='.'){
					dfs(i,j,0);
				}
			}
		}
		memset(vis,0,sizeof(vis));
		dfs(u,v,0);
		cout<<"Maximum rope length is "<<ans<<'.'<<endl;
	}
	return 0;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值