UVA 141 The Spot Game

UVA_141

由于一开始审题的问题,导致WA个不停,以后一定要加强审题!

首先,棋盘是N*N的,不是4*4的。其次,在判断这个状态是否出现过时,要分别判断3种情况,但存储状态的时候只存储1种情况。

#include<stdio.h>
#include
<string.h>
int head[100007],next[1100];
int st[440][250],dis[110],temp[50][50],N;
char flag[5];
int hash(int *A)
{
int i,v=0;
for(i=0;i<N*N;i++)
v
=((v<<1)+A[i])%100007;
return v;
}
void insert(int s)
{
int i,h;
h
=hash(st[s]);
next[s]
=head[h];
head[h]
=s;
}
int search(int s)
{
int i,h;
h
=hash(st[s]);
for(i=head[h];i!=-1;i=next[i])
if(memcmp(st[i],st[s],sizeof(st[i]))==0)
break;
if(i==-1)
return 0;
else
return 1;
}
int main()
{
int i,j,k,t,a,b,front,rear,ans;
while(1)
{
scanf(
"%d",&N);
if(N==0)
break;
ans
=0;
memset(head,
-1,sizeof(head));
memset(st[
0],0,sizeof(st[0]));
front
=rear=0;
dis[rear]
=0;
insert(rear);
rear
++;
for(t=0;t<2*N;t++)
{
scanf(
"%d%d%s",&a,&b,flag);
a
--;
b
--;
if(!ans)
{
memcpy(st[rear],st[front],
sizeof(st[rear]));
if(flag[0]=='+')
st[rear][a
*N+b]=1;
else
st[rear][a
*N+b]=0;
dis[rear]
=dis[front]+1;
front
=rear;
if(search(front))
{
ans
=(t+1)%2+1;
continue;
}
else
rear
++;
for(k=0;k<N*N;k++)
{
i
=k/N;
j
=k%N;
temp[i][j]
=st[front][k];
}
k
=0;
for(j=0;j<N;j++)
for(i=N-1;i>=0;i--)
st[rear][k
++]=temp[i][j];
if(search(rear))
{
ans
=(t+1)%2+1;
continue;
}
k
=0;
for(j=N-1;j>=0;j--)
for(i=0;i<N;i++)
st[rear][k
++]=temp[i][j];
if(search(rear))
{
ans
=(t+1)%2+1;
continue;
}
k
=0;
for(i=N-1;i>=0;i--)
for(j=N-1;j>=0;j--)
st[rear][k
++]=temp[i][j];
if(search(rear))
{
ans
=(t+1)%2+1;
continue;
}
insert(front);
}
}
if(ans==0)
printf(
"Draw\n");
else
printf(
"Player %d wins on move %d\n",ans,dis[front]);
}
return 0;
}

  

原文地址:https://www.cnblogs.com/staginner/p/2179565.html