博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2612 Find a Way
阅读量:4455 次
发布时间:2019-06-07

本文共 4087 字,大约阅读时间需要 13 分钟。

题目链接

题目大意:Y,M往@走,问走到最近的@耗时多少。上下左右移动,移动一次11分钟。

解题思路:上来直接枚举@宽搜到Y,M距离记录最小值果断wa,果然搜索还是太耗时了。宽搜两次记录Y到每个@的距离和M到每个@的距离,然后枚举每个点,是@就计算,记录最小值。

这道题最有趣的地方莫过于要脑补限制:

M Y是不能通过的

@是可以通过的

代码:

1 const int maxn = 500; 2 const int iadd[] = {
0, 1, 0, -1}, jadd[] = {
1, 0, -1, 0}; 3 char maze[maxn][maxn]; 4 int vis[maxn][maxn], disy[maxn][maxn], dism[maxn][maxn]; 5 int n, m; 6 struct node{ 7 int i, j, t; 8 }; 9 10 void bfs1(int sti, int stj){11 memset(vis, 0, sizeof(vis));12 memset(disy, 0x3f, sizeof(disy));13 node u; u.i = sti; u.j = stj; u.t = 0;14 queue
q;15 q.push(u);16 vis[u.i][u.j] = 1; disy[u.i][u.j] = 0;17 while(!q.empty()){18 u = q.front(); q.pop();19 for(int i = 0; i < 4; i++){20 node v = u;21 v.i += iadd[i]; v.j += jadd[i]; v.t += 1;22 if(v.i < 0 || v.j < 0 || v.i >= n || v.j >= m) continue;23 if(vis[v.i][v.j]) continue; 24 if(maze[v.i][v.j] == '#') continue;25 if(maze[v.i][v.j] == 'M') continue;26 q.push(v);27 disy[v.i][v.j] = v.t;28 vis[v.i][v.j] = 1;29 }30 }31 }32 33 void bfs2(int sti, int stj){34 memset(vis, 0, sizeof(vis));35 memset(dism, 0x3f, sizeof(dism));36 node u; u.i = sti; u.j = stj; u.t = 0;37 queue
q;38 q.push(u);39 vis[u.i][u.j] = 1; dism[u.i][u.j] = 0;40 while(!q.empty()){41 u = q.front(); q.pop();42 for(int i = 0; i < 4; i++){43 node v = u;44 v.i += iadd[i]; v.j += jadd[i]; v.t += 1;45 if(v.i < 0 || v.j < 0 || v.i >= n || v.j >= m) continue;46 if(vis[v.i][v.j]) continue; 47 if(maze[v.i][v.j] == '#') continue;48 if(maze[v.i][v.j] == 'Y') continue;49 q.push(v);50 dism[v.i][v.j] = v.t;51 vis[v.i][v.j] = 1;52 }53 }54 }55 56 int main(){57 while(scanf("%d %d", &n, &m) != EOF){58 memset(vis, 0, sizeof(vis));59 int yi, yj, mi, mj;60 for(int i = 0; i < n; i++){61 for(int j = 0; j < m; j++){62 scanf(" %c", &maze[i][j]);63 if(maze[i][j] == 'Y') yi = i, yj = j;64 if(maze[i][j] == 'M') mi = i, mj = j;65 }66 }67 bfs1(yi, yj);68 bfs2(mi, mj);69 int ans = inf;70 for(int i = 0; i < n; i++){71 for(int j = 0; j < m; j++){72 if(maze[i][j] == '@'){73 if(disy[i][j] != inf && dism[i][j] != inf)74 ans = min(ans, disy[i][j] + dism[i][j]);75 }76 }77 }78 printf("%d\n", ans * 11);79 }80 }

题目:

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 16695    Accepted Submission(s): 5358

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
 

 

Author
yifenfei
 

 

Source

 

转载于:https://www.cnblogs.com/bolderic/p/7342511.html

你可能感兴趣的文章
easyui时的时间格式yyyy-MM-dd与yyyy-MM-ddd HH:mm:ss
查看>>
专题:动态内存分配----基础概念篇
查看>>
Codeforces Round #426 (Div. 2) (A B C)
查看>>
The Most Simple Introduction to Hypothesis Testing
查看>>
UVA10791
查看>>
P2664 树上游戏
查看>>
jQuery 停止动画
查看>>
Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
查看>>
MyBatis Generator去掉生成的注解
查看>>
教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
查看>>
lua连续随机数
查看>>
checkstyle使用介绍
查看>>
history.js 一个无刷新就可改变浏览器栏地址的插件(不依赖jquery)
查看>>
会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!
查看>>
二维码图片生成
查看>>
在做操作系统实验的一些疑问
查看>>
Log4J日志配置详解
查看>>
NameNode 与 SecondaryNameNode 的工作机制
查看>>
Code obfuscation
查看>>
大厂资深面试官 带你破解Android高级面试
查看>>