POJ2632 Crashing Robots

本文介绍了一个基于C++实现的机器人路径模拟算法。该算法通过控制多个机器人在一个指定大小的地图上移动,同时避免机器人之间的碰撞及撞墙。文章详细展示了如何定义机器人结构、地图状态以及如何解析移动指令来更新机器人的位置。

 

模拟题。


代码如下,应该还算还理解吧。


#include<cstdio>
#include<cstring>
#include<iostream>
#define N 101
using namespace std;
int row,col,n,m;
int di[4][2] = {1,0,0,1,-1,0,0,-1};
bool map[N][N];
typedef struct{
int x,y;
int dir;
}Node;
Node robot[N];
typedef struct{
int i,rep;
char c;
}Dot;
Dot cor[N];
int ftest(char c)
{
   switch(c)
   {
   case 'N':return 1;
   case 'E':return 2;
   case 'S':return 3;
   case 'W':return 4;
   }
}
int judge(int x,int y)
{
     int i;
    for( i=1;i<=n;++i)
       if(robot[i].x==x&&robot[i].y==y)
       break;
    return i;
}
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
     memset(map,false,sizeof(map));
     cin>>col>>row;
     cin>>n>>m;
     for(int i=1;i<=n;++i)
      {
        char c;
        scanf("%d %d %c",&robot[i].y,&robot[i].x,&c);
        map[robot[i].x][robot[i].y] = true;
        robot[i].dir = ftest(c);
      }
     for(int i=1;i<=m;++i)
     {
       scanf("%d %c %d",&cor[i].i,&cor[i].c,&cor[i].rep);
     }
      int flag = 0;
     for(int i=1;i<=m&&flag==0;++i)
     {
       if(cor[i].c=='L')
       {
          int g = cor[i].rep;
          int mm = cor[i].i;
        for(int e=1;e<=g;++e)
        {
         robot[mm].dir--;
         if(robot[mm].dir<1)
            robot[mm].dir = 4;
        }

       }
       else if(cor[i].c=='R')
       {
         int g = cor[i].rep;
          int mm = cor[i].i;
        for(int e=1;e<=g;++e)
        {
            robot[mm].dir++;
            if(robot[mm].dir>4)
             robot[mm].dir = 1;
        }
       }
       else
       {
        int g = cor[i].rep;
        int mm = cor[i].i;
         for(int e=1;e<=g&&flag==0;++e)
         {
          int x1 = robot[mm].x + di[robot[mm].dir-1][0];
          int y1 = robot[mm].y + di[robot[mm].dir-1][1];
          if(map[x1][y1])
          {
           flag = 1;
           cout<<"Robot "<<mm<<" crashes into robot "<<judge(x1,y1)<<endl;
           break;
          }
          else if(1<=x1&&x1<=row&&y1>=1&&y1<=col)
          {
             map[robot[mm].x][robot[mm].y] = false;
             map[x1][y1] = true;
             robot[mm].x = x1; robot[mm].y = y1;
          }
          else
          {
              flag = 1;
              cout<<"Robot "<<mm<<" crashes into the wall"<<endl;
              break;
          }
         }
       }
     }
     if(flag==0)
      cout<<"OK"<<endl;
    }

    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值