1643. Attack of the Dark Fortress

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

bfs 就可以 不过要注意细节

思路:

分别求出 ‘!’到每个点的最短路 dist1 ,‘$’到每个点的最短路 dist2  和 ‘*’到每个点的最短路 dist3

然后枚举每个点 的 max(dist1+dist2)+dist3 值 求最小

代码:

#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=105;

char graph[N][N];

typedef pair<int,int>point;

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

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

vector<point>tran[27];

int n,m;

void bfs(int stx,int sty,int dist[N][N])

{

    queue<point>qt;

    qt.push(point(stx,sty));

    dist[stx][sty]=0;

    while(!qt.empty())

    {

        int x=(qt.front()).first;

        int y=(qt.front()).second;

        qt.pop();

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

        {

            int l1=x+X[i];

            int l2=y+Y[i];

            if(l1>=0&&l1<n&&l2>=0&&l2<m&&graph[l1][l2]!='#'&&graph[l1][l2]!='*'&&dist[l1][l2]==-1)

            {

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

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

                if(graph[l1][l2]>='A'&&graph[l1][l2]<='Z')

                {



                    for(unsigned int i=0;i<tran[graph[l1][l2]-'A'].size();++i)

                    {

                        int t1=tran[graph[l1][l2]-'A'][i].first;

                        int t2=tran[graph[l1][l2]-'A'][i].second;;

                        if(dist[t1][t2]==-1)

                        {

                            dist[t1][t2]=dist[l1][l2];

                            qt.push(point(t1,t2));

                        }

                    }

                }

            }

        }

    }/*

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

    {

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

        cout<<dist[i][j]<<" ";

        cout<<endl;

    }cout<<endl;*/

}

int main()

{

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

    int dist1[N][N];

    int dist2[N][N];

    int dist3[N][N];

    while(cin>>n>>m)

    {

        getchar();

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

        gets(graph[i]);

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

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

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

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

        tran[i].clear();

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

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

        if(graph[i][j]>='A'&&graph[i][j]<='Z')

        {tran[graph[i][j]-'A'].push_back(point(i,j));}

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

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

        {

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

            bfs(i,j,dist1);

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

            bfs(i,j,dist2);

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

            bfs(i,j,dist3);

        }

        int ans=INF;

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

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

        {

            if(dist1[i][j]>=0&&dist2[i][j]>=0&&dist3[i][j]>=0)

            {

                ans=min(ans,max(dist1[i][j],dist2[i][j])+dist3[i][j]);

            }

        }

        if(ans==INF)

        cout<<"Impossible"<<endl;

        else

        cout<<ans<<endl;

    }

    return 0;

}

 

你可能感兴趣的:(for)