The Spot Game +hash表

 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
解决方案:要注意几个点:1)每个图有四种情况;2)不是4x4;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<string ,int > Hash;
int matrix[55][55];
int N;
void hash(char a[],char b[],char c[],char d[]){
    int s=0;
    for(int i=1;i<=N;i++)
    for(int j=1;j<=N;j++){

    a[s++]=char(matrix[i][j]+'0');
    }
    a[s]='\0';
    s=0;
    for(int j=1;j<=N;j++)
    for(int i=N;i>=1;i--){

    b[s++]=char(matrix[i][j]+'0');
    }
     b[s]='\0';
    s=0;
    for(int j=N;j>=1;j--)
    for(int i=1;i<=N;i++){

     c[s++]=char(matrix[i][j]+'0');
    }
     c[s]='\0';
    s=0;
    for(int i=N;i>=1;i--)
    for(int j=N;j>=1;j--){

    d[s++]=char(matrix[i][j]+'0');
    }
     d[s]='\0';


}
int main(){

    while(~scanf("%d",&N)&&N){

        memset(matrix,0,sizeof(matrix));
        Hash.clear();
        int win=-1,step=-1,ex=0;
    for(int i=0;i<2*N;i++){
    int x,y;
    char C;
    scanf("%d %d %c",&x,&y,&C);
    if(C=='+') matrix[x][y]=1;
    else matrix[x][y]=0;
    char a[2555],b[2555],c[2555],d[2555];
    hash(a,b,c,d);
   // cout<<a<<endl;
    if(!Hash[a]&&!Hash[b]&&!Hash[c]&&!Hash[d])
    {Hash[a]=1;Hash[b]=1;Hash[c]=1;Hash[d]=1;ex^=1;}
    else if(win==-1&&step==-1&&(Hash[a]||Hash[b]||Hash[c]||Hash[d])){
    win=ex;step=i+1;
    }
    }
    if(win!=-1&&step!=-1)
    printf("Player %d wins on move %d\n",2-win,step);
    else {
    printf("Draw\n");
    }
    }
    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值