原题:http://acm.hdu.edu.cn/showproblem.php?pid=3533
题意:从(0 0)走到(n m),四个方向或者选择不走,每次走或不走都要消耗一个能量,问最少消耗多少能量,初始能量为d;
有k个炮台,c t v x y 分别表示炮台发射的方向,每次发射的时间间隔,炮火的速度,炮台的坐标;
有炮台的地方不能走,当某个时刻人和炮火在同一个坐标时就死了,途中能量耗尽也同样死掉;
炮火前进路上遇到另一个炮台则炮火消失,但炮台不消失;
思路:我们可以先预处理出哪些时刻哪些点是有炮火的,之后在BFS中直接判断即可;
注意:题目中n 和 m是相反的!
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
const int maxn = 110;
const int maxd = 1100;
int n, m, k, d;
bool vis[maxn][maxn][maxd];
bool cast[maxn][maxn];
int f[5][2] = {-1, 0, 0, -1, 0, 1, 1, 0, 0, 0};
bool hit[maxn][maxn][maxd];
struct pao
{
int x, y;
int t, v;
char dir[3];
}p[maxn];
struct node
{
int x, y;
int time;
};
void init()
{
for(int i = 1;i<=k;i++)
{
int x = p[i].x, y = p[i].y, v = p[i].v, t = p[i].t;
if(p[i].dir[0] == 'E')
{
int w = n;
for(int j = x+1;j<=n;j++)
{
if(cast[j][y])
{
w = j;
break;
}
}
int cur = 1;
for(int j = x + v;j<=w;j += v)
{
for(int kk = cur;kk<=d;kk += t)
hit[j][y][kk] = true;
cur++;
}
}
if(p[i].dir[0] == 'W')
{
int w = 0;
for(int j = x-1;j>=0;j--)
{
if(cast[j][y])
{
w = j;
break;
}
}
int cur = 1;
for(int j = x - v;j>=w;j -= v)
{
for(int kk = cur;kk<=d;kk += t)
hit[j][y][kk] = true;
cur++;
}
}
if(p[i].dir[0] == 'N')
{
int w = 0;
for(int j = y-1;j>=0;j--)
{
if(cast[x][j])
{
w = j;
break;
}
}
int cur = 1;
for(int j = y - v;j>=w;j -= v)
{
for(int kk = cur;kk<=d;kk += t)
hit[x][j][kk] = true;
cur++;
}
}
if(p[i].dir[0] == 'S')
{
int w = m;
for(int j = y+1;j<=m;j++)
{
if(cast[x][j])
{
w = j;
break;
}
}
int cur = 1;
for(int j = y + v;j<=w;j += v)
{
for(int kk = cur;kk<=d;kk += t)
hit[x][j][kk] = true;
cur++;
}
}
}
}
void bfs()
{
queue<node>q;
while(!q.empty()) q.pop();
node a;
a.x = 0, a.y = 0, a.time = 0;
q.push(a);
node c;
while(!q.empty())
{
node b = q.front();
q.pop();
for(int i = 0;i<5;i++)
{
int nx = b.x + f[i][0];
int ny = b.y + f[i][1];
int nt = b.time + 1;
if(cast[nx][ny]) continue; //该位置有炮台
if(nt > d) break; //能量耗尽
if(nx < 0 || ny < 0 || nx > n || ny > m || vis[nx][ny][nt] || hit[nx][ny][nt])
continue;
if(nx == n && ny == m)
{
printf("%d\n", nt);
return;
}
vis[nx][ny][nt] = true;
c.x = nx, c.y = ny, c.time = nt;
q.push(c);
}
}
printf("Bad luck!\n");
return;
}
int main()
{
while(~scanf("%d%d%d%d", &m, &n, &k, &d))
{
memset(vis, false, sizeof vis);
memset(hit, false, sizeof hit);
memset(cast, false, sizeof cast);
for(int i = 1;i<=k;i++)
{
scanf("%s%d%d%d%d", p[i].dir, &p[i].t, &p[i].v, &p[i].y, &p[i].x);
cast[p[i].x][p[i].y] = true;
}
vis[0][0][0] = true;
init();
bfs();
}
return 0;
}