Find a way HDU

来源:互联网 发布:淘宝网健身手球 编辑:程序博客网 时间:2024/06/01 22:35

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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866


题意: Y和M要去同一个肯德基会面,但是这个地方有很多肯德基,问到哪个肯德基会面所用时间最短,@标识肯德基。

分析: 坑点在Y能到的肯德基,M不一定能到,所以只有当两人都能到时才算数。其它的话,我是分别bfs,记录一个人到每个肯德基的时间,然后再bfs另一个人。






#include<iostream>#include<cstdio>#include<string.h>#include<math.h>#include<string>#include<map>#include<set>#include<vector>#include<algorithm>#include<queue>#include<iomanip>using namespace std;const int INF = 0x3f3f3f3f;const int NINF = 0xc0c0c0c0;typedef long long ll;bool ans[205][205];int res[205][205];int people[205][205];int n,m;int dx[4] = {0,1,0,-1};int dy[4] = {1,0,-1,0};struct P{    int x,y,step;};void bfs(int x,int y,bool vis[][205],int cnt){    queue<P> q;    P temp;    temp.x = x;    temp.y = y;    temp.step = 0;    q.push(temp);    while(!q.empty()){        temp = q.front();        q.pop();        if(ans[temp.y][temp.x] == 1){            res[temp.y][temp.x] += temp.step;            vis[temp.y][temp.x] = 0;            people[temp.y][temp.x]++;            cnt--;            if(cnt == 0) return;        }        for(int i=0;i<4;i++){            P dt = temp;            dt.x += dx[i];            dt.y += dy[i];            if(dt.x >=0 && dt.x < m && dt.y >= 0 && dt.y<n && vis[dt.y][dt.x] == 1){                vis[dt.y][dt.x] = 0;                dt.step++;                q.push(dt);            }        }    }}int main(){    while(cin >> n >> m){        int cnt;        cnt = 0;        bool vis[205][205];        memset(vis,0,sizeof(vis[0][0])*205*205);        memset(ans,0,sizeof(ans[0][0])*205*205);        memset(res,0,sizeof(res[0][0])*205*205);        memset(people,0,sizeof(people[0][0])*205*205);        int Yy,Yx,My,Mx;        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                char t;                cin >> t;                if(t == '.' || t == '@') vis[i][j] = 1;                if(t == '@'){                    ans[i][j] = 1;                    cnt++;                }                 if(t == 'Y'){                    Yy = i;                    Yx = j;                }                if(t == 'M'){                    My = i;                    Mx = j;                }            }        }         bool cpvis[205][205];        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                cpvis[i][j] = vis[i][j];            }        }        bfs(Yx,Yy,vis,cnt);        bfs(Mx,My,cpvis,cnt);        int minest = INF;        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                if(ans[i][j] == 1 && people[i][j] == 2){                    if(minest > res[i][j]){                        minest = res[i][j];                    }                }            }        }        cout << minest*11 << endl;    }}


原创粉丝点击