2017-2018 ACM-ICPC Asia East Continent League Final (ECL-Final 2017) L. SOS(博弈题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37025443/article/details/83791302

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.

题意:

两个人Panda,Sheep,轮流往一个1*n的矩阵里面放‘S’、‘O’。

当一个人放完之后,如果矩阵从左到右出现连续的"SOS",那么这个人就获胜

如果当所有空格都被填满之后,都没有"SOS",那么平局

Panda先手,两个人都采用最有策略

解析:

这道题...差一点点就做出来了...,我把边界提前了,应该是16及其以后的偶数是Sheep获胜,我提前成14及其以后的偶数....

这道题的时间也给人很大的迷惑...2s??我后来以为是sg函数做,就放弃了....


这道题一个很关键的地方就是SXXS是先手必败的,摆出这个局之后,谁先在这个局里面放字谁就输

6之前的矩阵,都是先手都无法必胜,都可以被搅局。

但在6之后的奇数n,先手只要在中间放S,然后在摆出上面的局面,那么最后一定是后手最先在局面里面放字

例如n=7时

对于6之后的偶数,只要摆出SXXS的局面,后手时一定必胜的。

但是对于n<=14的偶数,先手可以在中间放一个O来破环后手摆出上述的局面,

例如n=14

当n>14的偶数,即使中间放了O,两边依然右足够多的空格得到SXXS的局面,后手只需要选择连续的7个空格

并且该空格不与O相接,中间放S则就可以得到必胜.例如n=16

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;

const int MAX = 2e5+100;

int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        cas++;
        int n;
        scanf("%d",&n);
        printf("Case #%d: ",cas);
        if(n<=6||(n%2==0&&n<16)) printf("Draw\n");
        else if(n%2==1)printf("Panda\n");
        else printf("Sheep\n");

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/83791302