两个人玩取球的游戏。
一共有 N 个球,每人轮流取球,每次可取集合 {n1,n2,n3} 中的任何一个数目。
如果无法继续取球,则游戏结束。
此时,持有奇数个球的一方获胜。
如果两人都是奇数,则为平局。
假设双方都采用最聪明的取法,
第一个取球的人一定能赢吗?
试编程解决这个问题。
输入格式:
第一行 3 个正整数 n1 n2 n3,空格分开,表示每次可取的数目 (0<n1,n2,n3<100)
第二行 5 个正整数 x1 x2 ... x5,空格分开,表示 5 局的初始球数 (0<xi<1000)
输出格式:
一行 5 个字符,空格分开。分别表示每局先取球的人能否获胜。
能获胜则输出 +,
次之,如有办法逼平对手,输出 0,
无论如何都会输,则输出 -
例如,输入:
1 2 3
1 2 3 4 5
程序应该输出:
+ 0 + 0 -
再例如,输入:
1 4 5
10 11 12 13 15
程序应该输出:
0 - 0 + +
再例如,输入:
2 3 5
7 8 9 10 11
程序应该输出:
+ 0 0 0 0
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU 消耗 < 3000ms
对于一个局面 $(num, me, you)$ 表示当前剩下 $num$ 个球,你手上有 $you$ 个,我手上有 $me$ 个,当前这个局面可以 DFS 出三个局面 $(num-arr [0], you, me + arr [0]),\ (num - arr [1], you, me + arr [1]),\ (num - arr [2], you, me + arr [2])$,我取完球之后,$(num, me, you)$ 局面留给对方,那么对于对方来说,他的局面就是 $(num, you, me)$,上面的三个 DFS 局面就是这么来的
考虑什么时候搜索停止,假设 $arr$ 数组按照升序排好,最小的是 $arr [0]$,那么当 $num < arr [0]$ 时,双方都拿不了了,此时要进行结算,结算就是看此时双方手里球数量的奇偶性
首先给出 DFS 代码
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class Main {
-
- static int[] arr;
- static char res;
-
- public static void main(String[] args) {
- Scanner cin = new Scanner(System.in);
- arr = new int[3];
- arr[0] = cin.nextInt();
- arr[1] = cin.nextInt();
- arr[2] = cin.nextInt();
- Arrays.sort(arr);
- for (int i = 0; i < 5; i++) {
- int num = cin.nextInt();
- res = dfs(num, 0, 0);
- System.out.print(res + " ");
- }
- System.out.println();
- }
-
- static char dfs(int num, int me, int you) {
- if (num < arr[0]) {
- if ((me & 1) == 1) {
- if ((you & 1) == 1)
- return '0';
- else
- return '+';
- } else {
- if ((you & 1) == 1)
- return '-';
- else
- return '0';
- }
- }
- boolean ping = false;
- for (int i = 0; i < 3; i++) {
- if (num >= arr[i]) {
- res = dfs(num - arr[i], you, me + arr[i]);
- if (res == '-')
- return '+';
- if (res == '0')
- ping = true;
- }
- }
- // 如果能走到这行,说明不存在对手输的情况
- if (ping)
- return '0';
- return '-';
- }
- }
这个搜索代码无法通过全部测试用例,原因是会超时,所以考虑记忆化搜索,如果对于一个局面 $(num, me, you)$ 返回结果,此时将结果存在一个三维数组 $cache [num][me][you]$ 中,下次如果遇到相同的值,就直接返回这个结果。
但是这么做还不够,原因是 $me$ 和 $you$ 的值范围很大,可能很难同时达到 $num, me, you$ 同时都在缓存中记录的情况。
因此换一种想法,只记录当前局面的奇偶性,参数还是 $(num, me, you)$,只不过此时 $me$ 和 $you$ 的值只可能是 1 或 0,1 表示奇数,0 表示偶数。根据奇偶相加的性质,可以推出新的局面变化,假设当前局面是 (num, me, you)
,那么新的局面是 (num - arr[i], you, (arr[i] & 1) == 0 ? me : (1 - me))
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class Main {
-
- static int[] arr;
- static char res;
- static char[][][] cache = new char[1000][2][2];
-
- public static void main(String[] args) {
- Scanner cin = new Scanner(System.in);
- arr = new int[3];
- arr[0] = cin.nextInt();
- arr[1] = cin.nextInt();
- arr[2] = cin.nextInt();
- Arrays.sort(arr);
- for (int i = 0; i < 5; i++) {
- int num = cin.nextInt();
- res = dfs(num, 0, 0);
- System.out.print(res + " ");
- }
- System.out.println();
- }
-
- static char dfs(int num, int me, int you) {
- if (num < arr[0]) {
- if ((me & 1) == 1) {
- if ((you & 1) == 1)
- return cache[num][me][you] = '0';
- else
- return cache[num][me][you] = '+';
- } else {
- if ((you & 1) == 1)
- return cache[num][me][you] = '-';
- else
- return cache[num][me][you] = '0';
- }
- }
- if (cache[num][me][you] != '\0')
- return cache[num][me][you];
- boolean ping = false;
- for (int i = 0; i < 3; i++) {
- if (num >= arr[i]) {
- res = dfs(num - arr[i], you, (arr[i] & 1) == 0 ? me : (1 - me));
- if (res == '-')
- return cache[num][me][you] = '+';
- if (res == '0')
- ping = true;
- }
- }
- // 如果能走到这行,说明不存在对手输的情况
- if (ping)
- return cache[num][me][you] = '0';
- return cache[num][me][you] = '-';
- }
- }