Description
Karlsson has visited Lillebror again. They found a box of chocolates and a big whipped cream cake at Lillebror's place. Karlsson immediately suggested to divide the sweets fairly between Lillebror and himself. Specifically, to play together a game he has just invented with the chocolates. The winner will get the cake as a reward.
The box of chocolates has the form of a hexagon. It contains 19 cells for the chocolates, some of which contain a chocolate. The players move in turns. During one move it is allowed to eat one or several chocolates that lay in the neighboring cells on one line, parallel to one of the box's sides. The picture below shows the examples of allowed moves and of an unacceptable one. The player who cannot make a move loses.
Karlsson makes the first move as he is Lillebror's guest and not vice versa. The players play optimally. Determine who will get the cake.
Input
The input data contains 5 lines, containing 19 words consisting of one symbol. The word "O" means that the cell contains a chocolate and a "." stands for an empty cell. It is guaranteed that the box contains at least one chocolate. See the examples for better understanding.
Output
If Karlsson gets the cake, print "Karlsson" (without the quotes), otherwise print "Lillebror" (yet again without the quotes).
Sample Input
. . . . . O . . . O O . . . . . . . .
Lillebror
. . . . . . O . . . O . O . O . . O .
Karlsson
思路:总共就19个字符,用二进制压缩,然后枚举记忆化搜索,很像博弈的哪种。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int kx[]={1,1,1,2,3};///搜索范围
const int ky[]={3,4,5,5,5};
const int dx[]={1,0,1};
const int dy[]={0,1,1};
char s[55][55];
int h[1<<22];
bool ok()
{ int dex=0;
for(int i=0;i<5;++i)///找出当前状态
for(int j=kx[i];j<=ky[i];++j)
{dex<<=1;
if(s[i][j]=='O')
dex^=1;
}
int&ret=h[dex];///记忆化数组
if(ret==0)
{ ret=-1;
for(int i=0;ret<0&&i<5;++i)
for(int j=kx[i];ret<0&&j<=ky[i];++j)
for(int k=0;ret<0&&k<3;++k)
{int tx,ty;
tx=i;ty=j;
if(s[tx][ty]!='O')continue;
while(s[tx][ty]=='O')
{ s[tx][ty]='.';
tx+=dx[k];ty+=dy[k];
if(!ok()){ret=1;break;}///只要有满足的路径到必败点就是必胜点
}
while(tx^i||ty^j)///未找到还原状态
{
tx-=dx[k];ty-=dy[k];
s[tx][ty]='O';
}
s[tx][ty]='O';
}
}
return ret>0;
}
int main()
{ memset(h,0,sizeof(h));
for(int i=0;i<5;++i)
for(int j=kx[i];j<=ky[i];++j)
scanf(" %c",&s[i][j]);
/** for(int i=0;i<5;++i,puts(""))
for(int j=kx[i];j<=ky[i];++j)
printf(" %c",s[i][j]);*/
if(ok())cout<<"Karlsson\n";
else cout<<"Lillebror\n";
}