Escape

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>

using namespace std;

const int N = 110;

char os[] = {'N', 'S', 'W', 'E'};
int dx[] = {-1, 1, 0, 0, 0}, dy[] = {0, 0, -1, 1, 0};

int st[N][N];
int m, n, c, pw;

struct bullet{
    int o; // 方向
    int x, y; // 位置
    int s; // 速度
    
    int valid;
    
    void update(){
        for(int i = 0; i < s; i ++){
            x += dx[o], y += dy[o];
            if(st[x][y]) valid = 0;
            if(x < 0 || y < 0 || x > n || y > m) valid = 0;
        }
    }
    
};

struct castle{
  int o; // 方向
  int x, y; // 位置
  int s; // 速度
  int t; // 间隔
  
  int p; // 当前过了多长时间
  
  castle(int a, int b, int c, int d, int e){
      o = a, t = b, s = c, x = d, y = e;
      p = 0;
  }
  
  void add(){
      p ++;
  }
  
  int check(){
      if(p == t){
          p = 0;
          return 1;
      }
      return 0;
  }
  
  bullet get(){
      bullet res = {o, x, y, s, 1};
      // res.update();
      return res;
  }
};

struct node{
    int x, y;
    int pw;
};

//node pre[N][N][20];

vector<castle> castles;
vector<bullet> bullets;



int bfs(){
    queue<node> q;
    q.push({0, 0, pw});
    st[0][0] = 1;

    //pre[0][0][pw] = {-1, -1, -1};
    
    int pwt = pw;
    int cur = -1;
    while(q.size()){
        node h = q.front();
        q.pop();
        
        if(h.pw == 0) continue;
        
        if(cur == -1 || h.pw != cur){ // 预测下一秒的战场状况
            cur = h.pw;
            for(int i = 0; i < bullets.size(); i ++) bullets[i].update();
            for(int i = 0; i < castles.size(); i ++){
                castles[i].add();
                if(castles[i].check()) 
                    bullets.push_back(castles[i].get());
            }
        }
                
        for(int i = 0; i < 5; i ++){
            int x = h.x + dx[i], y = h.y + dy[i];
            if(x < 0 || y < 0 || x > n || y > m) continue;
            if(st[x][y] && (x != h.x || y != h.y)) continue;
            int blt = 0;
            for(int j = 0; j < bullets.size(); j ++)
                if(bullets[j].valid && bullets[j].x == x && bullets[j].y == y){
                    blt = 1;
                    break;
                }
            if(blt) continue;
            st[x][y] = 1;
            q.push({x, y, h.pw - 1});
            
            //pre[x][y][h.pw - 1] = h;
            
            if(x == n && y == m){
                // node k = {x, y, h.pw - 1};
                // while(pre[k.x][k.y][k.pw].x != -1){
                //     cout << k.x << ' ' << k.y << ' ' << k.pw << endl;
                //     k = pre[k.x][k.y][k.pw];
                // }
                return pwt - h.pw + 1;
            }
        }
    }
    
    return -1;
}

int main(){
    while(cin >> n >> m >> c >> pw){
        castles.clear();
        bullets.clear();
        
        memset(st, 0, sizeof st);
        
        for(int i = 0; i < c; i ++){
            char c;
            int o, t, v, x, y;
            cin >> c >> t >> v >> x >> y;
            if(c == 'N') o = 0;
            if(c == 'S') o = 1;
            if(c == 'W') o = 2;
            if(c == 'E') o = 3;
            st[x][y] = 1;
            castles.push_back(castle(o, t, v, x, y));
        }
        
        int res = bfs();
        if(~res) cout << res << endl;
        else cout << "Bad luck!" << endl;
    }
    
    return 0;
}
上一篇:【问题管理】-- restTemplate请求get方式中url包含+、空格、%、_等特殊符号


下一篇:C语言 ‘\t‘、‘\n‘ 不可见字符和 \n、\t互相转换