hdu 【2612】 Find a way
2016-04-15 16:14
363 查看
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9068 Accepted Submission(s): 2908
[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
#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; const int maxn = 205; const int INF = 0x3f3f3f3f; int n,m,sx,sy,ex,ey; char maze[maxn][maxn]; int vis[maxn][maxn]; int time[maxn][maxn]; int dir[4][2] = {{1,0},{-1,0},{0,-1},{0,1}}; struct Node { int x,y,t; }pre,cur; void bfs(int x,int y) { memset(vis,0,sizeof(vis)); queue <Node> que; while(!que.empty()) que.pop(); pre.x = x; pre.y = y; pre.t = 0; vis[x][y] = 1; que.push(pre); while(!que.empty()) { pre = que.front(); que.pop(); if(maze[pre.x][pre.y] == '@') time[pre.x][pre.y] += pre.t; for(int i = 0; i < 4; i++) { int xx = pre.x + dir[i][0]; int yy = pre.y + dir[i][1]; if(xx < 0 || xx >= n || yy < 0 || yy >= m) continue; if(!vis[xx][yy] && maze[xx][yy] != '#') { vis[xx][yy] = 1; cur.x = xx; cur.y = yy; cur.t = pre.t+11; que.push(cur); } } } } int main() { while(scanf("%d%d", &n, &m) != EOF) { memset(time,0,sizeof(time)); for(int i = 0; i < n; i++) scanf("%s", maze[i]); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(maze[i][j] == 'Y') sx = i, sy = j; else if(maze[i][j] == 'M') ex = i, ey = j; bfs(sx,sy); bfs(ex,ey); int Min = INF; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(time[i][j] && time[i][j] < Min) Min = time[i][j]; printf("%d\n", Min); } return 0; }
相关文章推荐
- Android之SystemUI加载流程和NavigationBar的分析
- CodeForces 660E Different Subsets For All Tuples(动态规划)
- iOS 富文本
- 172. Factorial Trailing Zeroes
- javascript初学——var,alert的使用,meta标签的使用
- CentOS6.5上hive安装过程与常见错误调试
- 数据归一化的方法总结
- U3D手游《苍穹变》性能优化经验谈
- 机房重构下机(职责链+策略+反射+存储过程)
- 嘉定三屠与扬州十屠
- 头像添加边框
- Software Testing 2nd Lab --- Selenium
- android 基础知识-API
- 说一说娱乐圈的那些追星的脑残粉(钟汉良粉丝)
- mysql 同步redis
- Eclipse安装Axis2插件失败
- 初识:pcl安装和配置
- VFL 语句
- 缺少web.xml的version引发的血案
- 动态规划:从新手到专家