zoj 1056 The Worm Turns

zoj 1056 The Worm Turns,第1张

zoj 1056 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;}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4908276.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存