您的位置:首页 > 其它

zoj1056 The Worm Turns

2006-07-10 20:04 357 查看
//zoj1056 The Worm Turns

//Accepted 1056 C++ 00:00.00 388K

#include <stdio.h>

struct worm{ int x,y;}w[20];

int n;

void init()

{

for (int i=0; i<20; ++i){

w[i].x = 25;

w[i].y = 30-i;

}

}

void move(int x,int y)

{

for (int i=19; i>0; --i){

w[i].x = w[i-1].x;

w[i].y = w[i-1].y;

}

w[0].x = x;

w[0].y = y;

}

int judge()

{

if (w[0].x<1 || w[0].x>50 || w[0].y<1 || w[0].y>50) return 2;

for (int i=1; i<20; ++i)

if (w[i].x==w[0].x && w[i].y==w[0].y)

return 1;

return 0;

}

void solve()

{

char s[101];

scanf ("%s",s);

init();

for (int i=0; i<n; ++i){

if (s[i]=='E') move(w[0].x,w[0].y+1);

if (s[i]=='W') move(w[0].x,w[0].y-1);

if (s[i]=='N') move(w[0].x-1,w[0].y);

if (s[i]=='S') move(w[0].x+1,w[0].y);

int j = judge();

if (j==1) {

printf ("The worm ran into itself on move %d./n",i+1);

return ;

}

if (j==2) {

printf ("The worm ran off the board on move %d./n",i+1);

return ;

}

}

printf ("The worm successfully made all %d moves./n",n);

}

int main()

{

#ifdef ONLINE_JUDGE

#else

freopen("1056.txt","r",stdin);

#endif

while (scanf("%d",&n)!=EOF && n)

solve();

#ifdef ONLINE_JUDGE

#else

fclose(stdin);

#endif

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: