ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Escape

2021-09-28 19:33:55  阅读:151  来源: 互联网

标签:castles pw int st ++ bullets 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;
}

标签:castles,pw,int,st,++,bullets,Escape
来源: https://www.cnblogs.com/tomori/p/15349622.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有