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;

}

 

 

你可能感兴趣的:(int)