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

    今天在看苏州通的代码,里面有个引导的代码,涉及到viewPager的切换动画: DepthPageTransformer : package com.soyoungboy.guide; import ...

  2. Android Studio 项目结构

    在Android Studio中创建项目后,会出现 activity_main.xml 和 MainActivity.java 两个文件 其中  Java文件定义了一个Activity,当应用运行时, ...

  3. hud 2549 壮志难酬

    Problem Description 话说MCA山上各路豪杰均出山抗敌,去年曾在江湖威名显赫的,江湖人称<万军中取上将首级舍我其谁>的甘露也不甘示弱,“天将降大任于斯人也,必先劳其筋骨, ...

  4. html css相对单位

    p{ background:grey; color:white; font-size:0.5in; height:2em; width:50%; } height:2em意思就是屏幕上显示的的高度为字 ...

  5. CentOS7 PostgreSQL 主从配置( 三)

    postgres 主备切换 主备查看 方法 ps -ef | grep wal (主库 sender)postgres 27873 27864 0 5月06 ? 00:00:10 postgres: ...

  6. centos中apache-tomcat的配置

    在centos中配置Apache-toncat需要先安装jdk,前面文章已经写了怎么配置jdk,这里略过. 首先到官网下载好Apache-tomcat安装包,我这里下载的是apache-tomcat- ...

  7. 数据库连接池c3p0的设置

    spring-hibernate.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  8. 后台数据download成excel的方法(controller/action)

    jsp页面端 <a href="/portal/server/importExec" title="Data Download"> <img ...

  9. project文件问题

    到编译文件中看看,红色的删除,坤哥这样做的 .

  10. SpringMVC redirect乱码问题

    转:http://blog.csdn.net/xubo_zhang/article/details/8239725 spring redirect 用spring redirect中文会乱码:如下示例 ...