Find a way
来源:互联网 发布:多益网络有哪些游戏 编辑:程序博客网 时间:2024/05/07 19:06
这道题目需要用到两次广搜,分别找出两个人到各个kfc花费的时间(有的人可能无法到达所有的kfc,而且只要不是#就都能走)
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.
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.
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
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
668866
#include<stdio.h>#include<string.h >#include<vector>#include<queue>#include<algorithm>using namespace std;struct list{int x;int y;int step;};int n,m;bool check(int x,int y);bool book[205][205];char fig[205][205];int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};vector<struct list> vy,vm;queue<struct list> q;int main(){while(scanf("%d%d",&n,&m)!=EOF) {int _min;struct list Y,M;vy.clear();vm.clear();memset(fig,0,sizeof(fig));for(int i=0;i<n;i++){for(int j=0;j<m;j++){scanf(" %c",&fig[i][j]);if(fig[i][j]=='Y'){Y.x=i;Y.y=j;Y.step=0;}if(fig[i][j]=='M'){M.x=i;M.y=j;M.step=0;}}}struct list ne;q.push(Y);memset(book,false,sizeof(book));book[Y.x][Y.y]=true;while(!q.empty()){struct list Q;Q=q.front();for(int i=0;i<4;i++){if(check(Q.x+dir[i][0],Q.y+dir[i][1])){book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;ne.x=Q.x+dir[i][0];ne.y=Q.y+dir[i][1];ne.step=Q.step+1;q.push(ne);if(fig[Q.x+dir[i][0]][Q.y+dir[i][1]]=='@'){vy.push_back(ne);}}}q.pop();}q.push(M);memset(book,false,sizeof(book));book[M.x][M.y]=true;while(!q.empty()){struct list Q;Q=q.front();for(int i=0;i<4;i++){if(check(Q.x+dir[i][0],Q.y+dir[i][1])){book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;ne.x=Q.x+dir[i][0];ne.y=Q.y+dir[i][1];ne.step=Q.step+1;q.push(ne);if(fig[Q.x+dir[i][0]][Q.y+dir[i][1]]=='@'){vm.push_back(ne);}}}q.pop();}_min=0x3f3f3f3f;for(int i=0;i<vy.size();i++){for(int j=0;j<vm.size();j++){if(vy[i].x==vm[j].x&&vy[i].y==vm[j].y)_min=min(_min,vy[i].step+vm[j].step);}}printf("%d\n",_min*11);}return 0;}bool check(int x,int y){if(x>=0&&x<n&&y>=0&&y<m)if(book[x][y]==false)if(fig[x][y]!='#')return true;return false;}
阅读全文
0 0
- Find a way
- Find a way hdu2612
- Find a way
- Find a way 2612
- HDU2612---Find a way
- Find a way
- hdu2612---Find a way
- Find a way
- N. Find a way
- HDU2612 Find a way
- Find a way--hdoj
- hdu Find a way
- Find a way
- Find a way
- hdu Find a way
- Find a way
- Find a way<hdoj2612>
- HDU:Find a way
- keytool/android签名/温习
- Android Launcher3修改长按屏幕空白处进入OverviewMode模式,进入OverviewMode模式点击Home无法复原的bug
- 常用的正则表达式验证
- HDU-2017 多校训练赛2-1011-Regular polygon
- 经典算法-“水仙花”3位数的个位,十位,百位的立方和等于原来的数字
- Find a way
- 判断一棵树是不是完全二叉树
- Easyui select onChange事件
- day08 abstract interface 多多指教
- 判断字符串中是否含有大写字母(Regex)
- Linux 设置apache,mysql开机启动
- 流式布局--标签
- JavaScript while, do while for 循环语句
- 通过JDBC进行简单的增删改查(以MySQL为例)