码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2612 Find a way

时间:2020-07-17 01:14:23      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:cin   tip   space   size   enter   and   stack   center   stream   

 

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

简简单单做两遍bfs,把两个起点到所有kfc的距离求出来,然后记下来,最后找一下最近的kfc

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
#define INF 999999
#define MAXN 1005

int m, n;
int mark[MAXN][MAXN][2];
int book[MAXN][MAXN];
char map[MAXN][MAXN];
int fx[4][2] = {
    {0,1},{0,-1},{1,0},{-1,0}
};


struct Node {
    int x;
    int y;
    int step;
};

bool check(int x, int y) {
    if (x >= 0 && x < m && y >= 0 && y < n && map[x][y] != # && book[x][y] == 0) {
        return true;
    }
    return false;
}

void BFS(int x, int y,int t) {
    memset(book, 0, sizeof(book)); 
    book[x][y] = 1;
    Node start, next;
    start.x = x;
    start.y = y;
    start.step = 0;
    queue<Node> que;
    que.push(start);
    while (!que.empty()) {
        start = que.front();
        que.pop();
        for (int i = 0; i < 4; i++){
            int tx = start.x + fx[i][0];
            int ty = start.y + fx[i][1];
            if (check(tx, ty)){
                book[tx][ty] = 1;
                next.x = tx;
                next.y = ty;
                next.step = start.step + 1;
                if (map[tx][ty] == @) {
                    mark[tx][ty][t] = next.step;
                }
                que.push(next);
            }
        }
    }

}



int main() {
    while (cin >> m >> n) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                mark[i][j][0] = mark[i][j][1] = 999;
            }
        }

        for (int i = 0; i < m; i++) {
            scanf("%s", map[i]);
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (map[i][j] == Y) {
                    BFS(i, j, 0);
                }
                else if (map[i][j] == M){
                    BFS(i, j, 1);
                }
            }
        }
        int mmin = 999;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int p = mark[i][j][0] + mark[i][j][1];
                if (map[i][j] == @ && mmin > p) {
                    mmin = p;
                }
            }
                
        }
            
        printf("%d\n", mmin * 11);
    }



    return 0;
}

 

HDU 2612 Find a way

标签:cin   tip   space   size   enter   and   stack   center   stream   

原文地址:https://www.cnblogs.com/Vetsama/p/13326385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!