A carrom game

3.5

14 votes
Dynamic Programming and Bit Masking, Applications of Dynamic Programming, Algorithms, Dynamic Programming
Problem

There is a table of carrom game which has 4 sides. There are 4 players sitting on each side in the following order, A,B,C, and D.

The players sitting opposite to each other are in the same team. Therefore, AC and BD are the two teams.

There are N black pawns and N white pawns distributed in the table at different positions. And there is also one Red pawn. 

The game goes as follows, 

  • Each pawn of each color has its difficulty levels givens. Initially, A,B,C, and D have their skill levels as P,Q,R, and S respectively.
  • The player can only hit a certain pawn if and only if the difficulty level of that pawn is less than equal to the skill level of that player. After hitting that particular pawn, the skill level of that player increases by that difficulty level.

Now the task for the players is to hit the pawns. AC team will always target Black Pawns and BD will always target White Pawns. No one will target Red pawn. If all the N pawns assigned to that team are hit, then only they will pursue Red Pawn. Whoever successfully hits the red pawn wins the game.

For every turn, in which a player is unable to hit any pawn, his skill level would be incremented by one.

The turn of the players goes as follows:

ABCDAB...and so on

Here, A starts first.

Notes 

  • A & C are on the same team and so are B & D.
  • Red pawn will only be hit after every N pawns of assigned color to that team are destroyed.
  • A player can only hit a single pawn during his turn if he wishes to do so. He can also opt to skip his turn, that is, he can decide not to hit any pawn even if he is capable of hitting one.
  • If a player opts for a skip round, his skill level will still increase by 1 as he did not hit any pawn during his turn.

You have to determine who will win the game considering each team plays optimally. 

Input format

  • The first line contains a single number N.
  • The second line contains N integers representing the difficulty levels of each Black Pawn.
  • The third line contains N integers representing the difficulty levels of each White Pawn.
  • The fourth line contains a single Integer representing the difficulty level of a single red pawn.
  • The fifth line contains P,Q,R, and S the initial skill level of the four players.

Output format

You have to print the string "AC" if AC team wins, else print "BD" (without the quotes).

Constraints

0 < N ⩽ 5
0 ⩽ P, Q, R, S ⩽ 100
0 ⩽ Sum of the difficulty levels of black pawns ⩽ 10
0 ⩽ Sum of the difficulty levels of white pawns ⩽ 10
0 ⩽ Difficulty level of single red pawn ⩽ 105

 

Time Limit: 10
Memory Limit: 256
Source Limit:
Explanation

The optimal strategy for B-D team would be that only either of B or D would be hitting all pawns so that the sum of White pawn i.e 24 would be added to either B or D leading it to make it atleast of Skill level 26. While an optimal strategy for A-B would lead them to maximum Skill level of 22. i.e player C hits all the black pawns. And if you would have noticed that either B/D after hitting a pawn of difficulty 2 they would end up with skill level of 4 but remaining pawns are of difficulty 10 & 12 so he would have to wait 6 rounds so that 4+6 (every round skill level increments by 1 of that player if no pawn is hit by that player). So after skill level of C becomes 10 he can hit 10 & 12 and total skill level becomes 10+10+12 = 32, while player B would end up 17 + 6 (of 6 rounds) = 21. After this red pawn is 100 difficult and C is 68 skills away while player B is 79 skills away so B hits after 68 rounds and ends up winning. So B-D team wins.

Editor Image

?