Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483289
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: C/C++

2013-03-02 18:19:30


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>

  3. #define MAX 101
  4. #define CRASH_WALL -1
  5. #define OK 0

  6. int a[101][101];

  7. typedef struct
  8. {
  9.     int x;
  10.     int y;
  11.     int dirc;
  12. }pos;

  13. /***********************************************
  14.   |func: 机器人按指令行动函数
  15.   |args: 第i个机器人状态,执行指令ins,走s步
  16.   |retn: 撞墙->CRASH_WALL,撞机器人->编号,其它->OK
  17. ************************************************/
  18. int Act(pos *r, int i, char ins, int s)
  19. {
  20.     int xoff, yoff, t;
  21.     
  22.     if ('L' == ins)
  23.     {
  24.         s %= 4;
  25.         r[i].dirc = (r[i].dirc + s + 4) % 4;
  26.     }
  27.     
  28.     else if ('R' == ins)
  29.     {
  30.         s %= 4;
  31.         r[i].dirc = (r[i].dirc - s + 4) % 4;
  32.     }
  33.     
  34.     else
  35.     {
  36.         switch(r[i].dirc)
  37.         {
  38.             case 0 :
  39.             {
  40.                 xoff = 0;
  41.                 yoff = 1;
  42.                 break;
  43.             }
  44.             case 1 :
  45.             {
  46.                 xoff = 1;
  47.                 yoff = 0;
  48.                 break;
  49.             }
  50.             case 2 :
  51.             {
  52.                 xoff = 0;
  53.                 yoff = -1;
  54.                 break;
  55.             }
  56.             case 3 :
  57.             {
  58.                 xoff = -1;
  59.                 yoff = 0;
  60.             }
  61.         }
  62.         while(s--) //模拟前进
  63.         {
  64.             t = a[r[i].x + xoff][r[i].y + yoff];
  65.             if (-1 == t)
  66.             {
  67.                 return CRASH_WALL;
  68.             }
  69.             else if (t != 0)
  70.             {
  71.                 return t; //撞到其它robot
  72.             }
  73.             a[r[i].x + xoff][r[i].y + yoff] = i; //更新当前robot序号到移动后的位置
  74.             a[r[i].x][r[i].y] = 0; //清除上一步的足迹
  75.             r[i].x += xoff; //更新新的坐标
  76.             r[i].y += yoff;
  77.         }
  78.     }
  79.     
  80.     return OK;
  81. }


  82. int main()
  83. {
  84.     pos robot[101]
  85.     char instruct, direction;
  86.     int k, ew, ns, num, m, i, j, step, failed;
  87.     
  88.     scanf("%d", &k);
  89.     while(k--)
  90.     {
  91.         memset(a, -1, sizeof(a)); //初始化-1为Wall
  92.         failed = 0; //crash标记
  93.         scanf("%d%d%d%d", &ew, &ns, &num, &m);
  94.         for(i=1; i<=ns; i++)
  95.         {
  96.             for (j=1; j<=ew; j++)
  97.             {
  98.                 a[i][j] = 0; //无阻挡物,标记为0
  99.             }
  100.         }
  101.         
  102.         i = 1;
  103.         while(i <= num)
  104.         {
  105.             scanf("%d%d %c", &robot[i].y, &robot[i].x, &direction); //数组的x为ns方向,数组y为ew方向
  106.             a[robot[i].x][robot[i].y] = i; //标记robot位置
  107.             switch(direction)
  108.             { //数组水平方向和题目一致, 垂直方向和题目相反.
  109.                 case 'E' : robot[i].dirc = 0; break;
  110.                 case 'N' : robot[i].dirc = 1; break;
  111.                 case 'W' : robot[i].dirc = 2; break;
  112.                 case 'S' : robot[i].dirc = 3; break;
  113.             }
  114.             i++;
  115.         }
  116.         
  117.         while(m)
  118.         {
  119.             while((m != 0) && failed) //这个很重要,在crash后要读完后面的指令才break
  120.             {
  121.                 scanf("%d %c%d", &i, &instruct, &step);
  122.                 m-- ;
  123.             }
  124.             if (0 == m)
  125.             {
  126.                 break;
  127.             }
  128.             scanf("%d %c%d", &i, &instruct, &step);
  129.             j = Act(robot, i, instruct, step);
  130.             if (CRASH_WALL == j)
  131.             {
  132.                 printf("Robot %d crashes into the walln", i);
  133.                 failed = 1;
  134.             }
  135.             else if (j != OK)
  136.             {
  137.                 printf("Robot %d crashes into robot %dn", i, j);
  138.                 failed = 1;
  139.             }
  140.             m--;
  141.         }
  142.         
  143.         if (!failed)
  144.         {
  145.             printf("OKn");
  146.         }
  147.     }
  148.     
  149.     return 0;
  150. }
2011-04-07 13:19 发表于百度空间,今搬至CU。
阅读(1772) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~