[除草]BZOJ 2548 灭鼠计划

题目大意:

很多武器, 很多老鼠, 模拟若干秒.(实际题意要麻烦很多)

 

简要分析:

算法????

我会告诉你除了模拟之外就没了么????

写模拟的时候用点平时从来不用的语法感觉很爽啊......虚基类......继承......多态......

 

代码实现:

View Code
  1 #include <cassert>
  2 #include <cctype>
  3 #include <cmath>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <utility>
  8 #include <functional>
  9 #include <string>
 10 #include <bitset>
 11 #include <deque>
 12 #include <list>
 13 #include <map>
 14 #include <queue>
 15 #include <set>
 16 #include <stack>
 17 #include <vector>
 18 #include <iterator>
 19 #include <algorithm>
 20 #include <complex>
 21 #include <valarray>
 22 #include <numeric>
 23 
 24 using namespace std;
 25 
 26 const int kMaxL = 10, kMaxR = 10, kMaxN = 50, kMaxM = 50, kMaxK = 50, kMaxP = 100;
 27 const int kFx[4] = {-1, 0, 1, 0}, kFy[4] = {0, 1, 0, -1};
 28 // 0 - N, 1 - E, 2 - S, 3 - W
 29 char idx[256];
 30 int l, r, n, m, k, p, limit, tot, cur;
 31 int grid[kMaxN][kMaxM];
 32 
 33 struct Mouse {
 34     bool exist;
 35     int x, y, dir, cnt;
 36     bool sex;
 37     int stun, fxxk, part, grow;
 38     bool right;
 39     void scan() {
 40         scanf("%d%d", &x, &y);
 41         -- x, -- y;
 42         char tmp[2];
 43         scanf("%s", tmp);
 44         dir = idx[(int)tmp[0]];
 45         scanf("%s", tmp);
 46         sex = (tmp[0] == 'X');
 47         exist = true;
 48         part = -1;
 49     }
 50     void move() {
 51         part = -1;
 52         if (grid[x][y] >> dir & 1) {
 53             x += kFx[dir];
 54             y += kFy[dir];
 55         }
 56         else {
 57             int dr = dir, dl = dir;
 58             dr = (dr + 1) % 4;
 59             dl = (dl - 1 + 4) % 4;
 60             if (!(grid[x][y] >> dl & 1) && !(grid[x][y] >> dr & 1)) {
 61                 dir = dl;
 62             }
 63             else {
 64                 if ((grid[x][y] >> dl & 1) && (grid[x][y] >> dr & 1)) {
 65                     if (right) dir = dr;
 66                     else dir = dl;
 67                     right ^= 1;
 68                 }
 69                 else if (grid[x][y] >> dl & 1) {
 70                     dir = dl;
 71                 }
 72                 else {
 73                     dir = dr;
 74                 }
 75             }
 76         }
 77     }
 78 };
 79 
 80 Mouse mice[kMaxK * 10];
 81 
 82 void DealWithGrow() {
 83     for (int i = 0; i < k; ++ i)
 84         if (mice[i].exist && !mice[i].stun && mice[i].grow)
 85             -- mice[i].grow;
 86 }
 87 
 88 void DealWithFxxk() {
 89     for (int i = 0; i < k; ++ i)
 90         if (mice[i].exist && mice[i].fxxk && mice[mice[i].part].exist && i < mice[i].part) {
 91             if (!mice[i].stun && !mice[mice[i].part].stun) {
 92                 -- mice[i].fxxk, -- mice[mice[i].part].fxxk;
 93                 if (!mice[i].fxxk) {
 94                     mice[i].stun = mice[mice[i].part].stun = 1;
 95                     for (int d = 0; d < 4; ++ d)
 96                         if (grid[mice[i].x][mice[i].y] >> d & 1) {
 97                             mice[k].x = mice[i].x;
 98                             mice[k].y = mice[i].y;
 99                             mice[k].sex = (d % 2 == 0);
100                             mice[k].grow = 5;
101                             mice[k].dir = d;
102                             mice[k].exist = true;
103                             mice[k].part = -1;
104                             ++ k;
105                         }
106                 }
107             }       
108         }
109     static int cnt[kMaxN][kMaxM];
110     memset(cnt, 0, sizeof(cnt));
111     for (int i = 0; i < k; ++ i)
112         if (mice[i].exist) cnt[mice[i].x][mice[i].y] ++;
113     for (int i = 0; i < k - 1; ++ i)
114         for (int j = i + 1; j < k; ++ j)
115             if (!mice[i].fxxk && !mice[j].fxxk && mice[i].exist && mice[j].exist && !mice[i].grow && !mice[j].grow && mice[i].x == mice[j].x && mice[i].y == mice[j].y && cnt[mice[i].x][mice[i].y] == 2 && (mice[i].sex ^ mice[j].sex) == 1 && !mice[i].stun && !mice[j].stun && (mice[i].part != j || mice[j].part != i)) {
116                 mice[i].fxxk = mice[j].fxxk = 2;
117                 mice[i].part = j, mice[j].part = i;
118             }
119 }
120 
121 void DealWithMove() {
122     for (int i = 0; i < k; ++ i)
123         if (mice[i].stun) -- mice[i].stun;
124         else if (!mice[i].fxxk) mice[i].move();
125 }
126 
127 struct WeapMother {
128     int x, y, t;
129     WeapMother() {}
130     virtual void expose() = 0;
131     void get() {
132         scanf("%d%d%d", &t, &x, &y);
133         -- x, -- y;
134     }
135 };
136 
137 struct Weap0 : public WeapMother {
138     virtual void expose() {
139         if (t != cur) return;
140         static bool mark[kMaxN][kMaxM];
141         memset(mark, false, sizeof(mark));
142         for (int d = 0; d < 4; ++ d) {
143             int nx = x, ny = y, len = 0;
144             mark[nx][ny] = true;
145             while (grid[nx][ny] >> d & 1) {
146                 ++ len;
147                 if (len > l) break;
148                 nx += kFx[d], ny += kFy[d];
149                 mark[nx][ny] = true;
150             }    
151         }
152         for (int i = 0; i < k; ++ i)
153             if (mark[mice[i].x][mice[i].y]) mice[i].exist = false;
154     }
155 };
156 
157 struct Weap1 : public WeapMother {
158 #define SQR(x) ((x) * (x))
159     virtual void expose() {
160         if (t != cur) return;
161         for (int i = 0; i < k; ++ i)
162             if (SQR(mice[i].x - x) + SQR(mice[i].y - y) <= r * r) {
163                 mice[i].stun += 3;
164             }
165     }
166 #undef SQR
167 };
168 
169 struct Weap2 : public WeapMother {
170     virtual void expose() {
171         if (t != cur) return;
172         for (int i = 0; i < k; ++ i)
173             if (mice[i].x == x && mice[i].y == y)
174                 mice[i].exist = false;    
175     }
176 };
177 
178 struct Weap3 : public WeapMother {
179     virtual void expose() {
180         if (t != cur) return;
181         for (int i = 0; i < k; ++ i)
182             if (mice[i].x == x && mice[i].y == y)
183                 mice[i].sex ^= 1;
184     }
185 };
186 
187 WeapMother *weap[kMaxP];
188 
189 int main() {
190     idx['N'] = 0, idx['E'] = 1, idx['S'] = 2, idx['W'] = 3;
191     scanf("%d%d%d%d", &l, &r, &n, &m);
192     for (int i = 0; i < n; ++ i)
193         for (int j = 0; j < m; ++ j)
194             scanf("%d", &grid[i][j]);
195     scanf("%d", &k);
196     for (int i = 0; i < k; ++ i)
197         mice[i].scan();
198     scanf("%d%d", &p, &limit);
199     for (int i = 0; i < p; ++ i) {
200         int type;
201         scanf("%d", &type);
202         -- type;
203         if (type == 0) {
204             Weap0 *nw = new Weap0;    
205             nw->get();
206             weap[i] = nw;
207         }
208         else if (type == 1) {
209             Weap1 *nw = new Weap1;
210             nw->get();
211             weap[i] = nw;
212         }
213         else if (type == 2) {
214             Weap2 *nw = new Weap2;
215             nw->get();
216             nw->t += 3;
217             weap[i] = nw;
218         }
219         else {
220             Weap3 *nw = new Weap3;
221             nw->get();
222             weap[i] = nw;
223         }
224     }
225     scanf("%d", &tot);
226     cur = -1;
227     int mice_cnt;
228     while (++ cur <= tot) {
229         for (int i = 0; i < p; ++ i) weap[i]->expose();
230         DealWithGrow();
231         DealWithFxxk();
232         DealWithMove();
233         mice_cnt = 0;
234         for (int i = 0; i < k; ++ i)
235             mice_cnt += mice[i].exist;
236         if (mice_cnt > limit) {
237             printf("-1\n");
238             return 0;
239         }
240     }
241     printf("%d\n", mice_cnt);
242     return 0;
243 }
posted @ 2012-07-06 21:20  zcwwzdjn  阅读(605)  评论(0编辑  收藏  举报