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");
    }
}
时间: 2024-08-03 09:12:49

uva 141 The Spot Game hash的相关文章

uva 141 - The Spot Game

点击打开链接 题目意思:     有两个人在玩游戏,游戏是在长为N的正方形棋盘,上面是由一序列格子组成,现在输入一些操作指令由两个人轮流操作,每一次操作对应的棋盘上面会有一个状态,如果当前的状态或这个状态旋转90或180度而来的状态以及出现过,那么这个人就输,如果当所有指令操作完以后还没分胜负就是平局 解题思路:       set+状态判重即可,注意对set里面放结构体的使用,重载<号.                        1:我们知道对于set和map而言,插入的数据默认是那些常见

UVa 141:The Spot Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=77 类型: 哈希判重 + 模拟 原题: The game of Spot is played on an NxN board as shown below for N = 4. During the game, alternate players may either place

算法题:UVA 10604 (记忆化搜索 + hash)

In a chemist's lab, there are several types of chemicals in tubes. The chemist wants to mix all these chemicals together, two chemicals at a time. Whenever two chemicals are mixed, some heat is generated and released into the air and the mixed chemic

UVa 188:Perfect Hash

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=124 类型: 哈希 原题: Perfect Software, Inc. has obtained a government contract to examine text flowing through a high-speed network

UVa 704:Colour Hash, 双向bfs

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=110&page=show_problem&problem=645 类型:隐式图搜索,双向bfs 原题: This puzzle consists of two wheels. Both wheels can rotate both clock and counter-clockwise. They contai

uva 10591 - Happy Number hash

   非常简单的题,易知平方和后不超过800,开个800的数组,预处理,然后直接定址就可以了,不过要注意a和an的区别 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #in

uva 188 - Perfect Hash 模拟

       这题只要读懂题就等于没难度了,完全照着题目给的公式模拟即可      要注意的就是末尾的多余空格.   /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #inc

uva 704 - Colour Hash map+双向bfs

     这题从上周日开始想写的,结果拖了一周--现在写题效率越来越低了,要计时训练了.      一道很复杂的隐式图搜索--状态数有24位,看到网上有人按11进制转换,然后对Hash_MAX取模,个人认为这种做法不靠谱.于是把状态变成string类型的,直接用map,对于map,24位的string效率还是非常高的.     搜索的时候双向bfs,因为比较懒,所以把反向和正向写一起了,其实逆向只用做一次就可以了.     还有一点要注意的就是要输出数字最小的一种变换序号,这个处理方法就是把一层

uva 188 - Perfect Hash

点击打开链接 题目意思: 说这个题目之前先让我喷喷,太尼玛恶心了一道水题装B 的好像很有技术含量,真的是恶心.                    题目给定一个字符串.每一个字符串由多个单词组成,现在有一个计算法则" a = 1 , b = 2 ...... z = 26   ,'a' = 1 , "bz" = 26*32^0 + 2*32^1 = 90 类似32进制的转换" 然后要求我们去求出每一单词的值w ,排序满足 W1<W2<.......&l