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