你知道吗,
我们只要记录每行和每列的点数就可以确定一个斑点分布图,这样以来我们就把空间复杂度由O(n*n)降低到O(n+n);
我开始的时候想要写哈希判重,我确实没想到怎么够到哈希,所以就采用了以为大神的方法,
直接用hashmap函数解决,
另外此题的图有个陷阱。如果不读题直接做的话肯定会掉进陷阱里,题目中的五幅图只有前四副有用,且还有一副没有画出,最后一副是干扰的,
代码如下:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <map>
using namespace std;
int n, num[2][55];
char str[101];
map<string,int> hash;
int judge()
{
int cnt = 0;
for(int i = 1; i <= n; i++) str[cnt++] = num[0][i]+1;
for(int i = 1; i <= n; i++) str[cnt++] = num[1][i]+1;
str[n+n] = 0;
if(hash[str]) return 1;
else cnt = 0;
for(int i = n; i >= 1; i--) str[cnt++] = num[1][i]+1;
for(int i = 1; i <= n; i++) str[cnt++] = num[0][i]+1;
str[n+n] = 0;
if(hash[str]) return 1;
else cnt = 0;
for(int i = n; i >= 1; i--) str[cnt++] = num[0][i]+1;
for(int i = n; i >= 1; i--) str[cnt++] = num[1][i]+1;
str[n+n] = 0;
if(hash[str]) return 1;
else cnt = 0;
for(int i = 1; i <= n; i++) str[cnt++] = num[1][i]+1;
for(int i = n; i >= 1; i--) str[cnt++] = num[0][i]+1;
str[n+n] = 0;
if(hash[str]) return 1;
else hash[str] = 1;
return 0;
}
int main ()
{
int a, b, ok;
char c;
while(scanf("%d",&n),n)
{
hash.clear();
memset(num,0,sizeof(num));
ok = 0;
for(int i = 0; i < n+n; i++)
{
scanf("%d %d %c",&a,&b,&c);
if(ok) continue;
if(c=='+')
{
num[0][a]++;
num[1][b]++;
}
else
{
num[0][a]--;
num[1][b]--;
}
if(ok = judge()) printf("Player %d wins on move %d\n",((i+1)&1)+1,i+1);
}
if(ok==0) printf("Draw\n");
}
return 0;
}