1. 程式人生 > >Find a way

Find a way

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

有時候搜尋寫不好跟處理方法有很大的關係,我把vis寫迴圈外面就wa了,寫裡邊就a了 注意: 1>KFC當成路來走,Y,F也是 2>用ans陣列記錄Y,M是否都能到達KFC,如果把vis寫外面,那麼遇到@不標記,其他點還可以到達同樣的@點地方,這樣使得ans值就不準確了,所以應該訪問過的點直接標為true。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 205;

#define fi first
#define se second

char s[N][N];
int n,m;
typedef struct Node{
    int x,y;
    int num;
}Node;
bool vis[N][N];
int col[5] = {0,0,-1,1};
int con[5] = {-1,1,0,0};
map<pair<int,int>,int >mp;
int ans[N][N];

bool judge(int i,int j)
{
    if(i < 0 || j < 0)
        return false;
    if(i >= n || j >= m)
        return false;
    return true;
}

void bfs(int p,int q)
{
    memset(vis,false,sizeof(vis));
    queue<Node>que;
    que.push((Node){p,q,0});
    while(!que.empty())
    {
        Node tmp = que.front();
        que.pop();
        int x = tmp.x,y = tmp.y;
        //我把vis寫這裡做判斷就不行,因為採用個數記錄是否Y,M都能到達KFC
        for(int i = 0;i < 4;++i)
        {
            int x1 = x + col[i],y1 = y + con[i];
            if(judge(x1,y1) && !vis[x1][y1]){
                vis[x1][y1] = true;
                if(s[x1][y1] == '@' ){
                    mp[make_pair(x1,y1)] = tmp.num + 1 + mp[make_pair(x1,y1)];
                    ans[x1][y1]++;
                    que.push((Node){x1,y1,tmp.num + 1});
                }
                if(s[x1][y1] != '#' && s[x1][y1] != '@'){
                    que.push((Node){x1,y1,tmp.num + 1});
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        mp.clear();
        for(int i = 0;i < n;++i)
            scanf("%s",s[i]);
        memset(ans,0,sizeof(ans));
        for(int i = 0;i < n;++i)
        {
            for(int j = 0;j < m;++j)
            {
                if(s[i][j] == 'Y'){
                    bfs(i,j);
                }
                if(s[i][j] == 'M'){
                    bfs(i,j);
                }
            }
        }
        int MIN = inf;
        for(map<pair<int,int>,int>::iterator it = mp.begin();it != mp.end();++it)
        {
            //cout << (it -> fi).fi << " " << (it -> fi).se << " " << it -> se << endl;
            int x = (it -> fi).fi,y = (it -> fi).se;
            if(ans[x][y] == 2)
                MIN = min(MIN,it -> se);
        }
        printf("%d\n",11 * MIN);
    }
    return 0;
}