HDU - 3533 BFS

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.
Input
For 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.
Output
If Little A can escape, print the minimum time required in seconds on a single line. Otherwise print “Bad luck!” without quotes.
Sample Input
4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 2 1 2 4
4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 1 1 2 4
Sample Output
9
Bad luck!

解题:其实就是一般的图的BFS,每一步的状态有三个(x,y,step)
用三维的数组标志。比较麻烦的是判断某一个点,在某个时刻是否会有炮弹。

BFS

int map[7][7];
int vis[7][7];
int dir[4][2] = { {1,0},{0,1},{0,-1},{-1,0} };

struct node
{
	int x, y;
	int step;
	node*last;

};

node* Bfs(int x, int y) {
	int i;
	memset(vis, 0, sizeof(vis));
	vis[0][0] = 1;
	queue<node *>q;
	node *s;
	s = (node *)malloc(sizeof(node));
	s->x = x;
	s->y = y;
	s->step = 0;
	s->last = NULL;
	q.push(s);

	while (!q.empty())
	{
		s = q.front();
		q.pop();
		if (s->x == 4 && s->y == 4)
		{
			return s;
		}
		for (int i = 0; i < 4; i++)
		{

			int stayx = s->x + dir[i][0];
			int stayy = s->y + dir[i][1];
			if (stayx < 0 || stayy < 0 || stayx>4 || stayy>4)continue;
			if (map[stayx][stayy])continue;
			if (vis[stayx][stayy])continue;


			vis[stayx][stayy] = 1;

			node *e;
			e = (node *)malloc(sizeof(node));
			e->x = stayx;
			e->y = stayy;
			e->step = s->step + 1;
			e->last = s;
			q.push(e);



		}


	}



	return NULL;

}

解题代码

#include<bits/stdc++.h>
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define out(x) cout<<x<<endl;
using namespace std;
const int maxn=105;
const int inf=9999999;

int n,m,k,d;
int dir[][2]={{0,1},{1,0},{-1,0},{0,-1},{0,0}};///对应的四个方向是ESNW,(0,0)表示在原地
struct castle{//炮台
    char dir;
    int t,v;
    void clear(){
        dir='*';t=v=0;
    }
    castle(){}
    castle(char dir,int t,int v){
        this->dir=dir;this->t=t;this->v=v;
    }
}castles[maxn][maxn];
bool vis[1005][maxn][maxn];//用int会超内存,int是4字节,bool是一个字节
struct node{//搜索的状态节点
    int x,y,step;
    node(){};
    node(int x,int y,int step){
        this->x=x;this->y=y;this->step=step;
    }
    bool operator<(const node& tt) const{
        return step>tt.step;
    }
};
bool pan(node s){
    if(s.x>=0&&s.x<=n&&s.y>=0&&s.y<=m)return true;
    return false;
}
bool isEnable(node s){///四个方向找最近的城堡,找出这个时刻这个位置是否有子弹。

    //printf("%d %d\n",s.x,s.y);
    if(castles[s.x][s.y].dir!='*')return false;
    ///-->E,表示往右看有没有炮台,有的话取第一个
    node tmp=s;
    castle cs;
    int time=s.step;
    while(pan(tmp)&&castles[tmp.x][tmp.y].dir=='*'){
        tmp.x+=dir[0][0];
        tmp.y+=dir[0][1];
    }
    if(pan(tmp))cs=castles[tmp.x][tmp.y];
    if(pan(tmp)&&cs.dir=='W'){
        int dis=tmp.y-s.y;
        if(dis%cs.v==0){
            if((time-dis/cs.v)>=0&&(time-dis/cs.v)%cs.t==0)
                return false;
        }
    }
    ///-->S
    tmp=s;
    while(pan(tmp)&&castles[tmp.x][tmp.y].dir=='*'){
        tmp.x+=dir[1][0];
        tmp.y+=dir[1][1];
    }
    if(pan(tmp))cs=castles[tmp.x][tmp.y];
    if(pan(tmp)&&cs.dir=='N'){
        int dis=tmp.x-s.x;
        if(dis%cs.v==0){
            if((time-dis/cs.v)>=0&&(time-dis/cs.v)%cs.t==0){
                return false;
            }
        }
    }
    ///-->N
    tmp=s;
    while(pan(tmp)&&castles[tmp.x][tmp.y].dir=='*'){
        tmp.x+=dir[2][0];
        tmp.y+=dir[2][1];
    }
    if(pan(tmp))cs=castles[tmp.x][tmp.y];
    if(pan(tmp)&&cs.dir=='S'){
        int dis=s.x-tmp.x;
        if(dis%cs.v==0){
            if((time-dis/cs.v)>=0&&(time-dis/cs.v)%cs.t==0){
                return false;
            }
        }
    }
    ///-->W
    tmp=s;
    while(pan(tmp)&&castles[tmp.x][tmp.y].dir=='*'){
        tmp.x+=dir[3][0];
        tmp.y+=dir[3][1];
    }
    if(pan(tmp))cs=castles[tmp.x][tmp.y];
    if(pan(tmp)&&cs.dir=='E'){
        int dis=s.y-tmp.y;
        if(dis%cs.v==0){
            if((time-dis/cs.v)>=0&&(time-dis/cs.v)%cs.t==0){
                return false;
            }
        }
    }
    return true;
}

int bfs(){
    cl(vis,false);
    priority_queue<node> q;
    q.push(node(0,0,0));
    vis[0][0][0]=true;
    while(!q.empty()){
        node s=q.top();q.pop();
        for(int i=0;i<5;i++){
            node tmp=s;
            tmp.x+=dir[i][0];
            tmp.y+=dir[i][1];
            tmp.step++;
            if(tmp.x==n&&tmp.y==m){
                if(castles[n][m].dir=='*')return tmp.step;
                else return -1;
            }
            if(tmp.step>d)return -1;
            if(pan(tmp)&&!vis[tmp.step][tmp.x][tmp.y]){
                if(isEnable(tmp)){
                    vis[tmp.step][tmp.x][tmp.y]=true;
                    q.push(tmp);
                }
            }
        }
    }
    return -1;
}
int main(){
    while(~scanf("%d%d%d%d",&n,&m,&k,&d)){

        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                castles[i][j].clear();
            }
        }

        for(int i=0;i<k;i++){
            char c[2];
            int t,v,x,y;
            scanf("%s%d%d%d%d",c,&t,&v,&x,&y);
            castles[x][y]=castle(c[0],t,v);
        }
        int ans=bfs();
        if(ans==-1){
            puts("Bad luck!");
        }
        else {
            printf("%d\n",ans);
        }
    }
    return 0;
}

这位大哥的 原文:https://blog.csdn.net/u013167299/article/details/47252973

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值