Vasya and Chess - CodeForces 493 D 博弈

在n*n的棋盘上,白皇后位于(1,1),黑皇后位于(1,n),其余格子布满中立棋子。双方轮流行动,必须吃掉一枚棋子,无法行动者判负。本文分析了不同棋盘大小下的最优策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vasya and Chess
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess.

The queen is the piece that captures all squares on its vertical, horizontal and diagonal lines. If the cell is located on the same vertical, horizontal or diagonal line with queen, and the cell contains a piece of the enemy color, the queen is able to move to this square. After that the enemy's piece is removed from the board. The queen cannot move to a cell containing an enemy piece if there is some other piece between it and the queen.

There is an n × n chessboard. We'll denote a cell on the intersection of the r-th row and c-th column as (r, c). The square (1, 1)contains the white queen and the square (1, n) contains the black queen. All other squares contain green pawns that don't belong to anyone.

The players move in turns. The player that moves first plays for the white queen, his opponent plays for the black queen.

On each move the player has to capture some piece with his queen (that is, move to a square that contains either a green pawn or the enemy queen). The player loses if either he cannot capture any piece during his move or the opponent took his queen during the previous move.

Help Vasya determine who wins if both players play with an optimal strategy on the board n × n.

Input

The input contains a single number n (2 ≤ n ≤ 109) — the size of the board.

Output

On the first line print the answer to problem — string "white" or string "black", depending on who wins if the both players play optimally.

If the answer is "white", then you should also print two integers r and c representing the cell (r, c), where the first player should make his first move to win. If there are multiple such cells, print the one with the minimum r. If there are still multiple squares, print the one with the minimum c.

Sample test(s)
input
2
output
white
1 2
input
3
output
black
Note

In the first sample test the white queen can capture the black queen at the first move, so the white player wins.

In the second test from the statement if the white queen captures the green pawn located on the central vertical line, then it will be captured by the black queen during the next move. So the only move for the white player is to capture the green pawn located at (2, 1).

Similarly, the black queen doesn't have any other options but to capture the green pawn located at (2, 3), otherwise if it goes to the middle vertical line, it will be captured by the white queen.

During the next move the same thing happens — neither the white, nor the black queen has other options rather than to capture green pawns situated above them. Thus, the white queen ends up on square (3, 1), and the black queen ends up on square (3, 3).

In this situation the white queen has to capture any of the green pawns located on the middle vertical line, after that it will be captured by the black queen. Thus, the player who plays for the black queen wins.


题意:在n*n的棋盘上,(1,1)点为白皇后,(1,n)点为黑皇后,其他位置布满中立的棋子,由白色先手,当一个人没有棋子可以吃的时候,或是自己的棋子被对方吃掉之后,那么它就输了,问最后是谁赢。

思路:首先看当n是奇数的时候,我们可以把最中间的一列看做对称线,那么白棋无论怎么走,黑棋都走一样的,那么最后会是白棋先走到这个对称线上,所以黑棋胜。当n为偶数的时候,只要白棋先走(1,2)这个点,那么又变成了上面的情况,先手的胜负判定同上。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
    int T,t,n,m,i,j,k;
    scanf("%d",&n);
    if(n&1)
    {
        printf("black\n");
    }
    else
       printf("white\n1 2\n");

}



### 关于 Vasya 和多重集的编程竞赛算法题目解决方案 #### 题目描述 给定一个多重集合 `s`,目标是将其分割成两个新的多重集合 `a` 和 `b` (其中一个可以为空),使得这两个新集合中的“好数”的数量相等。“好数”定义为在一个特定多集中恰好只出现一次的数字。 为了实现这一目标,需要考虑如何有效地统计并分配这些元素到不同的子集中去[^1]。 #### 解决思路 一种有效的解决方法是从输入数据的特点出发思考。如果某个数值在整个原始集合中出现了偶数次,则该值可以在不影响最终结果的情况下被平均分入两个子集中;而对于那些仅出现奇数次数的情况,则必须小心处理以确保能够达成平衡条件——即让尽可能多的不同类型的单例项分别进入各自的目标组内[^2]。 具体来说: - 对于任何频率大于等于两次(无论是奇还是偶)的数据点而言,总是能通过适当划分来满足上述要求; - 当遇到频度为一的情形时,就需要额外注意了:因为这直接影响着能否成功创建具有相同数目唯一成员的新分区。 因此,在实际编码过程中应该优先处理那些重复率较高的项目,并记录下所有独一无二实例的位置以便后续操作使用。 #### Python 实现代码示例 下面是一个基于此逻辑编写的Python函数,用于求解这个问题: ```python from collections import Counter def can_split_equally(s): count = Counter(s) # 统计各元素出现次数 singletons = sum(1 for v in count.values() if v == 1) return singletons % 2 == 0 # 测试用例 test_cases = [ [1, 2, 2, 3], # True 可以分成 {1} 和 {2, 2, 3} [1, 2, 3, 4, 5], # False 单独存在的数字有五个无法平分 ] for case in test_cases: print(f"Input: {case}, Can Split Equally? :{can_split_equally(case)}") ``` 这个程序首先利用 `collections.Counter` 来计算每个整数在列表里边出现过的总次数。接着它会遍历所有的键值对,累积起所有只出现过一次(也就是所谓的 “好数” 或者说是单一实例)的数量。最后一步就是判断这样的特殊案例是不是构成了一个偶数序列长度 —— 如果是的话就意味着存在至少一组可行解;反之则不存在这样的一分为二方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值