贪吃蛇的超简化版。。。提交了一次就AC了,虫子的身体上的各个点我用了个FIFO的点队列来处理,打印信息用错误代码的方式来处理,郁闷的是内存怎么占了那么大。。。懒得改了,当然如果用一个50*50的点阵来模拟也应该是没有问题的,或者直接根据各个方向出现的次数和先后关系算出来也是可行的,这样会比较少用空间。
C++ 00:00.00 2984K
//
C++ 00:00.00 2984K
#include < stdio.h >
struct POINT
{
int x,y;
POINT( int a = 0 , int b = 0 )
{
x = a;
y = b;
}
};
POINT worm[ 22 ]; // 虫子点队列,虫子的头就是队列的尾,从队列尾FIFO
int head,tail; // 队列头尾
int n; // 步骤数目
char m[ 101 ]; // 步骤内容,从下标1开始到n
int err; // 0正常,1撞板,2撞自己
/*
void pw(int i)
{
int j;
printf("After MOVE %d ",i);
for(j=head;j!=tail;j++)
{
if(22==j)
j = 1;
printf("(%d,%d) ",worm[j].x,worm[j].y) ;
if(j==21)
j = 0;
}
}
*/
void init()
{
int i,j;
for (i = 11 ,j = 1 ;i <= 30 ;i ++ ,j ++ ) // 初始化
{
worm[j].x = 25 ;
worm[j].y = i;
}
head = 1 ;
tail = 21 ;
err = 0 ;
}
POINT dequeue()
{
POINT x = worm[head];
if ( 21 == head)
head = 1 ;
else
head ++ ;
return x;
}
void enqueue(POINT x)
{
worm[tail] = x;
if ( 21 == tail)
tail = 1 ;
else
tail ++ ;
}
void solve() // E, W, N or S
{
int i,j,k,key,counter;
POINT c = worm[tail - 1 ];
for (i = 1 ;i <= n;i ++ )
{
if ( ' E ' == m[i])
{
c.y ++ ;
if (c.y > 50 )
{
err = 1 ;
break ;
}
}
else if ( ' W ' == m[i])
{
c.y -- ;
if (c.y < 1 )
{
err = 1 ;
break ;
}
}
else if ( ' N ' == m[i])
{
c.x -- ;
if (c.x < 1 )
{
err = 1 ;
break ;
}
}
else // S
{
c.x ++ ;
if (c.x > 50 )
{
err = 1 ;
break ;
}
}
/// //
for (j = head + 1 ;j != tail;j ++ )
{
if ( 22 == j)
j = 1 ;
if (c.x == worm[j].x && c.y == worm[j].y)
{
err = 2 ;
break ;
}
if (j == 21 )
j = 0 ;
}
if (err > 0 )
break ;
else
{
dequeue();
enqueue(c);
// pw(i);
}
}
if ( 0 == err)
printf( " The worm successfully made all %d moves./n " ,n);
else if ( 1 == err)
printf( " The worm ran off the board on move %d./n " ,i);
else if ( 2 == err)
printf( " The worm ran into itself on move %d./n " ,i);
}
int main()
{
// freopen("1056.txt","r",stdin);
while (scanf( " %d " , & n) != EOF && n != 0 )
{
scanf( " %s " ,m + 1 );
init();
solve();
}
// fclose(stdin);
return 0 ;
}
#include < stdio.h >
struct POINT
{
int x,y;
POINT( int a = 0 , int b = 0 )
{
x = a;
y = b;
}
};
POINT worm[ 22 ]; // 虫子点队列,虫子的头就是队列的尾,从队列尾FIFO
int head,tail; // 队列头尾
int n; // 步骤数目
char m[ 101 ]; // 步骤内容,从下标1开始到n
int err; // 0正常,1撞板,2撞自己
/*
void pw(int i)
{
int j;
printf("After MOVE %d ",i);
for(j=head;j!=tail;j++)
{
if(22==j)
j = 1;
printf("(%d,%d) ",worm[j].x,worm[j].y) ;
if(j==21)
j = 0;
}
}
*/
void init()
{
int i,j;
for (i = 11 ,j = 1 ;i <= 30 ;i ++ ,j ++ ) // 初始化
{
worm[j].x = 25 ;
worm[j].y = i;
}
head = 1 ;
tail = 21 ;
err = 0 ;
}
POINT dequeue()
{
POINT x = worm[head];
if ( 21 == head)
head = 1 ;
else
head ++ ;
return x;
}
void enqueue(POINT x)
{
worm[tail] = x;
if ( 21 == tail)
tail = 1 ;
else
tail ++ ;
}
void solve() // E, W, N or S
{
int i,j,k,key,counter;
POINT c = worm[tail - 1 ];
for (i = 1 ;i <= n;i ++ )
{
if ( ' E ' == m[i])
{
c.y ++ ;
if (c.y > 50 )
{
err = 1 ;
break ;
}
}
else if ( ' W ' == m[i])
{
c.y -- ;
if (c.y < 1 )
{
err = 1 ;
break ;
}
}
else if ( ' N ' == m[i])
{
c.x -- ;
if (c.x < 1 )
{
err = 1 ;
break ;
}
}
else // S
{
c.x ++ ;
if (c.x > 50 )
{
err = 1 ;
break ;
}
}
/// //
for (j = head + 1 ;j != tail;j ++ )
{
if ( 22 == j)
j = 1 ;
if (c.x == worm[j].x && c.y == worm[j].y)
{
err = 2 ;
break ;
}
if (j == 21 )
j = 0 ;
}
if (err > 0 )
break ;
else
{
dequeue();
enqueue(c);
// pw(i);
}
}
if ( 0 == err)
printf( " The worm successfully made all %d moves./n " ,n);
else if ( 1 == err)
printf( " The worm ran off the board on move %d./n " ,i);
else if ( 2 == err)
printf( " The worm ran into itself on move %d./n " ,i);
}
int main()
{
// freopen("1056.txt","r",stdin);
while (scanf( " %d " , & n) != EOF && n != 0 )
{
scanf( " %s " ,m + 1 );
init();
solve();
}
// fclose(stdin);
return 0 ;
}