解题思路
当一个环无法被涂上任何颜色的时候,可以推断,——颜色一定是交替出现的。
因为如果两个相同颜色连续,必定中间存在至少一个空位,这时另一种颜色必然会填上这个位置。
所以,当一个环无法涂上任何颜色时,颜色出现的数量一定是偶数。所以后手一定会赢。 只有在环的大小为 1 时,先手可以赢。
所以统计环大小为1的个数,若为奇数,就先手胜。
代码
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int T,n,a[1010];
int main(){
freopen("chess.in","r",stdin);
freopen("chess.out","w",stdout);
scanf("%d",&T);
while(T--)
{
int w=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1)
w++;
}
if(w%2!=0)
printf("rabbit\n");
else printf("hamster\n");
}
}