Gym - 101775L - SOS Gym (博弈)

版权声明:沃斯里德小浩浩啊 https://blog.csdn.net/Healer66/article/details/82555979

Mr. Panda and Mr. Sheep are playing a game on a 1 × N game board. Initially, all the cells are empty. Mr. Panda and Mr. Sheep take alternate moves and Mr.Panda moves first.

On each move, a player must fill an 'S' or 'O' into an empty cell. After the move, if there are 3 consecutive cells from left to right form the word "SOS", the player wins the game and the game finished.

If the board gets filled up without SOS appearing consecutively, no one wins the game, the game end with draw.

Now, if both Mr. Panda and Mr. Sheep play optimally, find out the result of the game.

Input

The first line of the input gives the number of test cases, TT test cases follow.

Each test case contains one line consists of one number N, indicating the size of the game board.

  • 1 ≤ T ≤ 1000.
  • 1 ≤ N ≤ 1000.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the game result if both players play optimally. "Panda" for Mr. Panda winning the game, "Sheep" for Mr. Sheep winning the game, "Draw" for draw game.

Example

Input

2
3
7

Output

Case #1: Draw
Case #2: Panda

Note

In the first test case, as there are only 3 cells, both players cannot win as Mr. Sheep only has one move and after his move, there is still one cell left empty. So Mr. Sheep is impossible to win, but it's easy for him to avoid Mr. Panda to win. He can either fill 'O' in the first or third cell, or fill 'S' in the second cell to make it a draw game.

题意:

1 x N的棋盘,熊猫和羊随机选择空地 下‘S’或者‘O’,谁先拼出sos谁赢,熊猫先手,下满了不分胜负输出draw。

思路:

首先s_ _s是个必胜态,因而N = 7是panda赢(中间下S,必是sheep先迈入s_ _s),然后发现大于7的奇数其实都是panda赢(虽说sheep可以在panda下s后率先构造出s_ _s态,但是先进入s_ _s的也必是sheep,这个下下就知道了)。

然后是大于七的偶数情况,如果panda先下s,sheep会先构造出必胜态,由于剩下的空格是偶数个,所以变成了panda先迈入s_ _s,所以panda不可能先下s,只能先下o,如果下了o,剩下的空格数必是奇数,此时sheep如何必胜呢?答案是连续的空格数大于等于7就行,等价于奇数个空格sheep先手,这个最小的偶数是多少呢?答案是16(不管怎么分割都最少留7个连续空格)

总结一下,大于等于7的奇数先手的panda必胜;大于等于16的偶数先手panda(必下o)留给后手sheep存在连续空格大于等于7的一共奇数个格子,sheep必胜;其余平局。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,t,cas=0;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        if(n>=16&&n%2==0) printf("Case #%d: Sheep\n",++cas);
        else if(n>=7&&(n&1))printf("Case #%d: Panda\n",++cas);
        else printf("Case #%d: Draw\n",++cas);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Healer66/article/details/82555979