uva 141 The Spot Game hash

简介:

  很简单的hash,可以知道只需要保存每行每列的总个数即可唯一确定图,所以只需开两个50的数组即可,然后对最长为100的字符串进行hash,一开始想直接ELF,但后来发现还是有重复,懒得写拉链法了,直接用map,0.008s就过了


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
using namespace std;
int num[2][51];
char str[101];
int n,N;
map<string,int> h;
bool check()
{
    int i,j,nn,k,t;
    for(i=j=nn=0;i<N;i++)//0
    {
        str[i]=num[nn][j++]+1;//加1使其不会被截断
        if(j>=n){j=0;nn++;}
    }
    if(h[str])return 0;
    for(i=j=0,t=nn=1;i<N;i++)//90
    {
        str[i]=num[nn][j]+1;
        j+=t;
        if(j>=n){j=n-1;nn--;t=-1;}
    }
    if(h[str])return 0;
    for(i=nn=0,j=n-1;i<N;i++)//180
    {
        str[i]=num[nn][j--]+1;
        if(j<0){j=n-1;nn++;}
    }
    if(h[str])return 0;
    for(i=0,j=n-1,t=-1,nn=1;i<N;i++)//270
    {
        str[i]=num[nn][j]+1;
        j+=t;
        if(j<0){j=0;nn--;t=1;}
    }
    if(h[str])return 0;
    else h[str]=1;
    return 1;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        h.clear();
        memset(num,0,sizeof(num));
        N=n<<1;
        int i,a,b,t;
        bool flag=0;
        char c;
        for(i=0;i<N;i++)
        {
            scanf("%d%d %c",&a,&b,&c);
            if(flag)continue;
            if(c=='-')t=-1;
            else t=1;
            num[0][a-1]+=t;
            num[1][b-1]+=t;
            if(!check())
            {
              printf("Player %d wins on move %d\n",(i+1)%2+1,i+1);
              flag=1;
            }
        }
        if(!flag)printf("Draw\n");
    }
}


目录
相关文章
|
算法
uva 10891 game of sum
题目链接 详细请参考刘汝佳《算法竞赛入门经典训练指南》 p67
36 0
uva10035 Primary Arithmetic
uva10035 Primary Arithmetic
34 0
最大乘积(Maximum Product,UVA 11059)
Problem D - Maximum Product Time Limit: 1 second   Given a sequence of integers S = {S1, S2, .
909 0
|
机器学习/深度学习

热门文章

最新文章