K - Find a way(BFS)

来源:互联网 发布:程序员入职心得体会 编辑:程序博客网 时间:2024/05/01 20:37

我一开始想错了思路,循环调用BFS结果导致各种TLE(超时)/(ㄒoㄒ)/~~,后来发现问题我给想复杂了,就把代码全删了,按新思路重新写了一遍,一遍就AC了。

解题思路;

题目中说,KFC的个数和位置是不确定的,所以我们在BFS中无需跳出,只要不是'#’的位置,把图中所有的位置与起点的位置的距离都记录下来,我们在读入图的时候顺便把Y,M的位置记录下来,利用数组和int 变量 num 把KFC的位置和个数记录下来,这样在调用两次BFS后,把Y,M与图中记录的距离,分别保存在d1,与d2中,再利用之前记录KFC位置的数组把Y,M各个对应KFC的位置找出来相加,找到最小的和便是最短的时间,详看代码。

Description

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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output

668866


#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<list>#include<iostream>#include<map>#include<queue>#include<set>#include<stack>#include<vector>using namespace std;#define MAX_N 210const int INF = 0x3f3f3f3f;struct node{    int x, y;    node(int x, int y):x(x), y(y){}};int mov[4][2] ={    0, 1,    0, -1,    1, 0,    -1, 0};int n, m;int x1, y1, x2, y2;int kfcx[MAX_N << 1];int kfcy[MAX_N << 1];char maze[MAX_N][MAX_N];int d1[MAX_N][MAX_N];int d2[MAX_N][MAX_N];bool vis[MAX_N][MAX_N];void bfs(int sx, int sy, int d[MAX_N][MAX_N]){    memset(d, -1, sizeof(d));    memset(vis, false, sizeof(vis));    queue<node> que;    vis[sx][sy] = true;    d[sx][sy] = 0;    que.push(node(sx, sy));    while(!que.empty())    {        node p = que.front();        que.pop();        for(int i = 0 ; i < 4 ; i++)        {            int nx = p.x + mov[i][0];            int ny = p.y + mov[i][1];            if(!vis[nx][ny] && nx >= 0 && ny >= 0 && nx < n && ny < m && maze[nx][ny] != '#')            {                vis[nx][ny] = true;                d[nx][ny] = d[p.x][p.y] + 1;                que.push(node(nx, ny));            }        }    }}int main(){    while(scanf("%d%d", &n, &m) != EOF)    {        getchar();        int num = 0;        for(int i = 0 ; i < n ; i++)        {            for(int j = 0 ; j < m ; j++)            {                scanf("%c", &maze[i][j]);                if(maze[i][j] == 'Y')                {                    x1 = i;                    y1 = j;                }                else if(maze[i][j] == 'M')                {                    x2 = i;                    y2 = j;                }                else if(maze[i][j] == '@')                {                    kfcx[num] = i;                    kfcy[num] = j;                    num++;                }            }            getchar();        }        bfs(x1, y1, d1);        bfs(x2, y2, d2);        int ans = INF;        for(int i = 0 ; i < num ; i++)        {            int x = kfcx[i];            int y = kfcy[i];            ans = min(ans, d1[x][y] + d2[x][y]);        }        cout<<ans * 11<<endl;    }    return 0;}


0 0
原创粉丝点击