VJ -K - Escape

题目来源VJ -K - 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.
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!

分析题意思路: 题目就是讲要从(0, 0) 跑到 (n, m), 典型的bfs(), 仅仅是过程判断多了一些:
首先要标记, 在(x, y)处 time时间有没有走过, 防止回走, 其次, 就是要往四个方向判断有没有被炮中的危险, 于北考虑发射向南的炮台, 只要考虑第一个, 因为后面(更北的)会被该炮台当下, 过不来, 以此类推, 向四个方向判断即可, 还要注意一点就是,可以站在原地不动。

上代码:

/*
https://vjudge.net/contest/441637#problem/K
	K - Escape
*/
#include<bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, 0};
int dy[] = {1, 0, -1, 0, 0};
int g[105][105];
bool vis[105][105][1005]; // 记录
struct node {
	int x, y;
	int step;
};

struct node2 {
	char c;
	int t, v;
} Pao[105][105]; // 记录炮塔位置, 周期, 发射时间, 速度

int n, m, k, d;

inline bool check(int x, int y) {
	if(x < 0 || y < 0 || x > n || y > m) {
		return true;
	}
	return false;
}
void bfs() {
	node Start;
	Start.x = Start.y = Start.step = 0;
	queue<node > q;
	q.push(Start);
	vis[Start.x][Start.y][Start.step] = true;
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		if(now.step > d) break; // 能量用完
		if(now.x == n && now.y == m) {
			cout << now.step << endl;
			return ; // 成功
		}
		for(int i = 0; i < 5; i ++) {
			int xx = now.x + dx[i];
			int yy = now.y + dy[i];
			if(check(xx, yy)) continue; // 越界
			if(!Pao[xx][yy].t && !vis[xx][yy][now.step+1] && now.step+1<=d) {
				bool flag = true; // 标记这点会不会死
				// 不是炮塔,步数, 标记, 满足, 再判断会不会打死

				// 向北找向南发射的炮塔
				for(int j = xx - 1; j >= 0; j --) {
					if(Pao[j][yy].t && Pao[j][yy].c == 'S') {
						int dd = xx - j; //距离
						if(dd%Pao[j][yy].v) break; //不能整除, 不会中
						else {
							int tt = now.step+1-dd/Pao[j][yy].v; // 时间
							if(tt < 0) break; //人还没到, 不会中
							if(tt%Pao[j][yy].t == 0) { // 时间刚刚好
								flag = 0;
								break;  //已经中炮
							}
						}
					}
					if(Pao[j][yy].t && Pao[j][yy].c != 'S') {
						break;  // 方向不对直接过
					}
				}
				if(!flag) continue; // 节省时间



				// 向南找向北发射的炮塔
				for(int j = xx + 1; j <= n; j ++) {
					if(Pao[j][yy].t && Pao[j][yy].c == 'N') {
						int dd = j - xx; //距离
						if(dd%Pao[j][yy].v) break; //不能整除, 不会中
						else {
							int tt = now.step+1-dd/Pao[j][yy].v; // 时间
							if(tt < 0) break; //人还没到, 不会中
							if(tt%Pao[j][yy].t == 0) { // 时间刚刚好
								flag = 0;
								break;  //已经中炮
							}
						}
					}
					if(Pao[j][yy].t && Pao[j][yy].c != 'N') {
						break;  // 方向不对直接过
					}
				}
				if(!flag) continue; // 节省时间



				// 向西找向东发射的炮塔
				for(int j = yy - 1; j >= 0; j --) {
					if(Pao[xx][j].t && Pao[xx][j].c == 'E') {
						int dd = yy - j; //距离
						if(dd%Pao[xx][j].v) break; //不能整除, 不会中
						else {
							int tt = now.step+1-dd/Pao[xx][j].v; // 时间
							if(tt < 0) break; //人还没到, 不会中
							if(tt%Pao[xx][j].t == 0) { // 时间刚刚好
								flag = 0;
								break;  //已经中炮
							}
						}
					}
					if(Pao[xx][j].t && Pao[xx][j].c != 'E') {
						break;  // 方向不对直接过
					}
				}
				if(!flag) continue; // 节省时间



				// 向北找向南发射的炮塔
				for(int j = yy + 1; j <= m; j ++) {
					if(Pao[xx][j].t && Pao[xx][j].c == 'W') {
						int dd = j - yy; //距离
						if(dd%Pao[xx][j].v) break; //不能整除, 不会中
						else {
							int tt = now.step+1-dd/Pao[xx][j].v; // 时间
							if(tt < 0) break; //人还没到, 不会中
							if(tt%Pao[xx][j].t == 0) { // 时间刚刚好
								flag = 0;
								break;  //已经中炮
							}
						}
					}
					if(Pao[xx][j].t && Pao[xx][j].c != 'W') {
						break;  // 方向不对直接过
					}
				}
				if(!flag) continue; // 节省时间
				node next;
				next.x =xx;
				next.y = yy;
				next.step = now.step + 1;
				vis[xx][yy][next.step] = true;
				q.push(next);
			}
		}

	}
	cout << "Bad luck!" << endl;
}
int main() {
	while(cin >> n >> m >> k >> d) {
		char c;
		int t, v, x, y;
		memset(Pao, 0, sizeof(Pao));
		memset(vis, 0, sizeof(vis));
		for(int i = 1; i <= k; i ++) {
			cin >> c >> t >> v >> x >> y;
			Pao[x][y].c = c;
			Pao[x][y].t = t;
			Pao[x][y].v = v;
		}
		bfs();
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值