Find a way (BFS)

 

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. 

InputThe 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 
OutputFor 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
错误代码  Runtime Error(ACCESS_VIOLATION)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
int m, n, k;
char map[205][205];
bool vis1[205][205],vis2[205][205];
int dis1[205][205],dis2[205][205];
int mo[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
struct point
{
    int x, y;
}Y, M,KFC[200];
void input()
{
    getchar();
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            scanf("%c", &map[i][j]);
            if (map[i][j] == 'Y')
            {
                Y.x = i;
                Y.y = j;
            }
            if (map[i][j] == 'M')
            {
                M.x = i;
                M.y = j;
            }
            if (map[i][j] == '@')
            {
                k++;
                KFC[k].x = i;
                KFC[k].y = j;
            }
        }
        getchar();
    }
}
void Ybfs()
{
    queue<point> poi;
    poi.push(Y);
    vis1[Y.x][Y.y] = 1; 
    while (!poi.empty())
    {
        point t;
        for (int j = 0; j <= 4; j++)
        {
            t.x = poi.front().x + mo[j][0];
            t.y = poi.front().y + mo[j][1];
            if (map[t.x][t.y] == '#') continue;
            if (t.x <= 0 || t.x > n || t.y <= 0 || t.y > n) continue;
            if (vis1[t.x][t.y] == 1) continue;
            dis1[t.x][t.y] = dis1[poi.front().x][poi.front().y] + 1;
            vis1[t.x][t.y] = 1;
            poi.push(t);
        }
        poi.pop();
    }
}
void Mbfs()
{
    queue<point> poi;
    poi.push(M);
    vis2[M.x][M.y] = 1;
    while (!poi.empty())
    {
        point t;
        for (int j = 0; j <= 4; j++)
        {
            t.x = poi.front().x + mo[j][0];
            t.y = poi.front().y + mo[j][1];
            if (map[t.x][t.y] == '#') continue;
            if (t.x <= 0 || t.x > n || t.y <= 0 || t.y > n) continue;
            if (vis2[t.x][t.y] == 1) continue;
            dis2[t.x][t.y] = dis2[poi.front().x][poi.front().y] + 1;
            vis2[t.x][t.y] = 1;
            poi.push(t);
        }
        poi.pop();
    }
}
int main()
{
    while (cin >> n >> m)
    {
        int ans;
        int s;
        priority_queue<int, vector<int>, greater<int> >step;
        input();
        memset(vis1, 0, sizeof(vis1));
        memset(dis1, 0, sizeof(dis1));
        memset(vis2, 0, sizeof(vis2));
        memset(dis2, 0, sizeof(dis2));
        Ybfs();
        Mbfs();
        for (int i = 1; i <= k; i++)
        {
            ans = dis1[KFC[i].x][KFC[i].y] + dis2[KFC[i].x][KFC[i].y];
            step.push(ans);
        }
        while (1)
        {
            if (step.top() == 0)
                step.pop();
            if (step.top() != 0)
            {
                s = step.top() * 11;
                cout << s << endl;
                break;
            }
        }
    }
}
AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
char map[202][202];//地图  
int a[202][202];//保存第一个人Y到各个节点(坐标)的最小步数,有的节点不用访问,下面有说明  
int b[202][202];//保存第二个人M   
int dx[4] = { 0,0,-1,1 };
int dy[4] = { 1,-1,0,0 };//方向  
int n, m;//地图大小  
int kcount;//KFC一共的个数  
int yx, yy, mx, my;//Y的坐标,M的坐标  
int flag;//用来判断执行第一个人的BFS还是第二个人的BFS  

void getmap()//输入地图  
{
    for (int i = 0; i<n; i++)
        for (int j = 0; j<m; j++)
        {
            cin >> map[i][j];
            if (map[i][j] == '@')
                kcount++;
            else if (map[i][j] == 'Y')
            {
                yx = i;
                yy = j;
            }
            else if (map[i][j] == 'M')
            {
                mx = i;
                my = j;
            }
        }
}

struct node
{
    int x, y;
};

void bfs(int x, int y)
{
    int k = 0;//搜索过程中的KFC的个数,初始为0  
    queue<node>q;
    node aa, bb;
    aa.x = x;
    aa.y = y;
    if (!flag)//flag=0的时候执行Y的bfs  
        a[x][y] = 0;
    else
        b[x][y] = 0;//flag=1的时候执行M的bfs  
    q.push(aa);
    while (!q.empty())
    {
        bb = q.front();
        q.pop();
        if (k == kcount)
            break;//退出条件 ,当搜索过程中访问过的KFC数量等于整个地图总的KFC数量时就可以退出,剩下的节点不需要再访问  
        for (int i = 0; i<4; i++)
        {
            aa.x = bb.x + dx[i];
            aa.y = bb.y + dy[i];
            if (!flag)//Y的bfs  
            {
                if (aa.x >= 0 && aa.x<n&&aa.y >= 0 && aa.y<m&&a[aa.x][aa.y] == 0 && map[aa.x][bb.y] != '#')
                {
                    if (map[aa.x][aa.y] == '@')//搜索过程中遇到KFC,数量加1  
                        k++;
                    a[aa.x][aa.y] = a[bb.x][bb.y] + 1;
                    q.push(aa);
                }
            }
            else//M的bfs  
            {
                if (aa.x >= 0 && aa.x<n&&aa.y >= 0 && aa.y<m&&b[aa.x][aa.y] == 0 && map[aa.x][bb.y] != '#')
                {
                    if (map[aa.x][aa.y] == '@')
                        k++;
                    b[aa.x][aa.y] = b[bb.x][bb.y] + 1;
                    q.push(aa);
                }
            }
        }//for  
    }//while  
}
int main()
{
    while (cin >> n >> m)
    {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        kcount = 0;
        flag = 0;
        getmap();
        bfs(yx, yy);
        flag = 1;
        bfs(mx, my);
        int minn = 1000000;
        for (int i = 0; i<n; i++)
            for (int j = 0; j<m; j++)
            {
                if (map[i][j] == '@')
                {
                    minn = min(minn, a[i][j] + b[i][j]);//某个KFC地点两者到达的总步数之和,取最小值。  
                }
            }
        cout << minn * 11 << endl;
    }
    return 0;
}

 

posted @ 2017-07-26 17:30  Edee  阅读(205)  评论(0编辑  收藏  举报