ZOJ 3369 Saving Princess

Saving Princess

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on ZJU. Original ID: 3369
64-bit integer IO format: %lld      Java class name: Main
Special Judge
 

Saving princesses is always a hard work. Ivan D'Ourack is planning to save the princess locked in the tower. However, n dangerous monsters are guarding the road from the city where Ivan lives to the tower where the princess is locked.

Fortunately Ivan is a warrior and a magician. Thus he can defeat monsters in a fight, and enchant them to pass unnoticed.

Initially Ivan has h health points, strength s, spell power p and m mana points. To defeat i-th monster in a fight, he must have strength at least si, and he loses max(2si - s, 0) health points in a fight. If the number of health points becomes 0 or less, Ivan dies. After defeating a monster Ivan's strength increases by 1.

To enchant i-th monster Ivan must have spell power at least pi and he spends mi mana points to do it. If Ivan does not have mi mana points, he cannot enchant the monster. After enchanting the monster Ivan's spell power increases by 1.

Find out, whether Ivan can save princess, and if he can how to do it.

Input

The first line of the input file contains nhsp and m (1 ≤ n ≤ 50, 1 ≤ h ≤ 50, 0 ≤ spm ≤ 50). The following n lines contain three integer numbers each --- sipi, and mi (1 ≤ sipimi≤ 50).

There are multiple cases. Process to the end of file.

Output

If Ivan cannot save princess, output "UNLUCKY". In the other case output n characters, the i-th character must be 'D' if Ivan must defeat the i-the monster, or 'E' if he must enchant it.

Sample Input

3 12 5 5 6
5 5 2
6 5 2
6 7 3
3 11 5 5 6
5 5 2
6 5 2
6 7 3

Sample Output

DED
UNLUCKY
 

Source

Author

Andrew Stankevich
 
解题:好强的BFS。。。记录状态,保留最优的健康值,在各项一样的情况下,选择活得最久的,啊哈。。。。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 100;
18 struct node{
19     int h,s,p,m,step;
20     node(int ax = 0,int ab = 0,int ac = 0,int ad = 0,int ae = 0){
21         h = ax;
22         s = ab;
23         p = ac;
24         m = ad;
25         step = ae;
26     }
27     char way[maxn];
28 };
29 queue<node>q;
30 int n,h,s,p,m;
31 int ms[maxn],mp[maxn],mm[maxn],opti[maxn][maxn][maxn];
32 bool bfs(){
33     while(!q.empty()) q.pop();
34     q.push(node(h,s,p,m,0));
35     while(!q.empty()){
36         node now = q.front();
37         q.pop();
38         if(now.step == n){
39             for(int i = 1; i <= n; ++i)
40                 putchar(now.way[i]);
41             putchar('
');
42             return true;
43         }
44         if(now.h < opti[now.s][now.p][now.m]) continue;
45         if(now.s >= ms[now.step+1]){
46             node tmp = now;
47             tmp.step++;
48             tmp.s++;
49             tmp.h -= max(2*ms[tmp.step] - now.s,0);
50             tmp.way[tmp.step] = 'D';
51             if(tmp.h > opti[tmp.s][tmp.p][tmp.m]){
52                 opti[tmp.s][tmp.p][tmp.m] = tmp.h;
53                 q.push(tmp);
54             }
55         }
56         if(now.p >= mp[now.step+1] && now.m >= mm[now.step+1]){
57             node tmp = now;
58             tmp.step++;
59             tmp.p++;
60             tmp.m -= mm[now.step+1];
61             tmp.way[tmp.step] = 'E';
62             if(tmp.h > opti[tmp.s][tmp.p][tmp.m]){
63                 opti[tmp.s][tmp.p][tmp.m] = tmp.h;
64                 q.push(tmp);
65             }
66         }
67     }
68     return false;
69 }
70 int main() {
71     while(~scanf("%d %d %d %d %d",&n,&h,&s,&p,&m)){
72         for(int i = 1; i <= n; ++i)
73             scanf("%d %d %d",ms+i,mp+i,mm+i);
74         memset(opti,0,sizeof(opti));
75         if(!bfs()) puts("UNLUCKY");
76     }
77     return 0;
78 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4083985.html