HDU2300 Crashing Robots

题目类型 : 模拟

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct robots
{
    int ix ;
    int iy ;
    int move ;
    void remove(void);
    bool event(int num , char e);
    bool judge_wall(void);
    bool judge_crash(int num);
} robot[120];

struct affairs
{
    int robot;   //事件发生的机器人编号
    char run ;   //事件类型
    int time ;   //事件执行次数
} affair[120];

struct maps
{
    int mx ;       //地图最大x坐标
    int my ;       //地图最大y坐标
    int robot_num;   //机器人数
    int affair_num;  //事件数
    int node[120][120];
    void init(void);  //地图初始化
} map;

int main()
{
    freopen("in.txt","r",stdin);
    int ncase ;
    bool safe ;
    cin >> ncase ;
    while(ncase --)
    {
        map.init();
        safe = true ;
        for(int k = 1, rr ; k <=map.affair_num&&safe; k++)
            for(int tt = 1 ; tt <=affair[k].time; tt ++)
            {
                rr = affair[k].robot ;
                map.node[robot[rr].ix][robot[rr].ix] = 0 ;
                if(!robot[rr].event(rr,affair[k].run))
                {
                    safe = false ;
                    break ;
                }
                map.node[robot[rr].ix][robot[rr].iy] = rr ;
            }
        if(safe)
            cout<< "OK" <<endl;
    }
    return 0;
}

void maps::init(void)
{
    char  mm ;
    memset(node,0,sizeof(node));
    cin >> mx >> my ;
    cin >> robot_num >> affair_num ;
    for(int i = 1 ; i <= robot_num ; i ++)
    {
        cin >> robot[i].ix >> robot[i].iy >> mm ;
        node[robot[i].ix][robot[i].iy] = i ; //将字符型的方向改为数值的方向
        if(mm == 'N')
            robot[i].move = 1 ;
        else if(mm == 'E')
            robot[i].move = 2 ;
        else if(mm == 'S')
            robot[i].move = 3 ;
        else if(mm == 'W')
            robot[i].move = 4 ;
    }
    for(int i = 1 ; i <= affair_num ; i ++)
        cin >> affair[i].robot >> affair[i].run >> affair[i].time ;
    return ;
}

void robots::remove(void)
{
    switch(move)
    {
    case 1 :
        iy ++ ;
        break ;
    case 2 :
        ix ++ ;
        break ;
    case 3 :
        iy -- ;
        break ;
    case 4 :
        ix -- ;
        break ;
    }
    return ;
}

bool robots::judge_crash(int num)
{
    if(map.node[ix][iy] == 0 )
        return true ;
    cout<<"Robot "<<num<<" crashes into robot "<<map.node[ix][iy]<<endl;
    return false ;
}

bool robots::event(int num,char e)
{
    switch(e)
    {
    case 'L' :
        move -- ;
        if(move < 1)
            move += 4 ;
        break ;
    case 'R' :
        move ++ ;
        if(move > 4)
            move -= 4 ;
        break ;
    case 'F' :
        remove();
        if(!judge_wall())
        {
            cout<<"Robot "<<num<<" crashes into the wall"<<endl;
            return false ;
        }
        if(!judge_crash(num))
        {
            return false;
        }
        break;
    }
    return true;
}

bool robots::judge_wall(void)
{
    if(ix > map.mx || ix < 1 ||iy > map.my || iy < 1)
        return false ;
    else
        return true ;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值