[哈希]The Spot Game uva141

本文深入探讨了放石头游戏的规则、胜负条件及策略。通过分析游戏板上石头布局的变化,揭示了如何避免重复布局以赢得游戏。文章详细介绍了如何利用哈希函数来追踪布局变化,以及在特定情况下如何判断游戏结束。通过实例输入输出,阐述了游戏的复杂性和解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



 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:

picture23

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 tex2html_wrap_inline180 N tex2html_wrap_inline180 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值