Gym - 101775L SOS —— 规律

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

题意:

长度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.

猜你喜欢

转载自blog.csdn.net/Lngxling/article/details/82905435
SOS
今日推荐