走格子收获

 eg:poj2632

   code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a,b;
int queue[101][101];
int xx[4]= {-1,0,1,0};   //!!!!!!!注意这里方向的顺序必须和0、1、2、3保持一致!!!!WA了好多遍都不知道哪错了
int yy[4]= {0,1,0,-1};
struct spot
{
    int x,y,d;
} robot[101];

bool forward(int s,int t)
{
    int x=robot[s].x;
    int y=robot[s].y;
    int d=robot[s].d;
    queue[x][y]=0;
    for(int i=0; i<t; i++)
    {
        x+=xx[d];
        y+=yy[d];
        if(queue[x][y])
        {
            cout<<"Robot "<<s<<" crashes into robot "<<queue[x][y]<<endl;
            return true;
        }
        if(x<1||x>a||y>b||y<1)
        {
            cout<<"Robot "<<s<<" crashes into the wall"<<endl;
            return true;
        }

    }
    robot[s].x=x;
    robot[s].y=y;
    queue[x][y]=s;
    return false;
}

bool action(int s,char dir,int t)
{
    switch (dir)
    {
    case 'F':
        return forward(s,t);
    case 'L':
        robot[s].d=(robot[s].d-t%4+4)%4;    //是编号始终为0、1、2、3 对于绕桌子一圈编号问题一样
        break;
    case 'R':
        robot[s].d=(robot[s].d+t%4)%4;
        break;
    }
    return false;
}

int main()
{
    int k;
    int n,m,t,s;
    int xi,yi;
    char dir;
    cin>>k;
    bool f=false;
    while(k--)
    {
        memset(queue,0,sizeof(queue));
        memset(robot,0,sizeof(struct spot)*101);
        cin>>a>>b>>n>>m;
        for(int i=1; i<=n; i++)
        {
            cin>>xi>>yi>>dir;
            queue[xi][yi]=i;             //这里注意,通过编号找位置,通过位置找编号
            robot[i].x=xi;
            robot[i].y=yi;
            switch(dir)          //可以用0、1、2、3来表示方向
            {                                                    N : 1
            case 'N':                                W:0    +    E :2 
                robot[i].d=1;                            S : 3
                break;
            case 'S':
                robot[i].d=3;
                break;
            case 'W':
                robot[i].d=0;
                break;
            case 'E':
                robot[i].d=2;
                break;
            }

        }
        f=false;
        for(int i=0; i<m; i++)
        {
            cin>>s>>dir>>t;
            if(!f) f=action(s,dir,t);
        }
        if(!f) cout<<"OK"<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值