ZOJ 2849 Attack of Panda Virus (优先队列 priority_queue)
生活随笔
收集整理的這篇文章主要介紹了
ZOJ 2849 Attack of Panda Virus (优先队列 priority_queue)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
優(yōu)先隊(duì)列,據(jù)說標(biāo)程是并查集,沒思路。貌似優(yōu)先隊(duì)列都是直接用stl寫的,又逼我用stl了。prioriry_queue不熟。
ps: value值越小,優(yōu)先級(jí)越高。所以重載 < 運(yùn)算符時(shí)按優(yōu)先級(jí)從大到小排序
bool operator < (const node a, const node b) {if(a.day != b.day) return a.day > b.day;
return a.type > b.type;
}
?
參考網(wǎng)上的代碼
View Code 1 #include <iostream>2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5
6 using namespace std;
7
8 const int N = 510;
9 const int inf = ~0u>>2;
10
11 struct node {
12 int x, y;
13 int day;
14 int type;
15
16 };
17
18 priority_queue<node> q;
19
20 int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
21 int m, n;
22 int mp[N][N];
23 int list[N*N];
24
25 bool operator < (const node a, const node b) {
26 if(a.day != b.day) return a.day > b.day;
27 return a.type > b.type;
28 }
29
30 bool insq(int x, int y) {
31 if(x < 0 || x >= m || y < 0 || y >= n) return false;
32 return true;
33 }
34
35 void bfs() {
36 node u, v;
37 int d, i;
38
39 while(!q.empty()) {
40 u = q.top(); q.pop();
41
42 d = -inf;
43 v.day = u.day;
44 v.type = u.type;
45
46 for(i = 0; i < 4; ++i) {
47 v.x = u.x + dir[i][0];
48 v.y = u.y + dir[i][1];
49
50 if(!insq(v.x, v.y) || mp[v.x][v.y] > 0) continue;
51
52 if(-1*mp[v.x][v.y] <= v.day) {
53 mp[v.x][v.y] = v.type;
54 list[v.type]++;
55 q.push(v);
56 } else if(mp[v.x][v.y] > d) {
57 d = mp[v.x][v.y];
58 }
59 }
60 if(d != -inf) {
61 u.day = -1*d;
62 q.push(u);
63 }
64 }
65 }
66
67 int main() {
68 freopen("data.in", "r", stdin);
69
70 int i, j, t;
71 node p;
72 while(~scanf("%d%d", &m, &n)) {
73 memset(list, 0, sizeof(list));
74
75 for(i = 0; i < m; ++i) {
76 for(j = 0; j < n; ++j) {
77 scanf("%d", &mp[i][j]);
78 if(mp[i][j] > 0) {
79 p.x = i; p.y = j;
80 p.day = 1; p.type = mp[i][j];
81 list[p.type] ++;
82 q.push(p);
83 }
84 }
85 }
86 bfs();
87 scanf("%d", &t);
88 while(t--) {
89 scanf("%d", &i);
90 printf("%d\n", list[i]);
91 }
92 }
93 return 0;
94 }
轉(zhuǎn)載于:https://www.cnblogs.com/vongang/archive/2012/04/01/2429355.html
總結(jié)
以上是生活随笔為你收集整理的ZOJ 2849 Attack of Panda Virus (优先队列 priority_queue)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mssql语句精华指令
- 下一篇: 在.m中的@interface (原创)