hdu 2612 Find a way
来源:互联网 发布:晋江 小说 推荐 知乎 编辑:程序博客网 时间:2024/06/10 22:47
Find a way
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 74 Accepted Submission(s) : 25
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
Sample Input
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866
两个人往kfc走,求走到后步数和最小,因为有很多kfc存在,就用2个数组记录2个人到不同kfc的答案,最后求和最小。wa了很多次,后来发现应该要把没走到的点步数记录为无限大
而不是0。。。
#include<iostream>#include<cstring>#include<queue>using namespace std;struct po{ int x,y; int step;};int ren[2][2];int kfc[222][2];char ma[222][222];int n,m;int ans1[222][222];int ans2[222][222];int vis1[222][222];int vis2[222][222];int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};bool check(int x,int y){ if(x<1||y<1||x>n||y>m||ma[x][y]=='#') return 1; return 0;}void bfs(int sx,int sy){ po tt,hh; queue<po> st; tt.x=sx; tt.y=sy; tt.step=0; ans1[sx][sy]=0; //memset(vis1,0,sizeof(vis1)); vis1[sx][sy]=1; st.push(tt); while(!st.empty()) { tt=st.front(); st.pop(); for(int i=0;i<4;i++) { hh=tt; hh.x+=dx[i]; hh.y+=dy[i]; hh.step+=1; if(check(hh.x,hh.y))continue; if(ans1[hh.x][hh.y])continue; //vis1[hh.x][hh.y]=1; ans1[hh.x][hh.y]=hh.step; st.push(hh); } }}void bfs1(int sx,int sy){ po tt,hh; queue<po> st; tt.x=sx; tt.y=sy; tt.step=0; ans2[sx][sy]=0; memset(vis2,0,sizeof(vis2)); //vis2[sx][sy]=1; st.push(tt); while(!st.empty()) { tt=st.front(); st.pop(); for(int i=0;i<4;i++) { hh=tt; hh.x+=dx[i]; hh.y+=dy[i]; hh.step+=1; if(check(hh.x,hh.y))continue; if(ans2[hh.x][hh.y])continue; //vis2[hh.x][hh.y]=1; ans2[hh.x][hh.y]=hh.step; st.push(hh); } } }int main(){ while(scanf("%d%d",&n,&m)!=EOF) { memset(ans1,0,sizeof(ans1)); memset(ans2,0,sizeof(ans2)); int js=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { cin>>ma[i][j]; if(ma[i][j]=='Y') { ren[0][0]=i; ren[0][1]=j; } if(ma[i][j]=='M') { ren[1][0]=i; ren[1][1]=j; } } bfs(ren[0][0],ren[0][1]); bfs1(ren[1][0],ren[1][1]); long long int ans=99999999; for(int ss=1;ss<=n;ss++) for(int sss=1;sss<=m;sss++) { if(ma[ss][sss]=='@') { if(ans>ans1[ss][sss]+ans2[ss][sss]&&ans1[ss][sss]!=0&&ans2[ss][sss]!=0) ans=ans1[ss][sss]+ans2[ss][sss]; } } cout<<ans*11<<endl; } return 0;}
- HDU 2612 Find a way
- HDU-2612-Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU 2612 Find A Way
- hdu 2612 Find a way
- HDU-2612-Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU-2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- 页面布局
- 7GOJ 学院 [连通性][并查集/DFS]
- 全志R16尝试移植UbuntuCore(没有做完!!!!)(分色排版)
- bzoj2406 矩阵 二分+有源汇上下界网络流
- AIZU OJ 2308 基础计算几何学习
- hdu 2612 Find a way
- servlet的init方法初始化方式
- 在menuconfig中增加EXT4的支持(没有完成)(分色排版)
- 主题模型︱几款新主题模型——SentenceLDA、CopulaLDA、TWE简析与实现
- final关键字 多态 抽象
- USACO-Section2.1 Ordered Fractions [其他][排序]
- 在内核中加入对EXT4的支持(没有做完!!!!)(分色排版)
- Swift中的本地化实现
- HDU4405:Aeroplane chess(概率dp & 期望)