博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2612
阅读量:6407 次
发布时间:2019-06-23

本文共 3285 字,大约阅读时间需要 10 分钟。

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

1 #include
2 #include
3 #include
4 #include
5 #include
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
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 }

 

 

转载于:https://www.cnblogs.com/tom987690183/p/3659977.html

你可能感兴趣的文章
PHPCMS源码底层分析 phpcms\base.php(编写中,未完成)
查看>>
Android 使用NDK编译sipdroid Library
查看>>
2012 MUTC 3 总结
查看>>
slim中的参数获取
查看>>
mysql5.7.22 zip 版安装
查看>>
time.setToNow() 取当前时间,月份有误
查看>>
arcengine9.3与10开发授权代码
查看>>
UEFI+GPT下安装Win10和Ubuntu16.04双系统相关问题(引导、无线连不上网)
查看>>
【题解】最大公约数之和 V3 51nod 1237 杜教筛
查看>>
架构师速成6.7-设计开发思路-uml 分类: 架构师速成 ...
查看>>
js设置radio选中
查看>>
8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,数据结构“栈”实现...
查看>>
第一次发博客-说说我的B/S开发框架(asp.net mvc + web api + easyui)
查看>>
python之路之线程,进程,协程
查看>>
ZROI2018提高day3t1
查看>>
VC的水波效果
查看>>
微信支付SDK集成
查看>>
如何使用wepy和 vant-weapp开发小程序
查看>>
Angular7教程-03-Angular常用操作(上)
查看>>
洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here
查看>>