nyoj 518 ball game

ball game

Time Limit: 1000  ms | Memory Limit: 65535  KB
Difficulty: 2
describe

    There are n balls in the box. Two people, A and B, take turns to take balls from the box. Each person can see how many balls the other person has taken and how many are left in the box. They're all smart and don't make wrong judgments.

    We agree that
    the number of balls each person takes out of the box must be: 1, 3, 7 or 8.

    You can't abstain when it's your turn to take the ball!

    A takes the ball first, then alternately takes the ball until it is finished.

    The side forced to get the last ball is the loser (lose side)
   

    Please program to determine whether A can win for a given initial number of balls without either side judging wrongly?

enter
The first is an integer n (n<100), which means that there are n integers next. Then there are n integers, each on a line (integer < 10000), representing the initial number of balls.
output
The program outputs n lines, indicating the winning or losing of A (0 for loss, 1 for win).
sample input
4
1
2
10
18
Sample output
0
1
1
0
source
2012 Blue Bridge Cup-10

   

Problem solving ideas:
1. To set the number of balls taken each time as ai (i<=4) a1 = 1, a2 = 3, a3 = 7, a4 = 8
2. The number of balls is n, and there are players A and B. At the beginning, A holds the first move. In this game, after the first player A takes ai balls, the number of remaining balls becomes n - ai (n - ai > 0);
The problem at this time is transformed into: the total number of balls is n - ai, and the problem of B playing the first hand;
重复这个步骤,容易得到求解策略:即执先手者只需考虑执行一次取球操作后,在剩余n-ai个球的问题中如何让先手必败.
3.举个栗子:

1 2 3 4 5 6 7 8 9 10
0 1 0 1 0 1 0 1 1  1

a.当球数为1,毫无疑问,先手必败;
b.球数为2,此时执先手者考虑如何取一次球后,在2-ai个球的游戏里,先手必败; 不难看出,当i = 1, 2 - a1 = 1, 1的状态下先手必败,即先手取一个球必胜;
c.球数为3,3-a1 == 2, 先手必胜,不行;3 - a2 = 0, 不满足条件;因此在这一种情况下必败;
依次类推,可推理出球数为任意n的博弈情况......

递推关系:if (f(n-ai) == 0) f(n) = 1(i = 1, 2, 3, 4且n-ai > 0);

代码如下:


#include<stdio.h>
int main() {
  int a[] = {0, 1, 3, 7, 8}, b[10010] = {0, 0};
  for (int i = 2; i <= 10010; i++) {
    b[i] = 0;
    for (int j = 1; j <= 4; j++) {
      if (b[i-a[j]] == 0 && i - a[j] > 0) {
        b[i] = 1;
        break;
      }
    }
  }
  int t, n;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    printf("%d\n", b[n]);
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685399&siteId=291194637