Saving Princess
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 n, h, s, p and m (1 ≤ n ≤ 50, 1 ≤ h ≤ 50, 0 ≤ s, p, m ≤ 50). The following n lines contain three integer numbers each --- si, pi, and mi (1 ≤ si, pi, mi≤ 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 65 5 26 5 26 7 33 11 5 5 65 5 26 5 26 7 3
Sample Output
DEDUNLUCKY
Source
Author
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #define LL long long14 #define pii pair 15 #define INF 0x3f3f3f3f16 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 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('\n');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 }