之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了。我队友告诉我应该打个表,这个方法确实不错。因为'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,防止超时是关键的更多相关文章

  1. HDU 2612 - Find a way - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...

  2. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  3. HDU 2612 (2次BFS,有点小细节)

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

  4. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  5. BFS(最短路) HDU 2612 Find a way

    题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...

  6. 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 ...

  7. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  8. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  9. 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,最先 ...

随机推荐

  1. 素数槽csuoj

    超时代码: #include <iostream> using namespace std;//写一个函数判断是否是素数bool isPrime(int num){int i=2;//co ...

  2. how to use the curses library in unix?

    In linux, you can use the ncurses library to use the terminal as a text buffer: move the cursor arou ...

  3. Nginx配置性能优化与压力测试webbench【转】

    这一篇我们来说Nginx配置性能优化与压力测试webbench. 基本的 (优化过的)配置 我们将修改的唯一文件是nginx.conf,其中包含Nginx不同模块的所有设置.你应该能够在服务器的/et ...

  4. Linux系统查看有几块硬盘

    使用df命令即可查看.df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文件系统被挂载的位置: 示例:[root@localhost ~]# d ...

  5. c语言库函数

    #include <stdio.h> #define LENGTH 100 main() { FILE *fd; char str[LENGTH]; fd = fopen("he ...

  6. 导出Excel数据

    先要导入jxl架包,其中的abc.xls为测试Excel,具体代码如下,仅供参考: import java.io.File; import java.io.FileInputStream; impor ...

  7. GCD is Funny

    GCD is Funny Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Pro ...

  8. ubuntu12.04的NFS配置

    安装nfs: #sudo apt-get install nfs-kernel-server ubuntu12.04中的已经是最新版本了,无需安装 打开/etc/exports文件,在末尾加入: /h ...

  9. 给Notepad++ 加带图标右键菜单

    给Notepad++ 加带图标右键菜单 方式一: 拷贝以下代码建立一个reg文件,替换相关路径,保存,双击运行加入注册表 Windows Registry Editor Version 5.00 [H ...

  10. 命令行调试smali

    am start -D -n com.agilebits.onepassword/.activity.LoginActivity ps | grep passu0_a126 1107 217 5107 ...