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,最先 ...
随机推荐
- ADPCM编码和解码
原文:http://www.znmcu.cn/znx_51_alltest_shell_fj_adpcm1.html ADPCM音频解码,其实放在这里有些不太合适. 在编写ZN-X开发板整板测试程序的 ...
- HttpClient的get和post方式提交数据的使用
/** * Http工具类 */ public class HttpUtil { // 创建HttpClient对象 public static HttpClient httpClient = new ...
- VNC VIEWER的使用集锦
关于颜色深度的问题, 今天用VNC Viewser ,连上去之后,发现色彩可能只有8或者16位 然后修改了 sever的depth,也不好使, 于是,就修改了 client的 colourlevel ...
- 如何查找僵尸进程并Kill之,杀不掉的要查看父进程并杀之
转自:如何查找僵尸进程并Kill之,杀不掉的要查看父进程并杀之 用ps和grep命令寻找僵尸进程#ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]'命令注解:-A ...
- akka 入门
http://blog.csdn.net/thkhxm/article/details/40182835 1.首先安装akka的相关包-- http://akka.io/downloads/2.导入依 ...
- dip2px
package com.itheima.zhbj.utils; import android.content.Context; public class DensityUtils { public s ...
- arm的编译器里已经有C标准库的lib包了,android为啥还要自己再实现呢
arm的编译器里已经有C标准库的lib包了,android为啥还要自己再实现呢 google自己搞的bionic libc来替代glibc想来是有原因的,本来glibc也是lgpl,应该也没有版权问题 ...
- Linq分组,linq方法分组
Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: 01.public class ...
- 怎样让pl sql developer 界面视图复位
tools->preferences->user interface->appearance->reset docking工具-首选项-用户界面-外观-复位停放
- API CLOUD 快捷键
常用快捷键有:Ctrl+Z:撤销Ctrl+N:创建项目或文件Ctrl+Shift+F:代码格式化(这个经常用,可以美化代码,也可以通过这个检查代码是否出错)Ctrl+/ :注释和反注释Alt+/:强制 ...