欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 3533 Escape(BFS+模拟)两种思路

程序员文章站 2022-03-25 17:02:08
...

Escape

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2400    Accepted Submission(s): 693


Problem Description
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.

HDU 3533 Escape(BFS+模拟)两种思路


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 10N 1 1 1 1W 1 1 3 2W 2 1 2 44 4 3 10N 1 1 1 1W 1 1 3 2W 1 1 2 4
 

Sample Output

9Bad luck!

 


中文题意:

    

一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y
问这个人能不能安全到达终点
要求: 
1.人不能到达炮塔所在的坐标
2.炮塔会挡住子弹
3.途中遇到子弹是安全的,但是人如果停在这个坐标,而子弹也刚好到这个坐标,人就被射死
4.人可以选择停止不动
思路:其实不难,我们只需要看当人位于某个点的时候,其四个方向是否有炮塔,这个炮塔是都向人的方向射击,然后再看子弹是否刚好位于这个坐标即可。
而标记的话,vis[x][y][time],对于time时刻,人位于x,y的情况只需要访问一次,这是唯一的

两种思路:

    第一种:根据自己的位置,向四个方向找最近的堡垒,然后判断是否会被射杀

    第二种:遍历全部堡垒数,看子弹是否会被阻挡,在看是否能够射击到。(这种比较快,为何???)


第一种

#include<bits/stdc++.h>

using namespace std;
int m,n,k,d;
bool maps[110][110];			//堡垒标记 
bool vis[110][110][1010];		//访问的点坐标还有时间【x】【y】【time】 

int dir[5][2]={{1,0},{-1,0},{0,1},{0,-1},{0,0}};  //上下左右还有停止不动 
struct node{
	char d;		//方向 
	int t,v;	//发射周期,子弹速度,堡垒坐标 
}castle[110][110];

struct node1{	 
	int x,y;
	int step;
};

bool judge1(int x,int y,int time){
	//向上,堡垒向下射击,并且判断是否会被射中 
	for(int i=x-1;i>=0;i--){
		if(maps[i][y]){
			if(castle[i][y].d=='S' && time>=1.0*(x-i)/castle[i][y].v){
				double c=1.0*(x-i)/castle[i][y].v;
				if(time==c) return false;
				while(time>=c){
					if(time==c) return false;
					c+=castle[i][y].t;
				}
			}
			break;					//距离最近的子弹才不会被别的堡垒挡道 
		}
	} 
	//向下,堡垒向上射击 
	for(int i=x+1;i<=m;i++){
		if(maps[i][y]){
		  	if(castle[i][y].d=='N' && time>=1.0*(i-x)/castle[i][y].v){
				double c=1.0*(i-x)/castle[i][y].v;
				if(time==c) return false;
				while(time>=c){
					if(time==c) return false;
					c+=castle[i][y].t;
				}
			}
			break;
		}
	}
	//向左,堡垒向右射击
	for(int i=y-1;i>=0;i--){
		if(maps[x][i]){
			if(castle[x][i].d=='E' && time>=1.0*(y-i)/castle[x][i].v){
				double c=1.0*(y-i)/castle[x][i].v;
				if(time==c) return false;
				while(time>=c){
					if(time==c) return false;
					c+=castle[x][i].t;
				}
			}
			break;
		}
	} 
	//向右,堡垒向左射击
	for(int i=y+1;i<=n;i++){
		if(maps[x][i]){
			if(castle[x][i].d=='W' && time>=1.0*(i-y)/castle[x][i].v){
				double c=1.0*(i-y)/castle[x][i].v;
				if(time==c) return false;
				while(time>=c){
					if(time==c) return false;
					c+=castle[x][i].t;
				}
			}
			break;
		}
	}
	return true;
}

//判断这个时候会不会跟子弹撞上 
bool judge(int x,int y,int time){
	if(x<0 || x>m || y<0 || y>n || time>d || maps[x][y]){		//在规定时间内要逃出(time<d) 
		return false;
	}
	if(judge1(x,y,time)){
		
		return true;
	}
	return false;
}
//BFS模板 
void BFS(){
	memset(vis,0,sizeof vis);
	
	queue<node1> s;
	s.push(node1{0,0,0});
	vis[0][0][0]=1;
	while(!s.empty()){
		node1 cnt = s.front();
		s.pop();
		if(cnt.step>d) printf("Bad luck!\n");
		if(cnt.x==m && cnt.y==n && cnt.step<=d){
			printf("%d\n",cnt.step);
			return;
		}
		for(int i=0;i<5;i++){
			int x=cnt.x+dir[i][0];
			int y=cnt.y+dir[i][1];
			int time=cnt.step+1;
			if(judge(x,y,time) && !vis[x][y][time]){
				vis[x][y][time]=1;
				s.push(node1{x,y,time});
			}
		}
	}
	printf("Bad luck!\n");
}

