1145. Rope in the Labyrinth

http://acm.timus.ru/problem.aspx?space=1&num=1145

两次bfs 找树的直径  还有在函数里面不能定义太大的数组 否则执行不了

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include <iomanip>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
const int N=1005;
int X[]={1,-1,0,0};
int Y[]={0,0,1,-1};
char s[N][N];
int dist[N][N];
int ans;
int n,m;
int stx,sty;
void bfs()
{
    memset(dist,-1,sizeof(dist));
    queue<int>qtx;
    queue<int>qty;
    qtx.push(stx);
    qty.push(sty);
    dist[stx][sty]=0;
    while(!qtx.empty())
    {
        int x=qtx.front();qtx.pop();
        int y=qty.front();qty.pop();
        for(int i=0;i<4;++i)
        {
            int l1=x+X[i];
            int l2=y+Y[i];
            if(x>=0&&x<n&&y>=0&&y<m&&s[l1][l2]=='.'&&dist[l1][l2]==-1)
            {
                dist[l1][l2]=dist[x][y]+1;
                if(dist[l1][l2]>ans)
                {
                    ans=dist[l1][l2];
                    stx=l1;
                    sty=l2;
                }
                qtx.push(l1);
                qty.push(l2);
            }
        }
    }
}
int main()
{
    //freopen("data.in","r",stdin);
    cin>>m>>n;
    getchar();
    for(int i=0;i<n;++i)
    gets(s[i]);
    stx=-1;
    sty=-1;
    int I=0;
    for(int i=0;i<n;++i)
    for(int j=0;j<m;++j)
    if(s[i][j]=='.')
    {
        ++I;
        if(stx==-1)
        {stx=i;sty=j;}
    }
    if(I<=1)
    {
        cout<<"0"<<endl;return 0;
    }
    ans=0;//cout<<stx<<" "<<sty<<endl;
    bfs();
    bfs();
    cout<<ans<<endl;
}

 

 

转载于:https://www.cnblogs.com/liulangye/archive/2012/11/21/2780295.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值