HDU 2612 find a way
2015-02-06 11:05
288 查看
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4360 Accepted Submission(s): 1466
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
[align=left]Sample Output[/align]
66
88
66
[align=left]Author[/align]
yifenfei
[align=left]Source[/align]
奋斗的年代
题意,Y和M两个人分别同乡村和城市出发,想去吃KFC,问最短时间能到达哪家KFC。
注意:Y走的话,可以走M。M走的话,可以走Y。‘@’代表KFC。‘#’是不能走的。刚才又测试了下数据。发现了也可以认为Y不能走M,M不能走Y。
两种代码都能AC。
数据:
2 3
YM@
。。。
结果分别是33 和55 。都能AC。
思路:跑两遍BFS,将每个人到达每一个KFC的步数用数组记录下来,然后遍历整个地图,找到两个人同时到达某一个KFC的最小值。
附上我认为Y能走M,M能走Y的代码
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> #define inf 0x6ffffff using namespace std; char map[202][202];// 地图 int vis[202][202];// 标记数组 int flag1[202][202];//记录M到达任意KFC的时间 int flag[202][202];//记录Y到达任意KFC的时间 int n,m; int x1,y1,x2,y2; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; struct node { int x,y,step; } ; bool check(int x,int y) { if(x<0 ||y<0 ||x>=n ||y>=m ||map[x][y]=='#' ||vis[x][y]) //检查是否越界,是否已经搜过 return 0; return 1; } void bfs(int x,int y,int a[][202]) //进入坐标和记录数组 { int i; node st,ed; queue<node>q; st.x=x; st.y=y; st.step=0; q.push(st); while(!q.empty()) { st=q.front(); q.pop(); for(i=0;i<4;i++) { ed.x=st.x+dir[i][0]; ed.y=st.y+dir[i][1]; if(!check(ed.x,ed.y)) continue; ed.step=st.step+1; vis[ed.x][ed.y]=1; if(map[ed.x][ed.y]=='@') { a[ed.x][ed.y]=ed.step; } q.push(ed); } } } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { memset(flag,0,sizeof(flag)); memset(vis,0,sizeof(vis)); memset(flag1,0,sizeof(flag1));//全部初始化 for(i=0;i<n;i++) scanf("%s",map[i]); for(i=0;i<n;i++) for(j=0;j<m;j++) { if(map[i][j]=='Y') { x1=i; y1=j; } if(map[i][j]=='M') { x2=i; y2=j; } } vis[x1][y1]=1; bfs(x1,y1,flag);//一遍BFS之后 memset(vis,0,sizeof(vis));//初始化 vis[x2][y2]=1; bfs(x2,y2,flag1); //再跑一遍BFS。 int min=inf; for(i=0;i<n;i++) for(j=0;j<m;j++) //遍历整个地图、 { if(min>flag[i][j]+flag1[i][j] &&flag[i][j] &&flag1[i][j])//有值的地方说明有KFC,找出两个人同时到达一个KFC的最短时间 min=flag[i][j]+flag1[i][j]; } printf("%d\n",min*11);//每走一步为11分钟。 } return 0; }
相关文章推荐
- HDU 2612 Find a way(bfs)
- 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 (BFS)
- HDU-2612-Find a way
- hdu 2612 Find a way (BFS)
- HDU 2612 Find a way(双BFS) (N)
- HDU 2612 Find a way
- hdu 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way【bfs】
- HDU 2612 Find a way
- Find a way HDU - 2612
- HDU 2612 Find a way(BFS)
- HDU - 2612 Find a way
- hdu 2612 Find a way(bfs+优先队列)
- hdu 2612 Find a way