栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1056 The Worm Turns

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号