【算法】Escape

The students of the HEU are maneuvering for their military training.
The red army and the blue army are at war today. The blue army
finds that Little A is the spy of the red army, so Little A has to
escape from the headquarters of the blue army to that of the red army.
The battle field is a rectangle of size m*n, and the headquarters of the
blue army and the red army are placed at (0, 0) and (m, n),
respectively, which means that Little A will go from (0, 0) to (m, n).
The picture below denotes the shape of the battle field and the notation
of directions that we will use later.

The blue army is eager to revenge, so it tries its best to kill
Little A during his escape. The blue army places many castles, which
will shoot to a fixed direction periodically. It costs Little A one unit
of energy per second, whether he moves or not. If he uses up all his
energy or gets shot at sometime, then he fails. Little A can move north,
south, east or west, one unit per second. Note he may stay at times in
order not to be shot.

To simplify the problem, let’s assume that Little A cannot stop in
the middle of a second. He will neither get shot nor block the bullet
during his move, which means that a bullet can only kill Little A at
positions with integer coordinates. Consider the example below. The
bullet moves from (0, 3) to (0, 0) at the speed of 3 units per second,
and Little A moves from (0, 0) to (0, 1) at the speed of 1 unit per
second. Then Little A is not killed. But if the bullet moves 2 units per
second in the above example, Little A will be killed at (0, 1).

Now, please tell Little A whether he can escape.

InputFor every test case, the first line has
four integers, m, n, k and d (2<=m, n<=100, 0<=k<=100, m+
n<=d<=1000). m and n are the size of the battle ground, k is the
number of castles and d is the units of energy Little A initially has.
The next k lines describe the castles each. Each line contains a
character c and four integers, t, v, x and y. Here c is ‘N’, ‘S’, ‘E’ or
‘W’ giving the direction to which the castle shoots, t is the period, v
is the velocity of the bullets shot (i.e. units passed per second), and
(x, y) is the location of the castle. Here we suppose that if a castle
is shot by other castles, it will block others’ shots but will NOT be
destroyed. And two bullets will pass each other without affecting their
directions and velocities.

All castles begin to shoot when Little A starts to escape.

Proceed to the end of file.

OutputIf Little A can escape, print the minimum time required in seconds on a single line. Otherwise print “Bad luck!” without quotes.


#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;

struct man{
    short x,y,t;
};

int n,m,k,energy;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
bool tmap[101][101][1001];//前两维代表坐标,后一维表时时间:意为每个位置的每个时刻有没子弹和碉堡(这里一定要用bool,用int会超内存)
bool vist[101][101][1001];//第几个时刻的位置走过否

int OK(int x,int y,int time)//返回1说明位位置的当前时间可以走,加入队列
{
    if(x>=0&&x<=n&&y>=0&&y<=m&&!tmap[x][y][time]&&n-x+m-y<=energy-time&&!vist[x][y][time])
    {
            vist[x][y][time]=true;  return 1;
    }
    return 0;
}
void bfs()
{
    queue<man>qMan;
    man pMan,tpMan;
    int time=0,x,y;

    pMan.x=0; pMan.y=0; pMan.t=0;
    if(OK(pMan.x,pMan.y,time))
        qMan.push(pMan);

    while(true)
    {
        time++;
        if(qMan.empty()||time>energy)
        {
            printf("Bad luck!\n"); return ;
        }
        while(!qMan.empty())
        {
            pMan=qMan.front();
            if(pMan.t>=time)
                break;
             qMan.pop();
             pMan.t=time;
            if(OK(pMan.x,pMan.y,time))//可以停在原地
                qMan.push(pMan);
            for(int e=0;e<4;e++)
            {
                tpMan=pMan;
                tpMan.x+=dir[e][0]; tpMan.y+=dir[e][1];
                if(OK(tpMan.x,tpMan.y,time))
                {
                    if(tpMan.x==n&&tpMan.y==m)
                    {
                        printf("%d\n",time); return ;
                    }
                    qMan.push(tpMan);
                }
            }
        }
    }
}
int main()
{
    char ss[3];
    int map[105][105];
    int tx,ty,x[105],y[105],T[105],v[105],d[105];
    while(scanf("%d%d%d%d",&n,&m,&k,&energy)>0)
    {
        for(int i=0;i<=n;i++)
            for(int j=0;j<=m;j++)
            for(int t=0;t<=energy;t++)
            tmap[i][j][t]=vist[i][j][t]=false;
        memset(map,0,sizeof(map));

        for(int i=0;i<k; i++)
        {
             scanf("%s%d%d%d%d",ss,&T[i],&v[i],&x[i],&y[i]);
                if(ss[0]==‘N‘) d[i]=0;
               else if(ss[0]==‘S‘) d[i]=1;
               else if(ss[0]==‘W‘) d[i]=2;
               else if(ss[0]==‘E‘) d[i]=3;
               map[x[i]][y[i]]=1;//碉堡位置
        }
        //计算每个时刻子弹和碉堡在地图的位置
        for(int i=0;i<k; i++)
        {
            for(int t=1;t<=energy; t++)//一开始当前碉堡发出的子弹的每个时间运行
            {
                 tmap[x[i]][y[i]][t]=true;//每个刻碉堡的位置
                //第i个碉堡一开始发的子弹运行了t时到达的位置
                 tx=x[i]+dir[d[i]][0]*v[i]*t;
                 ty=y[i]+dir[d[i]][1]*v[i]*t;

                 if(tx>=0&&tx<=n&&ty>=0&&ty<=m)//是否越界
                    for(int tt=t; tt<=energy; tt+=T[i])//第i个碉堡发出的子弹周期性的到达同一位置
                    if(!tmap[tx][ty][tt])
                    {
                        //判断从第i个碉堡位置发出的子弹能否到达位置(tx,ty)
                        int flag=0,xx=x[i],yy=y[i];
                        if(d[i]==0)
                        {
                            for( xx=x[i]-1; xx>=tx; xx--)
                                if(map[xx][yy])
                                    break;
                            if(xx<tx)  flag=1;
                        }
                        else if(d[i]==1)
                        {
                            for( xx=x[i]+1; xx<=tx; xx++)
                                if(map[xx][yy])
                                    break;
                            if(xx>tx)  flag=1;
                        }
                        else if(d[i]==2)
                        {
                            for(yy=y[i]-1; yy>=ty; yy--)
                                if(map[xx][yy])
                                break;
                            if(yy<ty) flag=1;
                        }
                        else
                        {
                            for(yy=y[i]+1; yy<=ty; yy++)
                                if(map[xx][yy])
                                break;
                            if(yy>ty) flag=1;
                        }

                        if(flag)//能到达,说明路过的地没有其他碉堡阻挡
                        tmap[tx][ty][tt]=true;
                    }
            }
        }
        bfs();
    }
}
时间: 2024-12-20 19:08:05

