# ZOJ1056 The Worm Turns

#include <iostream>
#include <string>
using namespace std;
//贪吃蛇节点
struct WNode
{
int x;//行号
int y;//列号
};
int main()
{
string moves;//移动序列
WNode worm[20];//贪吃蛇
int n,i,j;
while(cin>>n&&n!=0)
{
//从头部到尾部初始化贪吃蛇
for(i=0;i<20;++i)
{
worm[i].x = 25;//起始行在行
worm[i].y = 30-i;//起始所在列
}
cin>>moves;//输入移动序列
for (i=0;i<n;++i)
{
//贪吃蛇中其他节点移动到前一个节点位置上
for(j=19;j>0;--j)
{
worm[j].x=worm[j-1].x;
worm[j].y=worm[j-1].y;
}
//移动头部
if (moves[i]=='N')
{//向北
worm[0].x -= 1;
}
else if (moves[i]=='S')
{//向南
worm[0].x += 1;
}
else if (moves[i]=='W')
{//向西
worm[0].y -= 1;
}
else if (moves[i]=='E')
{//向东
worm[0].y += 1;
}
//判断是否出界
if(worm[0].x>50||worm[0].y>50||worm[0].x<1||worm[0].y<1)
{
cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
break;
}
//判断是否撞到自己身体了
for(j=1;j<20;++j)
{
//头部节点撞到其他节点
if(worm[0].x==worm[j].x&&worm[0].y==worm[j].y)
{
cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
break;
}
}
if(j!=20) break;//发生了碰撞，不能继续运动了
}
if(i==n)
cout<<"The worm successfully made all "<<n<<" moves."<<endl;
}
return 0;
}

|
NoSQL 关系型数据库 MySQL
CTFShow-SSRF(writeup)
CTFShow-SSRF(writeup)
101 0
|

UVA10474 大理石在哪儿 Where is the Marble?
UVA10474 大理石在哪儿 Where is the Marble?
47 0
|

HDU-1049 Climbing Worm
HDU-1049 Climbing Worm
65 0
|

|

|

|