当前位置:编程学习 > C/C++ >>

[hdu] Find a way (分别从两个点出发BFS,再计算最小值)

Find a way
Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 7   Accepted Submission(s) : 6

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
Y..@.
.#...
.#...
@..M.
#...#
 

Sample Output
66
88
66
 

Author
yifenfei
 

Source
奋斗的年代
 

[cpp] 
// 664.cpp : 定义控制台应用程序的入口点。 
// 
 
#include "stdafx.h" 
 
 
#include<cstdio> 
#include<cstring> 
#include<queue> 
#define INF 0x3f3f3f3f 
#define MAX 210 
#define MEM(arr,w) memset(arr,w,sizeof(arr)); 
using namespace std; 
struct node 

    int x,y; 
}st1,st2,ss,tt; 
int dir[4][2]={0,1,1,0,0,-1,-1,0}; 
int n,m,MAXT; 
char map[MAX][MAX]; 
int visit[MAX][MAX]; 
int step1[MAX][MAX],step2[MAX][MAX]; 
void Init() 

    MEM(visit,false); 
    MAXT=INF; 
    MEM(step1,0); 
    MEM(step2,0); 

void BFS(node st,int step[][210]) 

    queue<node> Q; 
    Q.push(st); 
    visit[st.x][st.y]=true; 
    while(!Q.empty()) 
    { 
        ss=Q.front(); 
        Q.pop(); 
        for(int i=0;i<4;i++) 
        { 
            tt.x=ss.x+dir[i][0]; 
            tt.y=ss.y+dir[i][1]; 
            if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&map[tt.x][tt.y]!='#'&&!visit[tt.x][tt.y])//最基本的条件必须符合 
            { 
                step[tt.x][tt.y]=step[ss.x][ss.y]+1; 
                visit[tt.x][tt.y]=true; 
                Q.push(tt); 
            } 
        } 
    } 
    return ; 

int main() 

    int i,j; 
    while(scanf("%d%d",&n,&m)!=EOF) 
    { 
        for(i=0;i<n;i++) 
        { 
            scanf("%s",map[i]); 
            for(j=0;j<m;j++) 
            { 
                if(map[i][j]=='Y') st1.x=i,st1.y=j; 
                if(map[i][j]=='M') st2.x=i,st2.y=j; 
            } 
        } 
        Init(); 
        BFS(st1,step1); 
        MEM(visit,false); 
        BFS(st2,step2); 
        for(i=0;i<n;i++) 
            for(j=0;j<m;j++) 
                if(map[i][j]=='@'&&step1[i][j]!=0&&step2[i][j]!=0) 
                    if(step1[i][j]+step2[i][j]<MAXT) MAXT=step1[i][j]+step2[i][j]; 
        printf("%d\n",MAXT*11); 
 
    } 


作者:zfz1015
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,