poj2632(模拟题)

摘要:本题难在麻烦二字,主要是需要注意第一次碰撞之后立刻要停止模拟.利用vector容器简化代码.

#include "stdafx.h"
#include "iostream"
#include "vector"
int test = 0;
using namespace std;
const char S[4] = {'N','W','S','E'};
int board[101][101] = {0};
struct Rob
{
    int index;
    int x;
    int y;
    char c;
};
char change(char c,char order,int repeat)
{
    int x = 0;
    switch(c)
    {
    case 'N':  x = 0;  break;
    case 'W':  x = 1;  break;
    case 'S':  x = 2;  break;
    case 'E':  x = 3;  break;
    }
    int ch;
    switch(order)
    {
    case 'L':  ch = 1;  break;
    case 'R':  ch = 3;  break;
    case 'F':  ch = 0;  break;
    }
    x = (x+ch*repeat)%4;
    return S[x];
};
int main()
{
    int k;
    cin>>k;
    while(k--)
    {
        memset(board,0,sizeof(board));
        int A,B,N,M;
        cin>>A>>B>>N>>M;
        test = 0;
        vector<Rob> robot;
        Rob R;
        int number = N;
        while(N--)
        {
            cin >> R.x>>R.y>>R.c;
            robot.push_back(R);
            board[R.x][R.y] = number - N;//标记该位置的机器人
        }
        int Index;
        Rob ans;
        while(M--)
        {
            int index,repeat;
           char order;
            cin >>index>>order>>repeat;
            if(test!=0)
            continue;
            Index = index;
            Rob &temprob = robot[index-1];
            board[temprob.x][temprob.y] = 0;//该位置清零
            if(order == 'F')
        {
            while(repeat--)//重复执行命令直到执行完重复步骤或者出现crashes
            {
                if(test!=0)
                continue;
                switch(temprob.c)
                {
                case 'N':  temprob.y += 1;  break;
                case 'W': temprob.x -= 1;  break;
                case 'S': temprob.y -= 1;  break;
                case 'E':  temprob.x += 1;  break;
                }
                if(temprob.x==0||temprob.x==A+1||temprob.y==0||temprob.y==B+1)//撞墙
                {
                  ans = temprob;
                  test = 1;
                  break;
                }
                else if(board[temprob.x][temprob.y]!=0)//撞到一个机器人
                {
                    ans = temprob;
                    test = 2;
                    break;
                }
            }
        }
        else //转换方向
            temprob.c = change(temprob.c,order,repeat);
            if(test==0)
             board[temprob.x][temprob.y] = index;//该位置此时被机器人占了
        }

            if(test==0)
            cout<<"OK"<<endl;
        else if (test==1)
                cout<<"Robot "<<Index<<" crashes into the wall"<<endl;
        else
                cout<<"Robot "<<Index<<" crashes into robot "<<board[ans.x][ans.y]<<endl; 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值