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;
}
//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;
}
相关文章推荐
- ZOJ-1056-The Worm Turns
- ZOJ1056 The Worm Turns
- ZOJ Problem Set – 1056 The Worm Turns
- ZOJ1056 The Worm Turns
- ZOJ Problem Set - 1056 The Worm Turns
- zoj 1056 The Worm Turns
- ZOJ&nbsp;1056&nbsp;The&nbsp;Worm&nbsp;Turns
- zoj 1056 The Worm Turns
- ZOJ 1056 The Worm Turns 杭电计算机复试2012第二题
- ZOJ 1056 The Worm Turns
- 9.3.4 The Worm Turns
- The Worm Turns
- The Worm Turns
- HDU 2782 The Worm Turns 【dfs深搜】
- HDU2782 The Worm Turns(DFS水)
- The Worm Turns
- 【HDOJ】2782 The Worm Turns
- TJU ACM-ICPC Online Judge—1191 The Worm Turns
- THE WORM TURNS
- 【DFS】 hdu2782 The Worm Turns