【算法】Escape的相关文章

hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 8001    Accepted Submission(s): 1758 Problem Description 2012 If this is the end of th

Memcache技术分享:介绍、使用、存储、算法、优化、命中率

原文地址:http://zhihuzeye.com/archives/2361 1.memcached 介绍 1.1 memcached 是什么? memcached 是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件.现在已成为mixi.hatena.Facebook.Vox.LiveJournal 等众多服务中提高Web应用扩展性的重要因素.许多Web 应用都将数据保存到RDBMS 中,应用服务器从中读取数据并在浏览器中

javascript 回溯寻路算法

最近一直在手游 caveboy escape(安卓上,不知道IOS上有没有,可以下来玩玩). 游戏规则是,在5x5的矩阵,从最下面的起点,每个颜色走三步,到达最上面的重点. 想写个js版本.碰到第一个问题就是,矩阵布局,寻路算法. 网上搜了下只有 PathFinding.js 带有的著名的 A*寻路法(自己百度) 源码: https://github.com/qiao/PathFinding.js DEMO: http://qiao.github.io/PathFinding.js/visual

HDU 3605 Escape (最大流)

Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description 2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do

C#常见算法题目

    //冒泡排序    public class bubblesorter    {        public void sort(int[] list)        {            int i, j, temp;            bool done = false;            j = 1;            while ((j < list.Length) && (!done))            {                don

Memcahce(MC)系列(一)Memcache介绍、使用、存储、算法、优化

关于memcache的安装,有兴趣的朋友请参考这篇文章:http://blog.csdn.net/xifeijian/article/details/22000173 1.memcached 介绍 1.1 memcached 是什么? memcached 是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件.现在已成为mixi.hatena.Facebook.Vox.LiveJournal 等众多服务中提高Web 应用扩展性的

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

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

HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧

前言 在编程过程中总结归纳出来的一种编程经验,从而形成的设计思想称为设计模式. 设计模式有23种.它适用于所有的编程语言. 常用的有创新型的设计模式:简单工厂.抽象工厂和单例模式:行为型的设计模式:模板设计模式.观察者模式和命令模式:结构性的设计模式:适配器设计模式.代理模式(静态和动态两种,典型的有在spring的AOP编程中使用)和装饰器设计模式. 正文 单例模式(singleton) 保证一个类在内存中只能创建一个实例. 1.实现步骤: 1)将构造器私有化,即使用private修饰构造器

一些简单的算法

一.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性. static int F1(int m) { int sum =0; bool flag =true; for (int i = 1; i <= m; i++) { if (flag) //一次是默认是True,下下也为True sum += i; else sum -= i; flag = !flag; } return sum; } //通过奇