hdu 2612 Find a way
2013-05-10 22:35
369 查看
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2514 Accepted Submission(s): 804
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 记录每个人到每个kfc所用的最小时间,然后从中选出两个相加为最小的。如果某个人去不到某一间kfc则标记这个时间为INF AC代码:#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <cmath> #include <string> #include <algorithm> #define INF 10000000 using namespace std; struct node { int x,y; int step; }yi,me; int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int m,n,flag; int dis[205][205][2]; char map[205][205]; bool v[205][205]; bool judge(int x,int y) { if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='#'&&!v[x][y]) return true; return false; } void bfs(node peo) { node first,next; queue<node>q; memset(v,false,sizeof(v)); v[peo.x][peo.y]=true; peo.step=0; q.push(peo); while(!q.empty()) { first=q.front(); q.pop(); if(map[first.x][first.y]=='@') dis[first.x][first.y][flag]=first.step; for(int i=0;i<4;i++) { next=first; next.x+=d[i][0]; next.y+=d[i][1]; if(!judge(next.x,next.y)) continue; v[next.x][next.y]=true; next.step+=11; q.push(next); } } } int main() { while(cin>>n>>m) { for(int i=0;i<n;i++) for(int j=0;j<m;j++) { cin>>map[i][j]; if(map[i][j]=='Y') { yi.x=i; yi.y=j; } else if(map[i][j]=='M') { me.x=i; me.y=j; } } for(int i=0;i<n;i++) for(int j=0;j<m;j++) dis[i][j][0]=dis[i][j][1]=INF; flag=0; bfs(yi); flag=1; bfs(me); int min=INF; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(map[i][j]=='@'&&min>dis[i][j][0]+dis[i][j][1]) min=dis[i][j][0]+dis[i][j][1]; cout<<min<<endl; } return 0; }
相关文章推荐
- HDU 2612 Find a way (BFS+dp)
- HDU 2612 Find a way(两次BFS)
- [kuangbin带你飞]专题一 简单搜索N - Find a way(HDU 2612)
- HDU ~ 2612 ~ Find a way (预处理+BFS)
- hdu 2612 Find a way(bfs)
- hdu 2612 Find a way (BFS)
- C语言BFS(4)___Find a way(Hdu 2612)
- HDU 2612 Find a way 多起点 多终点 BFS
- HDU 2612 Find a way BFS 2次广搜
- HDU 2612 Find a way【第一次双BFS】
- hdu 2612 Find a way (广搜)
- HDU 2612 Find a way (两次bfs)
- ACM_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 (注意细节的BFS)
- hdu 2612 Find a way
- HDU 2612 find a way
- HDU 2612.Find a way