Gaming DP: PIPI's Bomb

Gaming DP: PIPI's Bomb

question

insert image description here
insert image description here

train of thought

  This question is a game-like dynamic programming problem. Generally speaking, this type of problem is in the following form: the game is a game between two parties, one party acts first according to the rules, and after the action, hand over the current situation to the other party, and the other party continues to act according to the rules. When you're done, turn the situation back over again. By analogy, the two parties take turns to act until certain conditions are met, then one side wins or loses.
  The key to this type of problem lies in the switch of the first move. For example, for the current first mover, after he performs an operation, he will hand over a certain situation to the opponent. Then the opponent will face a new situation with the first move. Therefore, the DP array needs to save whether a certain situation will lead to the current first-hand win or first-hand loss.
  Taking this question as an example, we set the state DP[i], and DP[i] is 1, which means that when the countdown is i, the first mover at this time must win, and if it is 0, the first mover at this time must lose. Obviously DP[0] is 0, when the countdown is 0, the first mover is killed, and the first mover must lose. When i>0 && i<=aboth parties cannot speed up the time (because they will be killed by speeding up the time), they can only spend 1 second to give the bomb to others, then DP[i]=DP[i-1]^1, here is an explanation:

^ means XOR operation, if DP[i - 1] = 1, the first move wins when the countdown is i - 1, then the first mover must lose when the countdown is i, that is, DP[i] = 0, because the first mover in the next situation is The backhand of the previous situation. For example, when the countdown is 3, A will win first, then we can reverse the countdown to 4, B spends 1 second to give the bomb to A, and B will lose

  When i>a && i<=b+1, the first mover can always speed up to only 1s left, and then spend 1s to blow up the opponent to death, then DP[i]=1. At that timei > b+1 , at this time, the first player can choose to adjust the speed or not. If you choose not to speed up, the next situation is DP[i - 1]. If you choose to speed up, the next situation is that as long as DP[i - 1] DP[i-j-1](j >= a && j <= b)and If one of DP[ij-1] is 0, the current first player must win, DP[i] = 1, otherwise no matter how the current first player plays out, he will lose, DP[i] = 0.
  Finally, just judge DP[x] to get the answer.
  In the above state transition process, whether the current situation wins or loses depends on the outcome of the situation after the operation. Even if we only know the value DP[0] = 0 of one element of the DP array at the beginning, we can go through repeated inversions , calculate the outcome of all situations like Yuguo Suoyin in the law of causality.

the code

import java.util.*;

public class Main {
    
    
    // 1表示先手胜,0表示先手负
    static int[] dp = new int[1000005];
    public static void main(String[] args) {
    
    
        int x, a, b, i, j;
        Scanner scanner = new Scanner(System.in);
        x = scanner.nextInt();
        a = scanner.nextInt();
        b = scanner.nextInt();
        dp[0] = 0;
        for (i = 1; i <= x; i++) {
    
    
            if (i <= a) {
    
    
                dp[i] = dp[i - 1] ^ 1;
            } else if (i <= b + 1) {
    
    
                dp[i] = 1;
            } else {
    
    
                dp[i] = 0;
                if (dp[i - 1] == 0) {
    
    
                    dp[i] = 1;
                } else {
    
    
                    for (j = a; j <= b; j++) {
    
    
                        if (dp[i - j - 1] == 0) {
    
    
                            dp[i] = 1;
                            break;
                        }
                    }
                }
            }
        }
        System.out.println(dp[x] == 1 ? "PIPI" : "POPO");
    }
}

Guess you like

Origin blog.csdn.net/qq_44709990/article/details/121873862