int main(){
	char a;
	int b,c,e,f;
	while(~scanf("%d%d%d%d",&m,&n,&k,&d)){
		memset(maps,0,sizeof maps);
		for(int i=0;i<k;i++){
			getchar();
			scanf("%c%d%d%d%d",&a,&b,&c,&e,&f);
			castle[e][f].d=a,castle[e][f].t=b,castle[e][f].v=c;
			maps[e][f]=1;
		}
		BFS();
	}
} 


第二种:

#include<bits/stdc++.h>

using namespace std;
int m,n,k,d;
bool maps[110][110];
bool vis[110][110][1010];

int dir[5][2]={{1,0},{-1,0},{0,1},{0,-1},{0,0}};
struct node{
	char d;
	int t,v,x,y;	//发射周期,子弹速度,堡垒坐标 
}castle[110];

struct node1{
	int x,y;
	int step;
};

bool judge1(int x,int y,int time){
	for(int i=0;i<k;i++){
		//同一x轴上,方向向W的射击 
		if(x==castle[i].x && castle[i].y>y && castle[i].d=='W' &&time>=1.0*(castle[i].y-y)/castle[i].v){
			//查看是否会被挡住 
			int f=0;
			for(int j=castle[i].y-1;j>y;j--){
				if(maps[x][j]){
					f=1;
					break;
				}
			}
			if(f) continue;
			float c=1.0*(castle[i].y-y)/castle[i].v;
			if(c==time) return false;
			while(time>=c){
				if(c==time) return false;
				c+=castle[i].t;
			}
			continue;
		}
		//同x轴,方向向E 
		if(x==castle[i].x && castle[i].y<y && castle[i].d=='E' && time>=1.0*(y-castle[i].y)/castle[i].v){
			int f=0;
			for(int j=castle[i].y+1;j<y;j++){
				if(maps[x][j]){
					f=1;
					break;
				}
			}
			if(f) continue;
			double c=1.0*(y-castle[i].y)/castle[i].v;
			if(c==time) return false;
			while(time>=c){
				if(c==time) return false;
				c+=castle[i].t;
			}
			continue;
		}
		if(y==castle[i].y && castle[i].x<x && castle[i].d=='S' && time>=1.0*(x-castle[i].x)/castle[i].v){
			int f=0;
			for(int j=castle[i].x+1;j<x;j++){
				if(maps[j][y]){
					f=1;
					break; 
				}
			}
			if(f) continue;
			double c=1.0*(x-castle[i].x)/castle[i].v;
			if(c==time) return false;
			while(time>=c){
				if(c==time) return false;
				c+=castle[i].t;
			}
			continue;
		}
		if(y==castle[i].y && castle[i].x>x && castle[i].d=='N' && time>=1.0*(castle[i].x-x)/castle[i].v){
			int f=0;
			for(int j=castle[i].x-1;j>x;j--){
				if(maps[j][y]){
					f=1;
					break;
				}
			}
			if(f) continue;
			double c=1.0*(castle[i].x-x)/castle[i].v;
			if(c==time) return false;
			while(time>=c){
				if(c==time) return false;
				c+=castle[i].t;
			}
			continue;
		}
	}
	return true;
}

//判断这个时候会不会跟子弹撞上 
bool judge(int x,int y,int time){
	if(x<0 || x>m || y<0 || y>n || time>d || maps[x][y]){
		return false;
	}
	if(judge1(x,y,time)){
		return true;
	}
	return false;
}

void BFS(){
	memset(vis,0,sizeof vis);
	queue<node1> s;
	s.push(node1{0,0,0});
	vis[0][0][0]=1;
	while(!s.empty()){
		node1 cnt = s.front();
		s.pop();
		if(cnt.x==m && cnt.y==n && cnt.step<=d){
			printf("%d\n",cnt.step);
			return;
		}
		if(cnt.step>d) printf("Bad luck!\n");
		for(int i=0;i<5;i++){
			int x=cnt.x+dir[i][0];
			int y=cnt.y+dir[i][1];
			int time=cnt.step+1;
            if(!vis[x][y][time]){
				if(judge(x,y,time)){
					vis[x][y][time]=1;
					s.push(node1{x,y,time});
				}
			}
		}
	}
	printf("Bad luck!\n");
}

int main(){
	while(~scanf("%d%d%d%d",&m,&n,&k,&d)){
		memset(maps,0,sizeof maps);
		for(int i=0;i<k;i++){
			getchar();
			scanf("%c%d%d%d%d",&castle[i].d,&castle[i].t,&castle[i].v,&castle[i].x,&castle[i].y);
			maps[castle[i].x][castle[i].y]=1;
		}
		BFS();
	}
}


HDU 3533 Escape(BFS+模拟)两种思路



相关标签: BFS