简单的贪吃蛇

简单的贪吃蛇:

Sample Input

18
NWWWWWWWWWWSESSSWS
20
SSSWWNENNNNNWWWWSSSS
30
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
13
SWWWWWWWWWNEE
0

Sample Output

The worm successfully made all 18 moves.
The worm ran into itself on move 9.
The worm ran off the board on move 21.
The worm successfully made all 13 moves.

 

  1. //Worm.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. using namespace std;
  6. struct Node
  7. {
  8.  int x;
  9.  int y;
  10. };
  11. int _tmain(int argc, _TCHAR* argv[])
  12. {
  13.  Node ND[20];//length=10
  14.  bool Cp[51][51];
  15.     int H_x,H_y;
  16.  int n,i,j;
  17.  char Direct[100];
  18.  bool Cant;
  19.  while(cin>>n && n!=0)
  20.  {
  21.   for(i=0;i<n;i++)
  22.    cin>>Direct[i];
  23.   for(i=1;i<51;i++)
  24.    for(j=1;j<51;j++)
  25.     Cp[i][j]=false;
  26.   //Init
  27.   for(i=0;i<20;i++)
  28.   {
  29.    ND[i].x=25;
  30.    ND[i].y=11+i;
  31.   }
  32.         for(i=11;i<=30;i++)
  33.    Cp[25][i]=true;
  34.         Cant=false;
  35.   //move
  36.   for(i=0;i<n;i++)
  37.   {
  38.    if(i==0 && Direct[i]=='W')break;
  39.    switch(Direct[i])
  40.    {
  41.    case 'E':
  42.     {
  43.      if(ND[19].y==50)
  44.      {
  45.       cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
  46.       Cant=true;
  47.      }
  48.      else
  49.      {
  50.       for(j=1;j<19;j++)
  51.       {
  52.        //判断中间段是否已经在那里
  53.        if(ND[j].x==ND[19].x && ND[j].y==ND[19].y+1)
  54.        {
  55.         cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
  56.         Cant=true;
  57.         break;
  58.        }
  59.       }
  60.       if(Cant==false)
  61.       {
  62.        //改变状态
  63.        
  64.        H_x=ND[19].x;
  65.        H_y=ND[19].y;
  66.        for(j=0;j<19;j++)
  67.        {
  68.         ND[j].x=ND[j+1].x;
  69.         ND[j].y=ND[j+1].y;
  70.        }
  71.        ND[19].x=H_x;
  72.        ND[19].y=H_y+1;
  73.        
  74.       }
  75.      }
  76.      break;
  77.     }
  78.    case 'W':
  79.     {
  80.      if(ND[19].y==1)
  81.      {
  82.       cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
  83.       Cant=true;
  84.      }
  85.      else
  86.      {
  87.       for(j=1;j<19;j++)
  88.       {
  89.        //判断中间段是否已经在那里
  90.        if(ND[j].x==ND[19].x && ND[j].y==ND[19].y-1)
  91.        {
  92.         cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
  93.         Cant=true;
  94.         break;
  95.        }
  96.       }
  97.       if(Cant==false)
  98.       {
  99.        //改变状态
  100.        
  101.        H_x=ND[19].x;
  102.        H_y=ND[19].y;
  103.        for(j=0;j<19;j++)
  104.        {
  105.         ND[j].x=ND[j+1].x;
  106.         ND[j].y=ND[j+1].y;
  107.        }
  108.        ND[19].x=H_x;
  109.        ND[19].y=H_y-1;
  110.        
  111.       }
  112.      }
  113.      break;
  114.     }
  115.    case 'N':
  116.     {
  117.      if(ND[19].x==1)
  118.      {
  119.       cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
  120.       Cant=true;
  121.      }
  122.      else
  123.      {
  124.       for(j=1;j<19;j++)
  125.       {
  126.        //判断中间段是否已经在那里
  127.        if(ND[j].y==ND[19].y && ND[j].x==ND[19].x-1)
  128.        {
  129.         cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
  130.         Cant=true;
  131.         break;
  132.        }
  133.       }
  134.       if(Cant==false)
  135.       {
  136.        //改变状态
  137.        
  138.        H_x=ND[19].x;
  139.        H_y=ND[19].y;
  140.        for(j=0;j<19;j++)
  141.        {
  142.         ND[j].x=ND[j+1].x;
  143.         ND[j].y=ND[j+1].y;
  144.        }
  145.        ND[19].x=H_x-1;
  146.        ND[19].y=H_y;
  147.        
  148.       }
  149.      }
  150.      break;
  151.     }
  152.    case 'S':
  153.     {
  154.      if(ND[19].x==50)
  155.      {
  156.       cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
  157.       Cant=true;
  158.      }
  159.      else
  160.      {
  161.       for(j=1;j<19;j++)
  162.       {
  163.        //判断中间段是否已经在那里
  164.        if(ND[j].y==ND[19].y && ND[j].x==ND[19].x+1)
  165.        {
  166.         cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
  167.         Cant=true;
  168.         break;
  169.        }
  170.       }
  171.       if(Cant==false)
  172.       {
  173.        //改变状态
  174.        
  175.        H_x=ND[19].x;
  176.        H_y=ND[19].y;
  177.        for(j=0;j<19;j++)
  178.        {
  179.         ND[j].x=ND[j+1].x;
  180.         ND[j].y=ND[j+1].y;
  181.        }
  182.        ND[19].x=H_x+1;
  183.        ND[19].y=H_y;
  184.        
  185.       }
  186.      }
  187.      break;
  188.     }
  189.    }
  190.    if(Cant==true)break;
  191.   }
  192.   if(Cant==false)cout<<"The worm successfully made all "<<n<<" moves."<<endl;
  193.  }
  194.  return 0;
  195. }
  196.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值