HDU 2612 Find a way 题解
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25379 Accepted Submission(s): 8243
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
//Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y,step;
};
queue<node> q;
int n,k,vis[][];
char map[][];
int dir[][]={{,},{-,},{,-},{,}};//四个方向
int bfs(int a,int b,int r1,int r2)
{
while(!q.empty())
q.pop();
int i;
q.push(node{a,b,});
vis[a][b]=;
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==r1&&t.y==r2)
return t.step;
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<n&&dy<k&&!vis[dx][dy]&&map[dx][dy]!='#')//正常搜索
{
vis[dx][dy]=;
q.push(node{dx,dy,t.step+});
}
}
}
return MMAX;//如果搜不到就返回一个不能使ans改变的值
}
int main()
{
int a1,b1,a2,b2,r1[],r2[];
while(scanf("%d%d",&n,&k)!=EOF)
{
int t=,ans=MMAX,temp=;
for(int i=;i<n;i++)
{
scanf("%s",&map[i]);
for(int j=;j<k;j++)
{
if(map[i][j]=='Y')
{
a1=i;
b1=j;
}
if(map[i][j]=='M')//求出两个点的位置作为起点
{
a2=i;
b2=j;
}
if(map[i][j]=='@')//如果是KFC放到数组里面
{
r1[t]=i;
r2[t]=j;
t++;
}
}
}
for(int i=;i<t;i++)//寻找能到的KFC的最小值
{
temp=;
memset(vis,,sizeof(vis));
temp+=bfs(a1,b1,r1[i],r2[i]);
memset(vis,,sizeof(vis));
temp+=bfs(a2,b2,r1[i],r2[i]);
ans=min(ans,temp);
}
printf("%d\n",ans*);
}
return ;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
乍一看这个代码似乎还很有道理
然而

但是为什么会T呢
原因其实很简单
就是因为bfs算法时间复杂度较高
如果多次进行bfs的话
铁定超时
此处一个KFC就进行一次搜索
导致超时
于是笔者想出了一个较为不错的方法
只进行一次bfs
记录下所能到达的位置所需的步数
这样只有两次深搜即可
但是这里要注意一个小小的bug
就是KFC可能到不了
笔者因为这个WA了几次的
下面附上我精确注释
谁都能看懂的代码
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y;
}; int n,k,vis[][];
char map[][];
int dir[][]={{,},{-,},{,-},{,}};//四个方向
void bfs(int a,int b,int step[][])//暴力枚举能到的地方各点的步数
{
int i;
queue<node> q;
q.push(node{a,b});
vis[a][b]=;
while(!q.empty())
{
node t=q.front();
q.pop();
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<n&&dy<k&&!vis[dx][dy]&&map[dx][dy]!='#'&&!vis[dx][dy])//基本搜索
{
vis[dx][dy]=;
step[dx][dy]=step[t.x][t.y]+;
q.push(node{dx,dy});
}
}
}
}
int main()
{
int a1,b1,a2,b2,step1[][],step2[][];
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(step1,,sizeof(step1));
memset(step2,,sizeof(step2));
int t=,ans=MMAX;
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
for(int j=;j<k;j++)
{
if(map[i][j]=='Y')
{
a1=i;
b1=j;
}
if(map[i][j]=='M')//把两个点记录下来
{
a2=i;
b2=j;
}
}
}
memset(vis,,sizeof(vis));
bfs(a1,b1,step1);
memset(vis,,sizeof(vis));
bfs(a2,b2,step2);
for(int i=;i<n;i++)//遍历所有元素,使得能走到KFC且为最小
{
for(int j=;j<k;j++)
{
if(map[i][j]=='@'&&step1[i][j]&&step2[i][j]&&(step1[i][j]+step2[i][j]<ans))//注意此处step为0代表KFC到不了
ans=step1[i][j]+step2[i][j];
}
}
printf("%d\n",ans*);
}
return ;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
此时能够AC 时间为31ms

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Note:需要注意下bfs的使用次数即可
2018-11-16 00:25:12 Author:LanceYu
HDU 2612 Find a way 题解的更多相关文章
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
- HDU 2612 Find a way(双向bfs)
题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...
- 题解报告:hdu 2612 Find a way(双bfs)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- HDU 2612 - Find a way - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...
- hdu 2612 Find a way
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Description Pass a year learning in H ...
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- (广搜) Find a way -- hdu -- 2612
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others) ...
随机推荐
- shell编程(1)
shell编程(1) 杨乾成 2017301500302 一.尝试ping 题目第一项要求是检验自己主机所在网段有多少主机连通.于是我写的shell程序如下: #!/bin/bash i=; coun ...
- 三层交换机RIP动态路由实验
一. 实验目的 1. 掌握三层交换机之间通过RIP协议实现网段互通的配置方法. 2. 理解动态实现方式与静态方式的不同 二. 应用环境 当两台三层交换机级联时,为了保证每台交换机上所连接的 ...
- RaxML使用
1.下载 https://github.com/stamatak/standard-RAxML 2.How many Threads shall I use? 重要的是要知道,RAxML PThrea ...
- 调试经验分享-让自己的电脑充当WI-Fi模块,用来抓取连接Wi-Fi模块APP上的通信数据
需求 手头有了厂家的APP和Wi-Fi模块 在已经知道APP是通过TCP连接Wi-Fi模块(8266), 同时也知道了连接的端口号的 情况下如何知道厂家的APP发送给Wi-Fi模块的数据 打开自己的笔 ...
- 教你查阅Java API 英文文档(JDK 11)
JAVA Document:https://docs.oracle.com/en/java/javase/11/ 然后找到“Specifications”并点击 API Documentation 比 ...
- 责任链模式Scala的7种实现
责任链模式是经典的GoF 23种设计模式之一,也许你已经了解这种模式.不管你是否熟悉,建议读者在阅读本文之前,不妨先思考下面三个问题: (1) 如何用多种风格迥异的编程范式来实现责任链模式? (2) ...
- 第30课 线程同步(std::condition_variable)
一. 条件变量 (一)条件变量概述 多线程访问一个共享资源(或称临界区),不仅需要用互斥锁实现独享访问避免并发错误,在获得互斥锁进入临界区后,还需检查特定条件是否成立.当某个线程修改测试条件后,将通知 ...
- Qt Quick 组件与动态对象
博客24## 一.Components(组件) Component 是由 Qt 框架或开发者封装好的.只暴露了必要接口的 QML 类型,可以重复利用.一个 QML 组件就像一个黑盒子,它通过属性.信号 ...
- python 爬取媒体文件(无防火墙)
#coding = utf-8 import requests import pandas as pd import os,time root_path = './根目录/' input_file = ...
- 说一说switch关键字的奥秘
Switch语法 switch作为Java内置关键字,却在项目中真正使用的比较少.关于switch,还是有那么一些奥秘的. 要什么switch,我有if-else 确实,项目中使用switch比较少的 ...