Card Algorithm Game
I was playing with the following problem and have a brute force approach but could not come up with a nice solution. The problem is as follows:
There are 2*N cards. You and your opponent split them up (N cards to you and N to them). You know exactly what cards they have and what order they will play them in.
The rules of the game are as follows: For the first N/2 rounds, the person with the highest card wins and for last N/2 rounds, the person with the lowest card wins.
Given these rules and the order your opponents plays there cards, what is the maximum amount of wins you can get.
Example:
You have the cards: 2, 5, 6, 7. Your opponent has the cards: 1, 8, 4, 3 and plays them in that order.
The maximum score you can get is 2 since you play a 7 to their 1, lose the 2nd and 3rd round, and then play 2 in the last round to win.
My ideas: split your cards into two piles, your larger numbered and lower numbered ones. Then figure out an optimum matching up.
Pseudocode/algorithmic ideas would be greatly appreciated.
EDITS: There are a total of N rounds. The first N/2 rounds: the higher card wins. The last N/2 rounds: the lower card wins. N must be even.
I suggest:
if your remaining max is lower than opponent card, play lowest card (Lose)
else play your highest card (Win)
Similar for lower pile, inverting order.
First, create an array of N items (one per round). Each item is a list of "winning cards" for that round, ie the set of your cards which would win that round. In your example, you'd get {{2567},{},{2},{2}}
.
The following list gives some situations in which a card should be "assigned" to a round. This means that we decide that we'll play that card on that round and nothing can change that afterwards. After a card is assigned, the algorithm should go on after removing the assigned round from the set of rounds and the assigned card from the set of winning cards for any round.
It should be obvious that applying any of these rules in any situation would never reduce the number of possible wins, so applying them as much as possible before starting brute force is always a good idea.
Note that assigning a card to a round changes both the rounds and the cards that win in each round, so even if one of these rules is not applicable, it could become applicable after applying some other rule. So they must be tried iteratively until a full iteration on all of them produces no new assignments.
This is not a definitive solution but it surely will make things much easier for the final bruteforce step.
链接地址: http://www.djcxy.com/p/31068.html上一篇: 如何在Xcode中调整UIStackView的子视图?
下一篇: 卡算法游戏