一个像贪吃蛇的问题?高手拿分
http://acm.zju.edu.cn/show_problem.php?pid=1056
我的解法是这样的:
#include <iostream.h>
int offx[4]={0,1,0,-1};
int offy[4]={1,0,-1,0};
int all[52][52];
int head_x,head_y,tail_x,tail_y;
char step[100];
void move(int mhead_x,int mhead_y,int mtail_x,int mtail_y,int last,int forward)
{
all[mtail_x+offx[last]][mtail_y+offy[last]]-=1;
all[mhead_x+offx[forward]][mhead_y+offy[forward]]+=1;
tail_x=mtail_x+offx[last];
tail_y=mtail_y+offy[last];
head_x=mhead_x+offx[forward];
head_y=mhead_y+offy[forward];
}
int change(char ch)
{
switch (ch)
{
case 'E':return 0;
case 'S':return 1;
case 'W':return 2;
case 'N':return 3;
default:return 4;
}
}
int main()
{
int i,j,temp;
int n;
char ch,lch='E';
int inc=19;
int dec=0;
cin>>n;
while (n!=0)
{
for (i=0;i<52;i++)
{
for (j=0;j<52;j++)
{
all[i][j]=0;
}
}
for (i=11;i<31;i++)
{
all[25][i]=1;
}
for (i=0;i<100;i++)
{
step[i]='E';
}
head_x=25;
head_y=30;
tail_x=25;
tail_y=11;
for (i=0;i<n;i++)
{
cin>> ch;
step[inc]=ch;
lch=step[dec];
move(head_x,head_y,tail_x,tail_y,change(lch),change(ch));
if (all[head_x][head_y]>1)
{
cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
temp=i;
i=n+1;
}
for (int m=0;m<52;m++)
{
if ((all[0][m]>0) ¦ ¦(all[51][m]>0) ¦ ¦(all[m][0]>0) ¦ ¦(all[m][51]>0))
{
cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
temp=i;
i=n+1;
m=53;
}
}
inc++;
dec++;
}
if (i==n)
{
temp=i;
cout<<"The worm successfully made all "<<n<<" moves."<<endl;
}
if (temp!=i)
{
for (int k=temp+1;k<n;k++)
cin>>ch;
}
cin>>n;
}
return 0;
}
但是Wrong Answer.ft死了,给看一下吧,测试了很多,都可以啊