【搜索】 HDU 3533 Escape BFS 预处理

要从0,0 点 跑到m,n点  路上会有k个堡垒发射子弹,有子弹的地方不能走,子弹打到别的堡垒就会消失,或者一直飞出边界(人不能经过堡垒

可以上下左右或者站着不动 每步都需要消耗能量  一共有eng个能量

先预处理出地图 用三维数组表示mp[x][y][time] time表示该时间的地图上储存不能走的点

然后就是普通BFS

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 66666;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 1101521204;
const int mod = 10000007;
int m,n,k,eng;
struct node
{
    int x,y,v,t,f;
}kp[102];
struct node1
{
    int x,y,step;
};
queue<node1>q;
int xx[5]={0,-1,1,0,0};
int yy[5]={0,0,0,-1,1};
bool vis[110][110][1009];
bool mp[110][110][1009];
bool point[110][110];
bool inmp(int x,int y)
{
    if(x<0||x>m||y<0||y>n) return false;
    return true;
}
int bfs(int x,int y)
{
    node1 front,rear;
    front.x=x,front.y=y,front.step=0;
    while(!q.empty()) q.pop();
    q.push(front);
    while(!q.empty())
    {
        front=q.front();
        front.step++;
        q.pop();
        for(int i=0;i<5;i++)
        {
            int dx=front.x+xx[i],dy=front.y+yy[i];
            if(inmp(dx,dy)&&!mp[dx][dy][front.step]&&!point[dx][dy]&&!vis[dx][dy][front.step])
            {
                vis[dx][dy][front.step]=true;
                if(dx==m&&dy==n) return front.step;//到达终点
                if(front.step+1>eng) continue;
                rear.x=dx,rear.y=dy,rear.step=front.step;
                q.push(rear);
            }
        }
    }
    return -1;
}
int main()
{
   // IN;
    while(scanf("%d%d%d%d",&m,&n,&k,&eng)!=EOF)
    {
        cler(mp,false);
        cler(vis,false);
        cler(point,false);
        for(int i=0;i<k;i++)
        {
            char c[3];
            scanf("%s%d%d%d%d",c,&kp[i].t,&kp[i].v,&kp[i].x,&kp[i].y);
            if(c[0]=='N') kp[i].f=1;
            else if(c[0]=='S') kp[i].f=2;
            else if(c[0]=='W') kp[i].f=3;
            else if(c[0]=='E') kp[i].f=4;
            point[kp[i].x][kp[i].y]=true;
        }
        for(int i=0;i<k;i++)
        {
            int dx=kp[i].x,dy=kp[i].y,v=kp[i].v,next=kp[i].f;
            for(int j=1;j<=eng;j++)
            {
                int flag=0;
                dx+=xx[next],dy+=yy[next];
                if(!inmp(dx,dy)) break;
                for(int l=0;l<v;l++)//路上有堡垒
                {
                    if(point[dx-xx[next]*l][dy-yy[next]*l])
                    {
                        flag=1;break;
                    }
                }
                if(flag) break;
                int x=j;
                while(x<=eng)
                {
                    mp[dx][dy][x]=true;//标记不能走
                    x+=kp[i].t;
                }
            }
        }
        int ans=bfs(0,0);
        if(ans==-1)
            printf("Bad luck!\n");
        else printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-08 07:02:10

【搜索】 HDU 3533 Escape BFS 预处理的相关文章

HDU 3533 Escape (BFS + 预处理)

Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 541    Accepted Submission(s): 141 Problem Description The students of the HEU are maneuvering for their military training. The red army

HDU 3533 Escape BFS搜索

题意:懒得说了 分析:开个no[100][100][1000]的bool类型的数组就行了,没啥可说的 #include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <queue> #include

HDU 3533 Escape(BFS+预处理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=100)座炮台,每座炮台都有各自的发射方向.发射周期和发射速度,每隔一段时间会发射一定速度的炮弹,人每秒可以选择停在原地或者往上下左右走,问是否能在时间d之内安全到达终点.如果可以,请输出最短时间. 解题思路:BFS+预处理,分为以下几点: ①预处理,用step[x][y][t]记录(x,y)在时间t是否被炮

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

HDU 1733 Escape(分层网络流)

HDU 1733 Escape 题目链接 题意:给定一个图,#是墙,@是出口,.可以行走,X是人,每个时间每个格子只能站一个人,问最少需要多少时间能让人全部撤离(从出口出去) 思路:网络流,把每个结点每秒当成一个结点,这样枚举时间,每多一秒就在原来的网络上直接加一层继续增广即可,注意考虑方向的时候,要考虑上原地不动 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorit

cdoj 1380 Xiper的奇妙历险(2) [八数码问题 bfs + 预处理]

快要NOIP 2016 了,现在已经停课集训了.计划用10天来复习以前学习过的所有内容.首先就是搜索. 八数码是一道很经典的搜索题,普通的bfs就可求出.为了优化效率,我曾经用过康托展开来优化空间,甚至还用过A*来优化时间.不过这道题懒得写了,就一个普普通通的bfs,再加上一个stl 的map就水过了. 首先题目要求有多达10000组数据,依次搜索肯定是不行的,我试过用A*来写,第2组数据就会T掉,所以我们考虑用一个预处理.从末尾状态搜索所有可行的状态,并用一个map来存储答案.然后就很好写了.

HDU - 1428【bfs+记忆化】

点击查看详情--<IJCAI 2017 口碑商家客流量预测大赛> 漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4205    Accepted Submission(s): 1317 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在电脑边,缺乏运动.他决定充分利用每

HDU 3605 Escape(最大流+缩点转换)

http://acm.hdu.edu.cn/showproblem.php?pid=3605 题目很简单,要求的就是最后能搬到星球上去的人的个数.刚开始看到,知道是最大流,就把人和星球都设为点,能生存就连线,权值为1,最后建立超级源点和超级汇点.求出最大流量即可.先是RE,开大数组后TLE.仔细算了,光光人到星球的便就可达到100w了,超时的概率太大了.后来找了解题报告,知道了缩点这一说,因为星球个数m最大只有10个,所以每个人最多只有1024种情况,把这每一种情况设为点(这里很抽象),将之与符

HDU 3605 Escape【二分图多重匹配】

题意: 有n个人去m个星球  告诉你每个人想去哪些星球和每个星球最多容纳多少人,问能不能让所有人都满足 分析: 二分图多重匹配 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 100005; 8 const int maxm = 15; 9 10