您的位置:首页 > 其它

[哈希]The Spot Game uva141

2014-02-07 21:23 295 查看


 The Spot Game 
The game of Spot is played on an NxN board as shown below for N = 4. During the game, alternate players may either place a black counter (spot) in an empty square or remove one from the board, thus producing a variety
of patterns. If a board pattern (or its rotation by 90 degrees or 180 degrees) is repeated during a game, the player producing that pattern loses and the other player wins. The game terminates in a draw after 2N moves if no duplicate pattern is produced before
then.
Consider the following patterns:



If the first pattern had been produced earlier, then any of the following three patterns (plus one other not shown) would terminate the game, whereas the last one would not.

Input and Output

Input will consist of a series of games, each consisting of the size of the board, N (2 

 N 

 50)
followed, on separate lines, by 2N moves, whether they are all necessary or not. Each move will consist of the coordinates of a square (integers in the range 1..N) followed by a blank and a character `+' or `-' indicating the addition or removal of a spot
respectively. You may assume that all moves are legal, that is there will never be an attempt to place a spot on an occupied square, nor to remove a non-existent spot. Input will be terminated by a zero (0).

Output will consist of one line for each game indicating which player won and on which move, or that the game ended in a draw.

Sample input

2
1 1 +
2 2 +
2 2 -
1 2 +
2
1 1 +
2 2 +
1 2 +
2 2 -
0


Sample output

Player 2 wins on move 3
Draw


题意:放石头游戏(The game of Spot)在一块NxN的板子上进行,如下图为N=4的板子。游戏的玩法是两个玩家轮流放一块石头在空的格子上,或是可以从板子上拿一块石头起来,游戏的进行中可以发现,板子上石头的布局会不断变化,当一玩家排出已重复出现过的布局时,他就算输了这一局(一种布局如果将之旋转90度、180度、270度亦视为相同的布局)。若在2N步内未出现过相同的布局就算和局。

一开始就看错了题目,以为棋盘是4*4的,没看到n*n,wa了无数次,其中哈希函数也写错了。。一道简单的哈希题花了很久的时间。

#include<iostream>
#include<cstring>
#include<set>

using namespace std;

char grid[55][55];
set<string> vis;
int n;

int try_to_insert()
{
string str="";
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
str=str+grid[i][j];
}
}
if(vis.count(str)) return 0;
vis.insert(str);
str="";
for(i=n-1;i>=0;i--)
{
for(j=0;j<n;j++)
{
str=str+grid[i][j];
}
}
if(!vis.count(str)) vis.insert(str);
str="";
for(i=n-1;i>=0;i--)
{
for(j=n-1;j>=0;j--)
{
str=str+grid[i][j];
}
}
if(!vis.count(str)) vis.insert(str);
str="";
for(i=0;i<n;i++)
{
for(j=n-1;j>=0;j--)
{
str=str+grid[i][j];
}
}
if(!vis.count(str)) vis.insert(str);
return 1;
}

int main()
{
while(cin>>n&&n)
{
int flag=1,winner,num;
int i,x,y;
char ch;
vis.clear();
memset(grid,'0',sizeof(grid));
for(i=0;i<2*n;i++)
{
cin>>x>>y>>ch;
if(flag==0) continue;
if(ch=='+') grid[x-1][y-1]='1';
else if(ch=='-') grid[x-1][y-1]='0';
if(try_to_insert()==0)
{
flag=0;
num=i+1;
if(i%2==0) winner=2;
else winner=1;
cout<<"Player "<<winner<<" wins on move "<<num<<endl;
}
}
if(flag) cout<<"Draw"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hash