Coin-throwing gambling game-pang Guo hero club

Source: Internet
Author: User

This is an online programming challenge from panggo.com, which is a medium-to-upper difficulty. This formal question has aroused my interest in the challenge of the pang Guo network program. The following describes my solutions.

Question: Coin-throwing gambling games

Original Link (may be invalid): Http://hero.pongo.cn/OnlineCompiler/Index? Id = 59 & examid = 57

Question details: Small A and Small B initially had A and B respectively. They decided to play a gambling game. The rule of the game was to throw a coin. If the result was positive, mr. A needs to pay Mr. B C.

Otherwise, Small B will pay D yuan for small.

They threw coins until a person who was supposed to pay the money could not get the money, and thought he had lost his bankruptcy.

The coin is not even. It generates positive based on the probability of P1, and the opposite is produced based on the probability of 1-P1.

What is the probability that Mr. A will win (that is, Mr. B will lose in bankruptcy?

Input: A, B, C, D is an integer, 0 <= A, B <50, 0 <= C, D <= 100, P1 is a floating point 0 <= P1 <= 1;

Output: To ensure that the output is an integer, multiply the probability of a small A's victory by 100 and then perform an integer (directly truncates the digits following.

For example, if 0.125 is returned, 12 is output.

 

A look at this question is to calculate probability, but it is very troublesome to use mathematical formulas for calculation. So I will give an example to analyze and find a solution to this problem. Then, assuming that after several coins, Mr. A holds x yuan, and Mr. B holds y yuan, the moment is regarded as a State, so next time if you can continue to cast coins (I .e.: X is not less than 0, Y is not less than 0), then throw a coin, may become small a holding X-2 yuan, small B holding y + 2 yuan, this is a state, and the corresponding, small a holding x + 1 yuan, Small B holding Y-1 yuan, this is another state. Therefore, we can regard the entire Coin throwing process as a transformation between States. Are there infinite states? What is the relationship between states? Next, we will plot a state conversion chart for a group of given data. Assume that starting from small A has 5 yuan, Small B has 3 yuan, coins face up, small A to Small B's money is 2 yuan, coin down, Small B to small A's money is 1 yuan.

Here, the first digit in the ellipse is the amount of money held by small A, and the second digit is the amount of money held by Small B. Take the negative value of money held by A or B as the final state, because one person has lost all the money and there will be no more of the following state. However, two adjacent States, one of their sub-States, overlap. For example, the State (3/5) can be converted to (4/4) and adjacent to it (6/2) you can also convert them to (4/4) and merge them. This is a feature of state conversion.

As the number of state transitions increases, we can see that the last state always repeats with the former. This shows that all States should be limited. In the figure, the subsequent State conversion is not shown, because the State has already been repeated and it is impossible to see a new state.

Next we will consider the probability problem, and finally we will get the probability. Assume that the coin faces up with a probability of 0.7. The probability of the initial state (5/3) must be 1. After the coin is cast for the first time, the probability of (3/5) is 0.7, and the probability of (6/2) is. We can think that the probability of a subnode comes from the probability of a parent node. The probability of the two byte points of the node is equal to the probability of the node multiplied by 0.7 and 0.3 respectively. Therefore, the problem of probability calculation is also solved.

As the State changes, the probability is transferred. Observe the status transition chart again. If we want to calculate the probability of State (3/5), we need to find the State (5/3) and State (2/6), P (3/5) = P (5/3) * 0.7 + P (2/6) * 0.3; After calculating the formula of a node, we can obtain a matrix by stacking the formula together. For example:

In the figure, if the right side of the matrix is multiplied by the column vector of the probability of each state point, the probability of each state when the next coin is thrown is obtained. Here, I set the probability that three vertices (-2/10), (-1/9), and (9/-1) are provided to myself as 1, in this way, the probability of the next matrix multiplication will not be lost (it should be calculated and transferred to itself ). Such continuous Multiplication can obtain the limit of the matrix and obtain the final probability. The program cannot obtain the number of times, and does not need to multiply the number of times. The convergence of probability can achieve the desired effect. The following is my code:

# Include <cstdio> # include <string> # include <cmath> using namespace STD; struct node {int A; // The amount of money remaining for small A: int B; // The remaining amount of money in small B INT finish; // whether to end,-1 small a loses, 1 Small B loses, 0 fails to win struct node * Next ;}; int win (int A, int B, int C, int D, double P1) {double Pa = 0.0; double P2 = 1-P1; if (A = B & C = D & p1 = 0.5) {Pa = 0.5; // both parties have the same conditions, same probability} else if (a <C & B <D) {Pa = 1-P1; // a final winner} else {int start =-C; // The most A small fortune may be a negative int end = a + B + D; // the greatest wealth that A has, and a small B may be a negative int COUNT = end-start + 1; bool * exists = new bool [count]; int I = 0, j = 0; for (I = 0; I <count; ++ I) {// initialize the variable, set all to false exists [I] = false;} // determine the root node * root = new node (); root-> A = A; root-> B = B; root-> finish = 0; int current = 0; // current node int Total = 1; // total number of nodes node * front = root; node * tail = root; node * pnew = NULL; Exists [root-> A + C] = true; int finish = 0; int nexta = 0; int nextb = 0; while (current <total) {If (front-> finish = 0) {// handle the front case nexta = front-> A-C; nextb = front-> B + C; if (nexta <0) {finish =-1;} else {finish = 0;} pnew = new node (); pnew-> A = nexta; pnew-> B = nextb; pnew-> finish = finish; If (! Exists [nexta + C]) {// The node is not added to the queue tail-> next = pnew; tail = tail-> next; exists [nexta + C] = true; total ++;} // handling the opposite nexta = front-> A + D; nextb = front-> B-d; If (nextb <0) {finish = 1 ;} else {finish = 0;} pnew = new node (); pnew-> A = nexta; pnew-> B = nextb; pnew-> finish = finish; If (! Exists [nexta + C]) {// The node is not added to the queue tail-> next = pnew; tail = tail-> next; exists [nexta + C] = true; total ++ ;}/// if to judge finish = 0 front = front-> next; // move to the next node current ++ ;} // while loop // iteratively generate probability vector int * tag = new int [count]; int K = 0; for (I = 0; I <count; ++ I) {If (exists [I]) {tag [I] = K; k ++;} else {tag [I] =-1; // indicates that the node does not exist} double * pfir = new double [total]; double * psec = new double [t Otal]; double * pmid = NULL; int * Numbers = new int [total]; // The amount of money that a might possess: int r = 0, n = 0; // R loop variable, N iterations front = root; while (front! = NULL) {// initialize the amount of money that may occur for small A (arranged in ascending order) numbers [tag [Front-> A + C] = front->; front = front-> next;} // initialization probability for (r = 0; r <total; ++ R) {pfir [R] = 0.0; psec [R] = 0.0;} pfir [tag [A + C] = 1.0; // The initial probability is 1 int T = a + B; n = 0; double psum = 0.0; int V = 0; while (N ++ <1000) {// completes n iterations (up to 10 thousand times) for (r = 0; r <total; ++ R) {// completes an iteration if (numbers [R] <0) {// The Node psec [R] = pfir [R] + pfir [tag [num Bers [R] + C] * P1;} else if (numbers [R] <= T) {psec [R] = 0; if (numbers [R]-D> = 0) {psec [R] + = pfir [tag [numbers [R]-D + C] * P2 ;} if (numbers [R] + C <= T) {psec [R] + = pfir [tag [numbers [R] + C] * P1 ;}} else {psec [R] = pfir [R] + pfir [tag [numbers [R]-D + C] * P2 ;}} // complete an iteration // The probability of switching pmid = pfir; pfir = psec; psec = pmid; Pa = 0.0; psum = 0.0; For (V = 0; v <total; V ++) {// calculate the new probability if (Numbers [v]> = 0 & numbers [v] <= T) {psum + = pfir [v];} else if (numbers [v]> T) {pa + = pfir [v] ;}} if (psum <0.00001) {break ;}// complete n iterations} return Pa * 100;} // start prompt: the START unique identifier of the Automatic Marking. Do not delete or add it. Int main () {printf ("% d", WIN (5, 3, 2, 1, 0.7); // return 0 ;}// end // prompt: unique Identifier of the Automatic Marking end. Do not delete or add it.

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.