简单搜索----N - 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值