Find a way (HDU 2612)

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4
Y.#@

.#…
@…M
4 4
Y.#@

.#…
@#.M
5 5
Y…@.
.#…
.#…
@…M.
#…#
Sample Output
66
88
66

思路
1.一開始使用從‘@‘跑到Y,M的方法,遍歷每一個’@‘,即每一個’@‘都要跑一邊bfs,然後就超時了
2.然後換成Y,M跑到’@‘,並分別記錄下Y,M跑到每一個’@‘的時間,遍歷’@'求出最小的和即可,這樣只跑2遍

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;

int n,m;
char s[205][205];
int ans[3][205][205];
int X[4]={1,-1,0,0};
int Y[4]={0,0,1,-1};
bool visit[205][205];
struct node
{
    int x,y,step;
    node()
    {
        step=0;
    }
    node(int a,int b,int d)
    {
        x=a,y=b,step=d;
    }
}S,e[5],temp;


bool check(int x,int y)
{
    if(s[x][y]=='#'||x<1||x>n) return false;
    if(y>m||y<1||visit[x][y]) return false;
    return true;
}

void solve(int p)
{
    visit[e[p].x][e[p].y]=true;
    ans[p][e[p].x][e[p].y]=0;
    queue<node> que;
    que.push(e[p]);
    while(!que.empty())
    {
        temp=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int nx=temp.x+X[i];
            int ny=temp.y+Y[i];
            if(check(nx,ny))
            {
                visit[nx][ny]=true;
                que.push({nx,ny,temp.step+1});
                if(s[nx][ny]=='@') ans[p][nx][ny]=temp.step+1;
            }
        }
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cout.tie(0);
    while(cin>>n>>m)
    {
        memset(s,0,sizeof(s));
        memset(visit,0,sizeof(visit));
        memset(ans,0,sizeof(ans));
        int cnt=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>s[i][j];
                if(s[i][j]=='Y') e[1].x=i,e[1].y=j;
                if(s[i][j]=='M') e[2].x=i,e[2].y=j;
            }
        }
        int Ans=1000000;
        solve(1);
        memset(visit,0,sizeof(visit));
        solve(2);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='@'&&ans[1][i][j]&&ans[2][i][j])
                    Ans=min(Ans,ans[1][i][j]+ans[2][i][j]);
            }
        }
        cout<<Ans*11<<'\n';
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章