poj 3083 Children of the Candy Corn

http://poj.org/problem?id=3083

题目不难 就是繁琐呀

代码:

#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

#define sint short int

const int INF=0x3f3f3f3f;

//priority_queue<int,vector<int>,greater<int> >qt;

const int N=50;

char graph[N][N];

typedef pair<int,int>point;

queue<point>qt;

int dist[N][N];

bool in[N][N];

int X[]={-1,0,1,0};

int Y[]={0,1,0,-1};

int n,m;

bool OK(int x,int y)

{

    if(x>=0&&x<n&&y>=0&&y<m&&graph[x][y]!='#')

    return true;

    return false;

}

int spfa(int sx,int sy,int ex,int ey)

{

    memset(in,false,sizeof(in));

    memset(dist,-1,sizeof(dist));

    qt.push(point(sx,sy));

    in[sx][sy]=true;

    dist[sx][sy]=1;

    while(!qt.empty())

    {

        int x=qt.front().first;

        int y=qt.front().second;

        in[x][y]=false;

        qt.pop();

        for(int i=0;i<4;++i)

        {

            int l1=x+X[i];

            int l2=y+Y[i];

            if(OK(l1,l2)&&(dist[l1][l2]==-1||dist[l1][l2]>dist[x][y]+1))

            {

               dist[l1][l2]=dist[x][y]+1;

               if(!in[l1][l2])

               {

                   in[l1][l2]=true;

                   qt.push(point(l1,l2));

               }

            }

        }

    }

    return dist[ex][ey];

}

int Lsearch(int sx,int sy,int ex,int ey,int t)

{

    int step=1;

    int kx=sx,ky=sy;

    while(true)

    {

        if(kx==ex&&ky==ey)

        break;

        bool flag=false;

        for(int i=(t+3)%4,j=0;j<4;++j,i++)

        {

            int l=(i+4)%4;

            int l1=kx+X[l];

            int l2=ky+Y[l];

            if(OK(l1,l2))

            {kx=l1;ky=l2;t=l;++step;flag=true;break;}

        }

        if(flag==false)

        return INF;

    }

    return step;

}

int Rsearch(int sx,int sy,int ex,int ey,int t)

{//cout<<kx<<" "<<ky<<" "<<t<<endl;

    int step=1;

    int kx=sx,ky=sy;

    while(true)

    {//cout<<kx<<" "<<ky<<" "<<t<<endl;

        if(kx==ex&&ky==ey)

        break;

        bool flag=false;

        for(int i=(t+1)%4,j=0;j<4;++j,i--)

        {

            int l=(i+4)%4;

            int l1=kx+X[l];

            int l2=ky+Y[l];

            if(OK(l1,l2))

            {kx=l1;ky=l2;t=l;++step;flag=true;break;}

        }

        if(flag==false)

        return INF;

    }

    return step;

}

int main()

{

    //freopen("data.in","r",stdin);

    int T;

    scanf("%d",&T);

    while(T--)

    {

        int sx,sy,ex,ey;

        scanf("%d %d",&m,&n);

        getchar();

        for(int i=0;i<n;++i)

        gets(graph[i]);

        for(int i=0;i<n;++i)

        for(int j=0;j<m;++j)

        if(graph[i][j]=='S')

        {sx=i;sy=j;}

        else if(graph[i][j]=='E')

        {ex=i;ey=j;}

        int ans1=INF,ans2=INF,ans3;

        //cout<<sx<<" "<<sy<<" "<<ex<<" "<<ey<<endl;

        for(int i=0;i<4;++i)

        {

            ans1=min(ans1,Lsearch(sx,sy,ex,ey,i));

            ans2=min(ans2,Rsearch(sx,sy,ex,ey,i));

        }

        ans3=spfa(sx,sy,ex,ey);

        printf("%d %d %d\n",ans1,ans2,ans3);

    }

    return 0;

}

  

你可能感兴趣的:(children)