当前位置: 首页 > 工具软件 > Express Combo > 使用案例 >

hdu2612 Find a way BFS搜索 TWT Tokyo Olympic 2combo-4

易俊远
2023-12-01

Find a way

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


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

一开始没有看清题意,写的比较乱。。。吃拉面的时候想清楚了。。。回来写写写

本来以为会超时的

1A,嗯。。。并没有啥难度!

下面代码:

/*
 ━━━━━┒
 ┓┏┓┏┓┃μ'sic foever!!
 ┛┗┛┗┛┃\○/
 ┓┏┓┏┓┃ /
 ┛┗┛┗┛┃ノ)
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┛┗┛┗┛┃
 ┓┏┓┏┓┃
 ┃┃┃┃┃┃
 ┻┻┻┻┻┻
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=210;
const int INF=8000000;
int dy[maxn][maxn];
int dm[maxn][maxn];
char save[maxn][maxn];
int n,m;
int xy,yy,xm,ym;
struct unit{
    int x,y;
};
int result;
int move_x[4]={0,0,1,-1};
int move_y[4]={1,-1,0,0};
map<pair<int,int>,int> kfc;
void bfsy(){
    queue<unit> que;
    int i,j;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            dy[i][j]=INF;
        }
    }
    unit start;
    start.x=xy,start.y=yy;
    que.push(start);
    dy[start.x][start.y]=0;
    while(que.size()){
        //cout<<"i am Y"<<endl;
        
        unit q=que.front();
        //cout<<"Y "<<q.x<<" "<<q.y<<" "<<dy[q.x][q.y]<<endl;
        
        que.pop();
        if(save[q.x][q.y]=='@'){
            //cout<<"Y: "<<dy[q.x][q.y]<<endl;
            kfc[make_pair(q.x, q.y)]=dy[q.x][q.y];
            //break;
        }
        for(i=0;i<4;i++){
            int nx=q.x+move_x[i];
            int ny=q.y+move_y[i];
            if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&save[nx][ny]!='#'&&dy[nx][ny]==INF){
                unit now;
                now.x=nx;
                now.y=ny;
                que.push(now);
                dy[nx][ny]=dy[q.x][q.y]+1;
            }
        }
    }
}
void bfsm(){
    queue<unit> que;
    int i,j;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            dm[i][j]=INF;
        }
    }
    unit start;
    start.x=xm,start.y=ym;
    que.push(start);
    dm[start.x][start.y]=0;
    while(que.size()){
        //cout<<"i am M"<<endl;
        
        
        unit q=que.front();
        //cout<<"M: "<<q.x<<" "<<q.y<<" "<<dm[q.x][q.y]<<endl;
    
        que.pop();
        if(save[q.x][q.y]=='@'){
            //cout<<"M: "<<dm[q.x][q.y]<<" "<<kfc[make_pair(q.x, q.y)]<<endl;
            result=min(result,kfc[make_pair(q.x, q.y)]+dm[q.x][q.y]);
            //break;
        }
        for(i=0;i<4;i++){
            int nx=q.x+move_x[i];
            int ny=q.y+move_y[i];
            if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&save[nx][ny]!='#'&&dm[nx][ny]==INF){
                unit now;
                now.x=nx;
                now.y=ny;
                que.push(now);
                dm[nx][ny]=dm[q.x][q.y]+1;
            }
        }
    }
}
int main(){
    int i,j;
    while(~scanf("%d%d",&n,&m)){
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                cin>>save[i][j];
                if(save[i][j]=='Y'){
                    xy=i,yy=j;
                }
                if(save[i][j]=='M'){
                    xm=i,ym=j;
                }
                if(save[i][j]=='@'){
                    pair<int,int> kfc_point=make_pair(i, j);
                    kfc[kfc_point]=0;
                }
            }
        }
        result=INF;
        bfsy();
        bfsm();
        printf("%d\n",result*11);
    }
    return 0;
}





 类似资料: