UVA 141 The Spot Game

题目描述

在这里插入图片描述
在这里插入图片描述

  • 运用一个结构体,把每种棋盘状态存到一个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;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值