State compression-Leetcode #464 Can I Win

Source: Internet
Author: User

Dynamic programming is a top-down solution model, the key is to decompose and solve sub-problems, and then, according to the solution of sub-problem continuously upward recursion, to obtain the final Solution

So DP involves saving the solution of each computed sub-problem, so that when the same sub-problem is encountered, it is not necessary to continue the downward solution and the results can be obtained directly. State compression is used to save the solution of the sub-problem, the main idea is to all possible states (sub-problems) with a data structure (usually integer) of a unified representation, and then use map to each state and corresponding results associated, so that each time to solve sub-problem, find first, If the map already has a solution for that state, it is no longer needed, and it is also stored in a map after each solution of a state.

State compression is suitable for two-dollar states, where each column has a value of only 0 and 1 and is not suitable for solving large-scale problems (otherwise there is too much state to save)

Leetcode #464 Can I Win

https://leetcode.com/problems/can-i-win/

In the "game," the "turns" of the "players take", "adding" and "to a running" of total, any integer from 1..10. The player first causes the running total to reach or exceed wins.

What if we change the game so that players cannot re-use integers?

For example, the players might take turns drawing from a common pool of numbers of 1..15 without replacement until re Ach a total >= 100.

Given an integer maxChoosableInteger and another integer desiredTotal , determine if the first player to move can force a win, assuming both PLA Yers play optimally.

You can always assume that would not be larger than and would not be maxChoosableInteger desiredTotal larger than 300.

At first see this question, I think this is not a game tree! Then also did not think much, began phala Phala to knock Minimax, then make a αβ pruning feeling time complexity also almost. The results of a submission TL, the whole people are not good, but also thought their pruning did not cut well, and then really looked at the discuss only realized to use deep search, such as the game tree, such as violence wide search long overdue = =, (attached i timeout game tree code)

int Minmaxtree (bool turn, set<int> &cho, int goal, int now, int a, int b) {if (now>=goal) {if (turn) return-1;e LSE return 1;} Set<int>::iterator itr;if (Turn)//max Party {for (Itr=cho.begin (); Itr!=cho.end (); itr++) {set<int> tmp = cho; Tmp.erase (Tmp.find (*ITR)); a = Minmaxtree (0,tmp,goal,now+*itr,a,b); if (a>=b) break;}} Else{for (Itr=cho.begin (); Itr!=cho.end (); itr++) {set<int> tmp = Cho;tmp.erase (Tmp.find (*ITR)); b = MinMaxTree (1, TMP,GOAL,NOW+*ITR,A,B); if (a>=b) break;}} if (turn) return a;else return B; }

I then refer to the code on the discuss to achieve the dp+ state compression (basically hit ... ), the idea is a deep search of violence, with state compression record repeated state to reduce time consumption. Here is the main point of the state representation, the problem of each state (sub-problem) is the difference between:

1. The number of choices available

2. Current target number (how much worse is currently left from the original target)

That is, when the two states are the same, the two items are considered the same state. State distinction does not need to consider which party is currently, because both sides want to win

Now consider how to represent the state, the upper limit is n, then each state will have n Boolean value indicating whether the corresponding number has been used, such as n=3,[true false true] for 1, 3 can be used, 2 has been used. 01 is 101, you can find that it can be converted to the corresponding 2 binary number, so the n-bit 2 binary integer can directly represent each of the optional number of cases

Solves how to represent the optional number, also need to represent the current target, the method is to put the map directly into the vector, with vector subscript to indicate the target number, such as vector[goal][nums] = True, indicating that when the target number is goal, the current state is Nums, Players can win the game

bool miniMax (int status, vector<unordered_map<int,bool> > &DP, int goal, int maxn) {if (Dp[goal-1].find (status)!=dp[goal-1].end ())//the state has been searched for return dp[goal-1][status]; for (int i= maxn-1; i>=0; i--) {if (Status & (1<<i))//traverse each number if the number has not been used {//or, change the bit to 0 to indicate the use of that number if (i+1 >= goal | |!minimax (status^ <i) (DP,GOAL-I-1,MAXN))//If the target is currently available, or the other party cannot win {Dp[goal-1][status] = True;return true;}}} Dp[goal-1][status] = False;return false;} BOOL Caniwin (int maxchoosableinteger, int desiredtotal) {if (maxchoosableinteger>=desiredtotal) return True;if (( Maxchoosableinteger) * (maxchoosableinteger+1)/2<desiredtotal)//optional number of the sum less than the target must not be successful return false;int status = (1 < < Maxchoosableinteger)-1;//Initial state is full 1 i.e. all numbers are available vector<unordered_map<int,bool> > dp (desiredtotal);//Record status , Dp[goal][sta] indicates that the current available number is STA and can win return MiniMax (Status,dp,desiredtotal,maxchoosableinteger) If the target is goal;} 

With Unordered_map instead of a map, the search will be faster (and the corresponding space cost is higher). There is an episode in the Minimax in the DP when forget to pass the reference, causing the Shishun because of direct copy and constantly timed out, and have been unable to find the reason = =, the brain is not enough

State compression-Leetcode #464 Can I Win

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.