很简单的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"); } }