HDU 2612 Find a way

来源:互联网 发布:xplay6知乎 编辑:程序博客网 时间:2024/06/06 05:07

Find a way

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


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
题目大意:有两个人想在KFC约会,可是他们家又不在同一个地方,所以要找到一条路线使他们的步数最少,输出步数
对这两个人进行广搜即可

#include<iostream>#include<cstring>#include<cstdio>#include<queue>using namespace std;int n,m;char map[250][250];int book[250][250];int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int stepY[250][250];int stepM[250][250];struct Node{int x,y;};Node yff,mer;bool judge(int x,int y){if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='#'&&!book[x][y]) return true;return false;}void bfsY(Node a){queue<Node> que;que.push(a);book[a.x][a.y]=1;while(!que.empty()){Node now,next;now=que.front();que.pop();for(int i=0;i<4;i++){next.x=now.x+dir[i][0];next.y=now.y+dir[i][1];if(judge(next.x,next.y)){book[next.x][next.y]=1;stepY[next.x][next.y]=stepY[now.x][now.y]+1;que.push(next);}}}return;}void bfsM(Node a){queue<Node> que;que.push(a);book[a.x][a.y]=1;while(!que.empty()){Node now,next;now=que.front();que.pop();for(int i=0;i<4;i++){next.x=now.x+dir[i][0];next.y=now.y+dir[i][1];if(judge(next.x,next.y)){book[next.x][next.y]=1;stepM[next.x][next.y]=stepM[now.x][now.y]+1;que.push(next);}}}return;}int main(){while(~scanf("%d%d",&n,&m)){int max=6666666;for(int i=0;i<n;i++) scanf("%s",map[i]);for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(map[i][j]=='Y'){yff.x=i;yff.y=j;}if(map[i][j]=='M'){mer.x=i;mer.y=j;}}}memset(stepY,0,sizeof(stepY));memset(book,0,sizeof(book));bfsY(yff);memset(stepM,0,sizeof(stepM));memset(book,0,sizeof(book));bfsM(mer);for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(map[i][j]=='@'&&stepY[i][j]!=0&&stepM[i][j]!=0){if(stepY[i][j]+stepM[i][j]<max){max=stepY[i][j]+stepM[i][j];}}}}cout<<max*11<<endl;}}


原创粉丝点击