Find a way(两个BFS)

简介: Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally.

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.

SampleInput

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#

SampleOutput

66
88
66
题目大意就是说两个人要去同一个KFC碰头,问你最短的时间是多少,然后每走一步耗时11分钟。
没啥坑点,用二维数组记录步数就得了。
不过要注意,有些KFC可能走不过去,所以初始化的时候赋一个大值,找最小值,然后就是两次BFS就行了。

AC代码

  1 #include <iostream>
  2 #include <string>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <sstream>
  6 #include <iomanip>
  7 #include <map>
  8 #include <stack>
  9 #include <deque>
 10 #include <queue>
 11 #include <vector>
 12 #include <set>
 13 #include <list>
 14 #include <cstring>
 15 #include <cctype>
 16 #include <algorithm>
 17 #include <iterator>
 18 #include <cmath>
 19 #include <bitset>
 20 #include <ctime>
 21 #include <fstream>
 22 #include <limits.h>
 23 #include <numeric>
 24 
 25 using namespace std;
 26 
 27 #define F first
 28 #define S second
 29 #define mian main
 30 #define ture true
 31 
 32 #define MAXN 1000000+5
 33 #define MOD 1000000007
 34 #define PI (acos(-1.0))
 35 #define EPS 1e-6
 36 #define MMT(s) memset(s, 0, sizeof s)
 37 typedef unsigned long long ull;
 38 typedef long long ll;
 39 typedef double db;
 40 typedef long double ldb;
 41 typedef stringstream sstm;
 42 const int INF = 0x3f3f3f3f;
 43 
 44 char mp[205][205];
 45 int vis[205][205][2];
 46 int fx[4][2] = {0,1,0,-1,-1,0,1,0};
 47 int n,m,z;
 48 
 49 bool check(int x,int y){
 50     if(vis[x][y][z] == MAXN && x >= 0 && x < n && y >= 0 && y < m && mp[x][y] != '#')
 51         return true;
 52     return false;
 53 }
 54 
 55 void bfs(pair< int,int >s){
 56     vis[s.F][s.S][z] = 0;
 57     queue< pair< int,int > >q;
 58     q.push(s);
 59     while(!q.empty()){
 60         pair< int,int >tp = q.front();
 61         q.pop();
 62         for(int i = 0; i < 4; i++){
 63             int next_x = tp.F + fx[i][0];
 64             int next_y = tp.S + fx[i][1];
 65             if(check(next_x,next_y)){
 66                 vis[next_x][next_y][z] = vis[tp.F][tp.S][z] + 1;
 67                 q.push(make_pair(next_x,next_y));
 68             }
 69         }
 70     }
 71 }
 72 
 73 int main(){
 74     ios_base::sync_with_stdio(false);
 75     cout.tie(0);
 76     cin.tie(0);
 77 
 78     while(cin>>n>>m && n && m){
 79         MMT(mp);
 80         fill(&vis[0][0][0],&vis[0][0][0]+205*205*2,MAXN);
 81         for(int i = 0; i < n; i++){
 82             for(int j = 0; j < m; j++){
 83                 cin>>mp[i][j];
 84             }
 85         }
 86         for(int i = 0; i < n; i++)
 87             for(int j = 0; j < m; j++){
 88                 if(mp[i][j] == 'Y'){
 89                     z = 0;
 90                     bfs(make_pair(i,j));
 91                 }
 92                 if(mp[i][j] == 'M'){
 93                     z = 1;
 94                     bfs(make_pair(i,j));
 95                 }
 96             }
 97         int ans = MAXN;
 98         for(int i = 0; i < n; i++){
 99             for(int j = 0; j < m; j++){
100                 if(mp[i][j] == '@'){
101                     ans = min(ans,vis[i][j][0] + vis[i][j][1]);
102                     //11000055
103                 }
104             }
105         }
106         cout << ans*11 << endl;
107     }
108 
109     return 0;
110 }
View Code

 



目录
相关文章
|
存储 缓存 固态存储
这样优化Elasticsearch,显著提升查询速度
elasticsearch的搜索效率与多方面有关,例如系统资源、数据查询方式、数据索引方式等,本文从各方面讨论如何进行搜索速度的优化,提升查询的性能。
1796 0
|
消息中间件 数据挖掘 Kafka
揭秘大数据时代的极速王者!Flink:颠覆性流处理引擎,让实时数据分析燃爆你的想象力!
【8月更文挑战第29天】Apache Flink 是一个高性能的分布式流处理框架,适用于高吞吐量和低延迟的实时数据处理。它采用统一执行引擎处理有界和无界数据流,具备精确状态管理和灵活窗口操作等特性。Flink 支持毫秒级处理和广泛生态集成,但学习曲线较陡峭,社区相对较小。通过实时日志分析示例,我们展示了如何利用 Flink 从 Kafka 中读取数据并进行词频统计,体现了其强大功能和灵活性。
313 0
|
算法 Go Python
GitHub 上有哪些适合Python新手跟进的优质项目?
GitHub 上有哪些适合Python新手跟进的优质项目?
308 0
|
Java
HDOJ 1042 N!(大数阶乘JAVA)
HDOJ 1042 N!(大数阶乘JAVA)
147 0
|
2天前
|
云安全 数据采集 人工智能
古茗联名引爆全网,阿里云三层防护助力对抗黑产
阿里云三层校验+风险识别,为古茗每一杯奶茶保驾护航!
古茗联名引爆全网,阿里云三层防护助力对抗黑产
|
6天前
|
人工智能 中间件 API
AutoGen for .NET - 架构学习指南
《AutoGen for .NET 架构学习指南》系统解析微软多智能体框架,涵盖新旧双架构、核心设计、技术栈与实战路径,助你从入门到精通,构建分布式AI协同系统。
305 142
|
2天前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
404 0

热门文章

最新文章