HDU 2612 Find a way

来源:互联网 发布:饥荒联机网络启动失败 编辑:程序博客网 时间:2024/05/19 19:12

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)



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+最短路径,求出Y和M到每一个@的最短路径和,并找出最小值。



AC代码如下:

#include<stdio.h>#include<string.h>#include<math.h>#include<vector>#include<queue>#include<stack>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fint n,m;char maps[300][300];int vis[300][300];int dir[4][2]={{0,1},{1,0},{-1,0},{0,-1}};int dis[300][300][2],f;typedef struct cow{int x,y;int step;}Node;Node start,last,tmp;int  min(int x,int y){return x<y?x:y;}bool feasible(int x,int y){if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==-1&&maps[x][y]=='.'||maps[x][y]=='@')return true;return false;}queue <Node> q;void bfs(Node head){    Node next;    head.step=0;    vis[head.x][head.y]=1;    q.push(head);    while(!q.empty())    {        head=q.front();        q.pop();        for(int i=0; i<4; i++)        {            next.x=head.x+dir[i][0];            next.y=head.y+dir[i][1];            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&!vis[next.x][next.y]               &&(maps[next.x][next.y]=='.'||maps[next.x][next.y]=='@'))            {                vis[next.x][next.y]=1;                next.step=head.step+1;                if(maps[next.x][next.y]=='@')                    dis[next.x][next.y][f]=min(dis[next.x][next.y][f], next.step);                q.push(next);            }        }    }}int main(){while(~scanf("%d%d",&n,&m)){int i,j;f=0;memset(dis, INF, sizeof(dis));for(i=0;i<n;i++){getchar();for(j=0;j<m;j++){scanf("%c",&maps[i][j]);if(maps[i][j]=='Y'){start.x=i;start.y=j;}else if(maps[i][j]=='M'){last.x=i;last.y=j;}}}f=0;memset(vis,0,sizeof(vis));bfs(start);memset(vis,0,sizeof(vis));f=1;bfs(last);int mi=INF;for(i=0;i<n;i++){for(j=0;j<m;j++){if(maps[i][j]=='@'&&mi>dis[i][j][0]+dis[i][j][1]){mi=dis[i][j][0]+dis[i][j][1];}}}printf("%d\n",mi*11);}return 0;}


0 0
原创粉丝点击