题目描述
- 运用一个结构体,把每种棋盘状态存到一个set中,对每次输入后的棋盘状态进行比较,判断是否出现过
或者对输入的棋盘进行旋转也可以 - 由于set的具体实现采用了红黑树的平衡二叉树的数据结构,所以,set中的元素就有大小比较。重载函数bool operator < (const spot& a, const spot& b),以比较两个结构的大小,便于在set中插入和查找元素。
#include<set>
#include<cstring>
#include<iostream>
using namespace std;
struct spot{
bool arr[52][52];
};
int n;
bool operator<(const spot& a,const spot& b){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a.arr[i][j]<b.arr[i][j]) return true;
else if(a.arr[i][j]>b.arr[i][j]) return false;
return false;
}
void change(spot& a){
spot p;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
p.arr[i][j]=a.arr[j][n+1-i];
a=p;
}
int main(){
int x,y;
char c;
spot p;
while(cin>>n&&n){
bool flag=false;
set<spot>s;
memset(p.arr,false,sizeof(p.arr));
int count=0;
for(int num=1;num<=2*n;num++){
cin>>x>>y>>c;
if(flag)
continue;
if(c=='+')
p.arr[x][y]=true;
else
p.arr[x][y]=false;
if(s.count(p)){
flag=true;
count=num;
continue;
}
spot t(p);
for(int j=0;j<4;j++){
s.insert(t);
change(t);
}
}
if(flag)
if(count%2==0)
cout<<"Player 1 wins on move "<<count<<endl;
else
cout<<"Player 2 wins on move "<<count<<endl;
else
cout<<"Draw"<<endl;
}
}