HDOJ-2612 Find a way

来源:互联网 发布:windows rt刷win7系统 编辑:程序博客网 时间:2024/04/30 04:06

Find a way

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


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

Sample Output
668866
介个题非常走心,累了个半死,分成两次来广搜,BFS运用起来还是挺简单的。
下边是代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <queue>using namespace std;struct node{int x,y,time;};int n,m,ans;char map[210][210];int time[210][210];int vis[210][210];int fx[4]={1,-1,0,0};int fy[4]={0,0,1,-1};void BFS(int x,int y){node now,next,f;f.x=x;f.y=y;f.time=0;queue<node> q;q.push(f);while(!q.empty()){now=q.front();q.pop();if(map[now.x][now.y]=='@') time[now.x][now.y]+=now.time;for(int i=0;i<4;i++){next.x=now.x+fx[i];next.y=now.y+fy[i];if((map[next.x][next.y]=='.'||map[next.x][next.y]=='@')&&vis[next.x][next.y]==0&&next.x>=0&&next.x<n&&next.y>=0&&next.y<m){vis[next.x][next.y]=1;next.time=now.time+1;q.push(next);}}}}int main(){int x1,x2,y1,y2;while(~scanf("%d%d",&n,&m)){memset(time,0,sizeof(time));for(int i=0;i<n;i++){getchar();for(int j=0;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(vis,0,sizeof(vis));BFS(x1,y1);memset(vis,0,sizeof(vis));BFS(x2,y2);int min=99999999;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(time[i][j]&&min>time[i][j])    min=time[i][j];}}printf("%d\n",11*min);}return 0;}


0 0
原创粉丝点击