HDU2612:Find a way

Posted 0一叶0知秋0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2612:Find a way相关的知识,希望对你有一定的参考价值。

HDU2612:Find a way

kuangbin专题一:N题

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17428    Accepted Submission(s): 5605


Problem 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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 [email protected] .#... .#... @..M. #...#
 

 

Sample Output
66 88 66
 题意:两个人碰面吃饭 , 每走一步用时11min 问两人耗费总时间最小是多少
思路:以两个人的位置为起点 , 两次广度优先搜索 , 由于存在多个饭馆 遇到一个饭馆就 存一下费用(耗时) ,
取 两人到同一位置的最小耗时和
/* 不想加注释 ......*/
#include <Cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std ; 

#define maxn 300 
bool visit[maxn][maxn] ; 
int n , m ; 
char map[maxn][maxn] ; 
int result1[maxn][maxn] ; 
int result2[maxn][maxn] ; 
int x1 , y1 , x2 , y2 ; 
int dirx[] = {0 , 0 , -1 , 1 } ; 
int diry[] = {-1 , 1 , 0 , 0 } ; 

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

bool check(int x , int y ){
    if(x>=1 && x<= n && y>=1 && y<=m){
        return true ; 
    }
    return false ; 
}

void BFS(int x , int y , int result[][maxn]){
    memset(visit , 0 , sizeof(visit)) ; 
    
    queue<node> Q ;  
    node st ; 
    st.x = x ; 
    st.y = y ; 
    st.step = 0 ; 
    Q.push(st) ; 
    while(!Q.empty()){
        node q = Q.front() ; 
        Q.pop() ; 
        if(map[q.x][q.y] == @){
            result[q.x][q.y] = q.step ; 
        }
        node turn ; 
        for(int i=0 ; i<4 ; i++){
            turn.x = q.x + dirx[i] ; 
            turn.y = q.y + diry[i] ; 
            turn.step = q.step + 1 ; 
            if(check(turn.x , turn.y ) && !visit[turn.x][turn.y] && map[turn.x][turn.y]!=# ) {
                visit[turn.x][turn.y] = 1 ;
                Q.push(turn) ; 
            }
        }
        
    }
    return;
}

int main(){
    
    while(~scanf("%d%d" , &n , &m)){
        for(int i=1 ; i<=n ; i++){
            for(int j=1 ; j<=m ; j++){
                scanf(" %c" , &map[i][j]) ; 
                if(map[i][j] == Y){
                    x1 = i ; 
                    y1 = j ; 
                }
                if(map[i][j] == M){
                    x2 = i ; 
                    y2 = j ; 
                }
            }
        }
        
        memset(result1 , 0 , sizeof(result1)) ; 
        memset(result2 , 0 , sizeof(result2)) ;
        
        BFS(x1 , y1 , result1) ; 
        BFS(x2 , y2 , result2) ;
        
        int min_num = 1000000 ; 
        for(int i=1 ; i<=n ; i++){
            for(int j=1 ; j<= m ; j++){
                if(result1[i][j] == 0 || result2[i][j] == 0 || map[i][j] != @)
                    continue ; 
                    
                min_num = min(result1[i][j] + result2[i][j] , min_num ) ; 
            }
        } 
        printf("%d\n" , min_num * 11 ) ; 
    } 
    return 0 ; 
}

 

以上是关于HDU2612:Find a way的主要内容,如果未能解决你的问题,请参考以下文章

HDU-2612-Find a way

hdu2612 Find a way

HDU 2612 Find a way

HDU 2612 - Find a way - [BFS]

HDU2612:Find a way

HDU2612 find a way