程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [hdu] Find a way (分別從兩個點出發BFS,再計算最小值)

[hdu] Find a way (分別從兩個點出發BFS,再計算最小值)

編輯:C++入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved