東華大學2020年程序設計競賽(同步賽)F.A Simple Game
題目描述
Alice and Bob play a game.Initially they have n binary strings (a string which consists of zeroes and ones). They take alternating turns, and Alice is moving first. During each turn, the player has to choose several (at least one) strings and do one of the following operations (you can perform different operations on different strings):
1:choose a character ‘1’, and replace it with ‘0’.
2:choose a contiguous substring consisting only of characters ‘0’, and replace it with ‘1’(The length of the substring is at least 2).
For example, if s = “11000”,then after operation 1 s can turn into “01000” or “10000”. if s = “11000”, then after operation 2 s can turn into “1110”, “1101” or “111”.
Whoever is unable to choose,loses.You have to determine who wins if they both play optimally.
輸入描述:
The first line contains one integer t (1≤ t ≤100) — the number of test cases. Then the test cases follow.
For each test case, the first line contains one integern(1 ≤ n ≤ 10).
There arenlines following, the i-th of which contains a binary string of length not more than 1000.
輸出描述:
For each test case print sdzNB if Alice can win and kgNB otherwise.
示例1
輸入
2
2
00
1
3
11
11
11
輸出
sdzNB
kgNB
簡單博弈~
我們首先考慮只有一個字符串的博弈策略~
不難發現,對當前操作的那個人字符串如果只包含 ,那麼必輸,不理解的可以推理一下 或者 的情況,後面多個 的情況都可以通過前面遞推 ,那麼最優策略就很簡單了,只要把 轉化爲 即可,儘可能讓對面操作時僅剩下 就一定能贏~
知道最優博弈策略後就很簡單了,對一個字符串只需判斷其含字符 的個數即可,奇數 贏,反之則輸~
那麼推廣到多個字符串怎麼判斷呢,我是構造了幾個簡單的例子發現其實和一個字符串答案一樣,只要存在一個含奇數個 的字符串 一定能贏。我是這麼理解的, 可以與 先把偶數字符串走掉,此時最後肯定是 先手,她只需要操作那個含奇數個 的字符串就能贏了~
AC代碼如下:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int f(string s){
int cnt=0;
for(char c:s)
if(c=='1') cnt++;
return cnt%2;
}
int main(){
int t,n;
cin>>t;
while(t--){
string s;
cin>>n;
int num=0;
for(int i=0;i<n;i++) cin>>s,num+=f(s);
if(num) puts("sdzNB");
else puts("kgNB");
}
}