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");
    }
}


目录
相关文章
|
8月前
UVa11565 - Simple Equations
UVa11565 - Simple Equations
35 0
|
算法 索引
LeetCode 55. Jump Game
给定一个非负整数数组,您最初定位在数组的第一个索引处。 数组中的每个元素表示该位置的最大跳转长度。 确定您是否能够到达最后一个索引。
71 0
LeetCode 55. Jump Game
|
算法 索引
LeetCode 45. Jump Game II
给定一个非负整数数组,初始位置在索引为0的位置,数组中的每个元素表示该位置的能够跳转的最大部署。目标是以最小跳跃次数到达最后一个位置(索引)。
50 0
LeetCode 45. Jump Game II
|
人工智能 算法 大数据
|
Java
HDU 5882 Balanced Game
Balanced Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 508    Accepted Submission(s): ...
816 0
LeetCode - 45. Jump Game II
45. Jump Game II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组a,玩家的初始位置在idx=0,玩家需要到达的位置是idx=a.
918 0
|
C++ Python
[LeetCode] Jump Game II
This problem has a nice BFS structure. Let's illustrate it using the example nums = [2, 3, 1, 1, 4] in the problem statement.
885 0