HDOJ-2612 Find a way
来源:互联网 发布:windows rt刷win7系统 编辑:程序博客网 时间:2024/04/30 04:06
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10425 Accepted Submission(s): 3418
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.
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
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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866介个题非常走心,累了个半死,分成两次来广搜,BFS运用起来还是挺简单的。下边是代码:#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <queue>using namespace std;struct node{int x,y,time;};int n,m,ans;char map[210][210];int time[210][210];int vis[210][210];int fx[4]={1,-1,0,0};int fy[4]={0,0,1,-1};void BFS(int x,int y){node now,next,f;f.x=x;f.y=y;f.time=0;queue<node> q;q.push(f);while(!q.empty()){now=q.front();q.pop();if(map[now.x][now.y]=='@') time[now.x][now.y]+=now.time;for(int i=0;i<4;i++){next.x=now.x+fx[i];next.y=now.y+fy[i];if((map[next.x][next.y]=='.'||map[next.x][next.y]=='@')&&vis[next.x][next.y]==0&&next.x>=0&&next.x<n&&next.y>=0&&next.y<m){vis[next.x][next.y]=1;next.time=now.time+1;q.push(next);}}}}int main(){int x1,x2,y1,y2;while(~scanf("%d%d",&n,&m)){memset(time,0,sizeof(time));for(int i=0;i<n;i++){getchar();for(int j=0;j<m;j++){scanf("%c",&map[i][j]);if(map[i][j]=='Y'){x1=i;y1=j;}if(map[i][j]=='M'){x2=i;y2=j;}}}memset(vis,0,sizeof(vis));BFS(x1,y1);memset(vis,0,sizeof(vis));BFS(x2,y2);int min=99999999;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(time[i][j]&&min>time[i][j]) min=time[i][j];}}printf("%d\n",11*min);}return 0;}
0 0
- hdoj 2612 Find a way
- hdoj 2612 Find a way
- HDOJ 2612 Find a way
- HDOJ-2612 Find a way
- Find a way--hdoj
- hdoj 2612 Find a way (BFS)
- hdoj-2612-find a way-双向BFS
- HDOJ 2612 Find a way【BFS】
- HDOJ 2612 Find a way (双向BFS)
- HDU/HDOJ 2612 Find a way 双向BFS
- HDOJ 题目2612 Find a way(BFS)
- hdoj 2612 Find a way简单广搜
- HDOJ 2612 Find a way【最短路 双重bfs】
- hdoj 2612 Find a way (bfs,双向搜索)
- Find a way 2612
- HDOJ Find a way(两次bfs)
- HDU 2612 Find a way
- HDU-2612-Find a way
- 337. House Robber III
- hdu 2177 取(2堆)石子游戏 (威佐夫博弈求取后状态+二分)
- Android 开发资源
- POJ 1011 Sticks (经典DFS)
- web框架学习前复习——jsDom
- HDOJ-2612 Find a way
- SpringMVC+MyBatis - 7 Spring自动扫描注解类的冲突问题
- poj 3233 Matrix Power Series(矩阵快速幂+分治)
- IO流切割文件
- ViewPager滑动监听
- Linux操作之进程与线程
- 理解伪元素 :before 和 :after
- SQL 操作列转行查询
- 并发之Threads,Executors,ForkJoin和Actors