hdu2612——Find a way
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.
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
简单的BFS 对两个点bfs就行
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>using namespace std;int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
char mat[205][205];
bool vis1[205][205];
bool vis2[205][205];
int ans1[205][205];
int ans2[205][205];struct node
{int x,y;int t;
}temp1, temp2;
queue< node >qu;
int n, m;bool is_legal(int x, int y)
{if( x < 0 || x >= n || y < 0 || y >= m || mat[x][y] == '#')return false;return true;
}int bfs1(int sx, int sy)
{memset( vis1, 0, sizeof(vis1) );while( !qu.empty() )qu.pop();temp1.x = sx;temp1.y = sy;temp1.t = 0;vis1[temp1.x][temp1.y] = 1;ans1[temp1.x][temp1.y] = 0;qu.push(temp1);while( !qu.empty() ){temp1 = qu.front();qu.pop(); for(int i = 0; i < 4; i ++){temp2.x = temp1.x + dir[i][0];temp2.y = temp1.y + dir[i][1];if( !is_legal(temp2.x, temp2.y) )continue;if(vis1[temp2.x][temp2.y] == 1)continue;vis1[temp2.x][temp2.y] = 1;temp2.t = temp1.t + 11;ans1[temp2.x][temp2.y] = temp2.t;qu.push(temp2);}}
}int bfs2(int sx, int sy)
{memset( vis2, 0, sizeof(vis1) );while( !qu.empty() )qu.pop();temp1.x = sx;temp1.y = sy;temp1.t = 0;vis2[temp1.x][temp1.y] = 1;ans2[temp1.x][temp1.y] = 0;qu.push(temp1);while( !qu.empty() ){temp1 = qu.front();qu.pop(); for(int i = 0; i < 4; i ++){temp2.x = temp1.x + dir[i][0];temp2.y = temp1.y + dir[i][1];if( !is_legal(temp2.x, temp2.y) )continue;if(vis2[temp2.x][temp2.y] == 1)continue;vis2[temp2.x][temp2.y] = 1;temp2.t = temp1.t + 11;ans2[temp2.x][temp2.y] = temp2.t;qu.push(temp2);}}
}int main()
{while( ~scanf("%d%d", &n, &m) ){int x1, y1, x2, y2, min_ans = 0x3f3f3f3f;for(int i = 0; i < n; i ++){scanf("%s", mat[i]);for(int j = 0; j < m; j ++){if(mat[i][j] == 'Y'){x1 = i;y1 = j;}else if(mat[i][j] =='M'){x2 = i;y2 = j;}}}bfs1(x1,y1);bfs2(x2,y2);for(int i = 0; i < n; i ++)for(int j = 0; j < m; j ++){if(mat[i][j] == '@' && vis1[i][j] && vis2[i][j]){min_ans = min(min_ans, ans1[i][j] + ans2[i][j]);}}printf("%d\n", min_ans);}return 0;
}
hdu2612——Find a way相关推荐
- HDU2612 Find a Way BFS
题意: yifenfei和merceki要去KFC聚会,给出一个地方的地图,n*m,有若干个个KFC,然后他们每走一步需要11分钟(注意:这里时间不能重叠的,比如yi走了一步,me也是走了一步,则一共 ...
- HDU-2612 Find a way
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- HDU2612(BFS算法)
Problem Descrption Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Le ...
- Find a way hdu2612
Find a way Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- Hdu2612 Find a way
Find a way Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- 22.11.27补卡 HDU-2612 bfs
这题dfs没法写, 因为dfs没法处理最优解的情况 分别记录每个人走每一个kfc的最短路径 然后一个双重循环搜最小值就好了 初始化记得初始化为最大值, 不然有可能影响最min取值 ps: 这题我用st ...
- 【HDU - 2612】Find a way(bfs)
-->Find a way 直接上Chinese Descriptions: hsj和lsh最近迷上了pokemon go的游戏.在双十一大物期中考试来临之前,他们想抓一只稀有土拨鼠来攒攒人品 ...
- ACM训练计划建议(转)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
- ACM的分类训练题集
1.数论 大概有素数测试(筛法),扩展欧几里得算法,同余模运算,高斯消元,中国剩余定理,莫比乌斯反演等等. 我不擅长这方面(数学烂,还好后期团队里有两位数学大神),不发表评论. 推荐题目: 同余模运算 ...
最新文章
- PAT:1053. Path of Equal Weight (30) AC
- 6万人砍不下来一部拼多多手机,背后原来是这个原因。
- 2019编译ffepeg vs_如何在windows10下使用vs2017编译最新版本的FFmpeg和ffplay
- 计算机的网络与结构,计算机结构与组成29-网络.ppt
- android inset 标签,android – 有几个WindowInsets?
- PCL综述—三维图像处理
- OpenCV-实现直方图均衡化(对比cv::equalizeHist)
- java sdk他edk de区别_最低SDK版本/目标SDK版本与编译SDK版本之间有什么区别?
- 深度解析copy与strong的区别
- Python科学绘图 南丁格尔图/玫瑰图
- 本科有计算机应用吗,计算机应用专业自考本科
- 10-16 C1-2新增订单统计信息 (20 分)
- Python爬虫入门教程【11】:半次元COS图爬取
- tenacity 报错_Python Tenacity 实现重试机制
- 对于“色盲悖论”问题的理解
- 苹果icloud登录_如何在Windows电脑上使用苹果iCloud服务?
- mysql中unl是什么健_UNL类图关系全面剖析
- 使用echarts生成漂亮的3D地图
- QQ自动强制加好友代码
- SVN checkout报错The XML response contains invalid XML
热门文章
- java新建jframe_如何在Swing java中创建JFrame模型
- 尚未提交线上版本_开发微信小程序如果显示尚未提交线上版本该怎么办
- Qt笔记(三十七)之解决QWebSocket收发中文乱码问题
- Salesforce中jquery ui中的autoComplete实现自动联想
- 【性能优化】404- 从 12.67s到1.06s 性能优化实战
- 以切身体验告诉大家,爱护好自己的牙齿
- 建筑材料行业 | 官网数字化升级案例分析合集
- 一文说清丨科学认识幽门螺旋杆菌
- HOOK(易语言高级部分)
- “ChatGPT之父”勇闯币圈!数十亿人的空投计划,只需交出你的虹膜?