1. 程式人生 > >Gym - 101775L SOS —— 規律

Gym - 101775L SOS —— 規律

題意:

長度n的棋盤,依次向裡面放S或者O,誰下的時候形成了SOS誰就贏

Panda先手Sheep後手

思路:

這題真的蠻有趣

所有情況中,只有形成了S - - S這種情況時,不管再向裡面填了什麼,另一個人都會贏,這是這題的突破口

(1)當n小於7的時候,隨便模擬一下知道最優情況下都是平局,不會有一個人會讓另一個人贏

(2)當n為大於7的奇數時,先手必勝

以7為例:

先手    - - - S - - -

後手   O - - S - - -

先手   O - - S - -  S

這樣先手就先形成了S - - S這種情況,當n為奇數時,在S - - S外面一定還剩餘偶數個待填的位置,那麼一定是後手先填進S - - S裡,這樣先手就必贏

這樣我們可以看到,當出現了一個長度為7的空閒格且除了這7個外還剩餘偶數個時,先下進7格中間S位置的人就贏了

(3)當n為大於等於16的偶數時,後手必勝

首先先手不可能會贏,因為總數為偶數,S - - S外面會剩餘奇數個待填位置

這時會出現兩邊都有一個長度為7的空閒格,先手不想讓後手贏,只能堵住其中一個,但是不能全部堵住

以16為例:

先手 - - - O - - - - - - - - - - - - 

後手 - - - O - - - - - - - - S - - -

先手 - - - O - - - - - - - - S - - O

後手 - - - O - - - - - S - - S - - O

這樣後手先形成S - - S且待填位置還剩偶數個,一定會後手勝

(4)其他情況下兩者誰都贏不了,最優情況下只能平局

(5)當n=14時是比較特殊的,按上面的分析它也兩邊都有長度為7的空閒格,但是模擬一下會發現先手有不讓後手贏的策略

當n=14

先手 - - - - - - O - - - - - - -

後手 - - - - - - O - - - S - - -

先手- - - - - - O - - - S - - O

這時後手無法在下標8號位置填S,因為會與左側的O連起來讓先手勝,所以只能平局

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define max_ 100010
#define mod 1000000007
#define inf 0x3f3f3f3f
int casnum=1;
int n;
int main(int argc, char const *argv[]) {
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n<7)
        printf("Case #%d: Draw\n",casnum++ );
        else
        {
            if(n&1)
            printf("Case #%d: Panda\n",casnum++ );
            else
            {
                if(n>=16)
                printf("Case #%d: Sheep\n",casnum++ );
                else
                printf("Case #%d: Draw\n",casnum++ );
            }
        }
    }
    return 0;
}

L. SOS

time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

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

Copy

2
3
7

output

Copy

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.