博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 2612 Find a way(BFS)
阅读量:4577 次
发布时间:2019-06-08

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

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. 

InputThe 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 
OutputFor 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的距离,用广度搜索计算,然后从两人都能到达的kfc中选取路程最小的那个。

1 #include 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 typedef struct{ 9 int i,j;10 int step;11 }loca;12 13 int n,m;14 char mm[205][205];//地图15 int flag[205][205];//此位置是否待过16 int step[205][205];//到达此处的步数,只有@时才赋值17 int isarrival[205][205];//如果此处两个人都到达才可以18 int yifeni=0,yifenj=0;//yifen位置19 int meri=0,merj=0;//mer位置20 queue
q;21 loca now,nextt;22 int add[2][4]={-1,1,0,0,23 0,0,-1,1};24 25 void go(int si,int sj){26 now.i=si;27 now.j=sj;28 now.step=0;29 flag[si][sj]=1;30 q.push(now);31 while(!q.empty()){32 now=q.front(); q.pop();33 if(mm[now.i][now.j]=='@'){34 step[now.i][now.j]+=now.step;35 isarrival[now.i][now.j]++;36 }37 flag[now.i][now.j]=1;38 for(int i=0;i<4;i++){39 nextt.i=now.i+add[0][i];40 nextt.j=now.j+add[1][i];41 nextt.step=now.step+1;42 if(nextt.i<0||nextt.i>=n||nextt.j<0||nextt.j>m){43 continue;44 }45 if(flag[nextt.i][nextt.j]==0 && mm[nextt.i][nextt.j]!='#'){46 q.push(nextt);47 flag[nextt.i][nextt.j]=1;48 }49 }50 }51 }52 53 int main()54 {55 while(~scanf("%d %d",&n,&m)){56 memset(step,0,sizeof(step));57 memset(isarrival,0,sizeof(isarrival));58 memset(flag,0,sizeof(flag));59 for(int i=0;i

 

转载于:https://www.cnblogs.com/TWS-YIFEI/p/8821428.html

你可能感兴趣的文章
vim下正则表达式的非贪婪匹配
查看>>
一个python的计算熵(entropy)的函数
查看>>
spring源码学习——spring整体架构和设计理念
查看>>
模拟window系统的“回收站”
查看>>
报文格式【定长报文】
查看>>
RDLC报表钻取空白页问题
查看>>
多路电梯调度的思想
查看>>
jQuery-对Select的操作
查看>>
过滤器、监听器、拦截器的区别
查看>>
为什么要进行需求分析?通常对软件系统有哪些需求?
查看>>
一些模板
查看>>
jquery和dom元素相互转换
查看>>
放大的X--HDOJ-201307292012
查看>>
题目831-签到-nyoj-20140818
查看>>
百词斩-斩家秘籍
查看>>
Mysql主从配置,实现读写分离
查看>>
完整版本的停车场管理系统源代码带服务端+手机android客户端
查看>>
排序精讲
查看>>
【bzoj3172】 Tjoi2013—单词
查看>>
【uoj2】 NOI2014—起床困难综合症
查看>>