HDU 2612 Find a way BFS,防止超时是关键
之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了。我队友告诉我应该打个表,这个方法确实不错。因为'Y'和'M'是唯一的,我通过这两个点分别广搜一次,对所有到达‘@’的情况打个表,这样就节省了很多时间。具体看代码吧。
#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1005 using namespace std; int n,m,vis[MAX][MAX],v[][]= {{,},{,-},{,},{-,}},Time[MAX][MAX],b[MAX][MAX];
char Map[MAX][MAX]; struct node
{
int x,y,step;
}; void BFS(int sx,int sy)
{
memset(vis,,sizeof(vis));
queue<node>Q;
node a,next;
a.x=sx;
a.y=sy;
a.step=;
Q.push(a);
vis[sx][sy]=;
while(!Q.empty())
{
a=Q.front();
Q.pop(); if(Map[a.x][a.y]=='@' && !b[a.x][a.y])//数组b记录是否到达过这个‘@’
{
b[a.x][a.y]=;
Time[a.x][a.y]+=a.step*;
next.x=sx;
next.y=sy;
next.step=;
Q.push(next);
} for(int i=; i<; i++)
{
next.x=a.x+v[i][];
next.y=a.y+v[i][]; if(next.x>= && next.x<n && next.y>= && next.y<m && Map[next.x][next.y]!='#' && !vis[next.x][next.y])
{
vis[next.x][next.y]=;
next.step=a.step+;
Q.push(next);
}
}
}
} int main()
{
int i,j,ans,x1,x2,y1,y2;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=INF;
for(i=; i<n; i++)
{
scanf("%s",Map[i]);
} for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
if(Map[i][j]=='Y')
{
x1=i;
y1=j;
} if(Map[i][j]=='M')
{
x2=i;
y2=j;
}
}
} memset(b,,sizeof(b));
memset(Time,,sizeof(Time));
BFS(x1,y1);//对‘Y’到达‘@’进行广搜
memset(b,,sizeof(b));
BFS(x2,y2);对‘M’到达‘@’进行广搜 for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(Time[i][j]!=)
ans=min(ans,Time[i][j]);
}
} printf("%d\n",ans);
}
return ;
}
HDU 2612 Find a way BFS,防止超时是关键的更多相关文章
- 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 bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- HDU 2612 (2次BFS,有点小细节)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- 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 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
随机推荐
- 照着例子学习protobuf-python
以下是照着python操作protobuf进行的protobuf-python的学习笔记: 首先是protobuf的下载与安装: 1 由于google被墙,所以去github上面搜索了一下protob ...
- android代码格式化方法小结
转载:http://blog.csdn.net/androidzhaoxiaogang/article/details/7692526 Download the android-formatting. ...
- Spring 框架理论基础
一. IOC 控制反转 概念解释:当我需要一个资源时,容器已经帮我准备好,我只需要接受就可以. // 加载 IOC 容器 ApplicationContext ac = new ClassPathXm ...
- Chapter 1 First Sight——25
"They are… very nice-looking." I struggled with the conspicuous understatement. 他们都很好看,我与轻 ...
- OPENWRT make menuconfig错误之一
1.make menuconfig rm: cannot remove `tmp/.host.mk': Permission denied 退到trunk上级目录sudo chown -R 777 t ...
- 面向方面编程(AOP)
简介 如果你很熟悉面向方面编程(AOP),你就会知道给代码增加"切面"可以使代码更清晰并且具有可维护性.但是AOP通常都依赖于第三方类库或者硬编码的.net特性来工作.虽然这些实现 ...
- marzullo's algorithm
given several intervals, how to find a interval which is a intersect of the most number of the given ...
- 查询MySQL中某个数据库中有多少张表
SELECT COUNT(*) TABLES, table_schema FROM information_schema.TABLES WHERE table_schema = '数据库' GRO ...
- psql 命令总结
1 登录数据库 Connection options: -h, --host=HOSTNAME database server host or socket directory (default: & ...
- Oracle删除死锁进程的方法
本文实例讲述了Oracle删除死锁进程的方法.分享给大家供大家参考.具体如下: 步骤1:用以下SQL查看进程列表,判断出被锁定的表 复制代码代码如下: SELECT dob.OBJECT_NAME T ...