首页 > 其他 > 详细

hdu 2612

时间:2014-04-13 15:55:49      阅读:660      评论:0      收藏:0      [点我收藏+]

Find a way

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


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
 
能不能用交替进行的方式进行双向光搜?
不行,因为可能你走的可能不是通往最佳的@的。
另加一组数据

6 5
Y.@#.
.#.#.
.#.#.
.#.#.
.#.#.
##M..

77

bubuko.com,布布扣
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<queue>
  6 using namespace std;
  7 
  8 int n,m;
  9 int yx,yy,mx,my;
 10 int to[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
 11 char a[210][210];
 12 int  time[2][202][202];
 13 bool hash[2][202][202];
 14 bool flag;
 15 struct node
 16 {
 17     int x,y;
 18     int time;
 19 };
 20 queue<node>Q[2];
 21 
 22 int Min(int x,int y)
 23 {
 24     return x>y? y:x;
 25 }
 26 bool pd(node &t)
 27 {
 28     if(t.x>=1&&t.x<=n && t.y>=1&&t.y<=m && a[t.x][t.y]!=#)return false;
 29     return true;
 30 }
 31 int bfs(int x)
 32 {
 33     int i,hxl=1111111;
 34     node t,cur;
 35 
 36     while(!Q[x].empty())
 37     {
 38         cur=Q[x].front();
 39         Q[x].pop();
 40         for(i=0;i<4;i++)
 41         {
 42             t=cur;
 43             t.x=t.x+to[i][0];
 44             t.y=t.y+to[i][1];
 45             t.time++;
 46             if(pd(t))continue;
 47             if(hash[x][t.x][t.y])continue;
 48             hash[x][t.x][t.y]=true;
 49             time[x][t.x][t.y]=t.time;
 50             if(x==1 && a[t.x][t.y]==@)
 51             {
 52                 hxl=Min(hxl,time[x^1][t.x][t.y]+time[x][t.x][t.y]);
 53             }
 54             Q[x].push(t);
 55         }
 56     }
 57     return hxl;
 58 }
 59 void dbfs()
 60 {
 61     int ans=0;
 62     node t;
 63     t.x=yx;
 64     t.y=yy;
 65     t.time=0;
 66     Q[0].push(t);
 67     hash[0][yx][yy]=true;
 68 
 69     t.x=mx;
 70     t.y=my;
 71     t.time=0;
 72     Q[1].push(t);
 73     hash[1][mx][my]=true;
 74 
 75     bfs(0);
 76     ans = bfs(1);
 77     printf("%d\n",ans*11);
 78 }
 79 int main()
 80 {
 81     int i,j;
 82     while(scanf("%d%d",&n,&m)>0)
 83     {
 84         for(i=1;i<=n;i++)
 85             scanf("%s",a[i]+1);
 86         memset(hash,false,sizeof(hash));
 87         memset(time,0,sizeof(time));
 88         for(i=1;i<=n;i++)
 89             for(j=1;j<=m;j++){
 90                 if(a[i][j]==Y){
 91                     yx=i;
 92                     yy=j;
 93                 }
 94                 else if(a[i][j]==M){
 95                     mx=i;
 96                     my=j;
 97                 }
 98             }
 99         while(!Q[0].empty()){
100             Q[0].pop();
101         }
102         while(!Q[1].empty()){
103             Q[1].pop();
104         }
105         flag=false;
106         dbfs();
107     }
108     return 0;
109 }
bubuko.com,布布扣

 

 

hdu 2612,布布扣,bubuko.com

hdu 2612

原文:http://www.cnblogs.com/tom987690183/p/3659977.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!