专题训练二 搜索进阶 HDU - 3533 Escape (BFS,A*(曼哈顿距离))

HDU - 3533 Escape

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.

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

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100+10;
int n, m, k, d;
struct c {
	int dir, t, v, x, y;
}ca[maxn];
struct node {
	int x, y, step;
	node(){};
	node(int x, int y, int step) : x(x), y(y), step(step) {};
};
bool mp[maxn][maxn], hav[maxn][maxn][1010];
bool vis[maxn][maxn][1010];
int dir[][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {0, 0}};
void init() {
	memset(hav, false, sizeof(hav));
	for(int i = 0; i < k; i++) { //堡垒 
		for(int j = 0; j <= d; j += ca[i].t) { //模拟子弹 
			for(int kk = 1; ; kk++) { //枚举路程 
				int x = ca[i].x + dir[ca[i].dir][0] * kk;
				int y = ca[i].y + dir[ca[i].dir][1] * kk;
				if(x < 0 || x > n || y < 0 || y > m || mp[x][y]) break;
				if(kk % ca[i].v == 0) { //到达整点时刻,更新hav数组 
					hav[x][y][j+kk/ca[i].v] = true;
				}
			}
		}
	}
}
bool check(int x, int y) {
	return x >= 0 && x <= n && y >= 0 && y <= m;
}
int bfs() {
	memset(vis, false, sizeof(vis));
	queue<node> q;
	q.push(node(0, 0, 0));
	vis[0][0][0] = true;
	while(!q.empty()) {
		node cur = q.front(); q.pop();
		int x = cur.x, y = cur.y, step = cur.step;
		if(step > d) { //能量耗尽 
			return -1;
		}
		if(x == n && y == m) {
			return step;
		}
		for(int i = 0; i < 5; i++) {
			int nx = x + dir[i][0], ny = y + dir[i][1];
			if(check(nx, ny) && !mp[nx][ny] && !vis[nx][ny][step+1] && !hav[nx][ny][step+1]) {
				vis[nx][ny][step+1] = true;
				q.push(node(nx, ny, step+1));
			}
		}
	}
	return -1;
}
int main() {
	while(cin >> n >> m >> k >> d) {
		memset(mp, false, sizeof(mp));
		char dir;
		for(int i = 0; i < k; i++) { 
			cin >> dir >> ca[i].t >> ca[i].v >> ca[i].x >> ca[i].y;
			if(dir == 'N') ca[i].dir = 0;
			else if(dir == 'S') ca[i].dir = 1;
			else if(dir == 'E') ca[i].dir = 2;
			else if(dir == 'W') ca[i].dir = 3;
			mp[ca[i].x][ca[i].y] = true;
		}
		init();
		int ans = bfs();
		if(ans == -1) {
			cout << "Bad luck!" << endl;
		}
		else cout << ans << endl;
	}
}

A*

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100+10;
bool mp[maxn][maxn], hav[maxn][maxn][1010];
bool vis[maxn][maxn][1010];
int dir[5][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {0, 0}};
int n, m, k, d;
struct c {
    int dir, t, v, x, y;
} ca[maxn];
struct node {
    int x, y, step, F;  // F = step + 曼哈顿距离 (A*算法核心, 启发函数)
    node(){};
	node(int x, int y, int step, int F) : x(x), y(y), step(step), F(F) {};
    bool operator < (const node &b) const { //曼哈顿 + step 距离越小优先级越高 
        return F > b.F;
    }
};
void init() {
    memset(hav, false, sizeof(hav));
    for(int i = 0; i < k; i++) {   //枚举城堡
        for(int j = 0; j <= d; j += ca[i].t) {   //模拟一颗子弹
            for(int kk = 1; ; kk++) {  //枚举路程
                int x = ca[i].x + dir[ca[i].dir][0] * kk;
                int y = ca[i].y + dir[ca[i].dir][1] * kk;
                if(x < 0 || x > n || y < 0 || y > m || mp[x][y]) break;
                if(kk % ca[i].v == 0)    //到达整点时刻,更新hav数组
                    hav[x][y][j+kk/ca[i].v] = true;
            }
        }
    }
}
bool check(int x, int y) {
	return x >= 0 && x <= n && y >= 0 && y <= m;
}
int bfs() {
    memset(vis, false, sizeof(vis));
    priority_queue<node> q;
    vis[0][0][0] = true;
    q.push(node(0, 0, 0, 0+n+m));
    while(!q.empty()) {
        node cur = q.top(); q.pop();
        int x = cur.x, y = cur.y, step = cur.step;
        if(step > d) return -1;
        if(x == n && y == m) return step;
        for(int i = 0; i < 5; i++) {
            int nx = x + dir[i][0], ny = y + dir[i][1], nstep = step + 1;
            if(check(nx, ny) && !mp[nx][ny] && !hav[nx][ny][nstep] 
					&& !vis[nx][ny][nstep]) {
                vis[nx][ny][nstep] = true;
				int nF = nstep + abs(nx - n) + abs(ny - m);
                q.push(node(nx, ny, nstep, nF));
            }
        }
    }
    return -1;
}

int main() {
    while(cin >> n >> m >> k >> d) {
        memset(mp, false, sizeof(mp));
        char dir;
        for(int i = 0; i < k; i++) {
            cin >> dir >> ca[i].t >> ca[i].v >> ca[i].x >> ca[i].y;
            if(dir == 'N') ca[i].dir = 0;
            if(dir == 'S') ca[i].dir = 1;
            if(dir == 'E') ca[i].dir = 2;
            if(dir == 'W') ca[i].dir = 3;
            mp[ca[i].x][ca[i].y] = true;
        }
        init();
        int ans = bfs();
        if(ans == -1) cout << "Bad luck!" << endl;
        else cout << ans << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值