poj 2632 (模拟)

4 篇文章 0 订阅


思路:

使用x ,y,dir 数组 分别表示 第i 个robot 的 X,Y,坐标和 方向。

一开始,我将x ,y 轴的方向弄错了,wa 了。。一定要注意方向。

我将N, E,S, W 方向 0,1,2,3;

判断crash 的时候,先判断撞墙 再  循环判断时候碰到 其他robot

并且使用一个flag  判断是否撞到。


思路清晰的话,代码不难写出来。。

不过模拟题目太恶心了,写了一个小时。


还要注意一点

对 方向 进行运算的时候 要注意正负, 第一次左转的时候使用  dir [i] =( dir [i] -repeat +4) % 4  。答案错误。因为我没考虑到repeat(重复次数)的大小。第二次我就使用

  dir [i] =( dir [i] -repeat +400) % 4  




memory  time
220k    16ms
#include <iostream>
#include <queue>
#include <stdio.h>
#include <cmath>
#include <memory.h>
using namespace std;
int n,m,a,b,i,j,k,robot,repeat;
int flag;
char action;
int x[110],y[110];
int  dir[110];
// 0,1,2,3 分别表示N,E,S,W;
//int move[4][2]={ {0,-1},{1,0},{0,1},{-1,0} };
int  operate(int robot,char action,int repeat)
{
    if(action=='L')
    {
        dir[robot]=(dir[robot]-repeat+400)%4;
    }
    else if(action=='R')
    {
        dir[robot]=(dir[robot]+repeat)%4;
    }
    else if(action=='F')
    {
        for(int i=0;i<repeat;i++)
        {
            if(dir[robot]==0)
            {
                y[robot]++;
                if(y[robot]==b+1)
                {
                    cout<<"Robot "<<robot<<" crashes into the wall"<<endl;
                    return 0;
                }
                for(j=1;j<=n;j++)
                {
                    if(j!=robot)
                    {
                        if(y[robot]==y[j]&&x[robot]==x[j])
                         {
                             cout<<"Robot "<<robot<<" crashes into robot "<<j<<endl;
                             return 0;
                         }
                    }
                }
            }
            else if(dir[robot]==1)
            {
                x[robot]++;
                if(x[robot]==a+1)
                {
                    cout<<"Robot "<<robot<<" crashes into the wall"<<endl;
                    return 0;
                }
                for(j=1;j<=n;j++)
                {
                    if(j!=robot)
                    {
                        if(x[robot]==x[j]&&y[robot]==y[j])
                          {
                             cout<<"Robot "<<robot<<" crashes into robot "<<j<<endl;
                             return 0;
                          }
                    }
                }
            }
            else if(dir[robot]==2)
            {
                y[robot]--;
                if(y[robot]==0)
                {
                    cout<<"Robot "<<robot<<" crashes into the wall"<<endl;
                    return 0;
                }
                for(j=1;j<=n;j++)
                {
                    if(j!=robot)
                    {
                        if(y[robot]==y[j]&&x[robot]==x[j])
                        {
                            cout<<"Robot "<<robot<<" crashes into robot "<<j<<endl;
                            return 0;
                        }
                    }
                }
            }
            else if(dir[robot]==3)
            {
                x[robot]--;
                if(x[robot]==0)
                {
                    cout<<"Robot "<<robot<<" crashes into the wall"<<endl;
                    return 0;
                }
                for(j=1;j<=n;j++)
                {
                    if(j!=robot)
                    {
                        if(x[robot]==x[j] &&y[robot]==y[j])
                        {
                            cout<<"Robot "<<robot<<" crashes into robot "<<j<<endl;
                            return 0;
                        }
                    }
                }
            }
        }
    }
    return 1;
}
int main()
{
    cin>>k;
    char c;

    while(k--)
    {
        cin>>a>>b;
        cin>>n>>m;
        flag=1;
        for(i=1;i<=n;i++)
        {
            cin>>x[i]>>y[i]>>c;
            if(c=='E')
                dir[i]=1;
            else if(c=='N')
                dir[i]=0;
            else if(c=='S')
                dir[i]=2;
            else
                dir[i]=3;
        }
        for(i=0;i<m;i++)
        {
            cin>>robot>>action>>repeat;
            if(flag)
                flag=operate(robot,action,repeat);
        }
        if(flag)
            cout<<"OK"<<endl;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值