SP7034 CROBOTS - Crashing Robots

· · 题解

分析:就是一个比较复杂的模拟,只要注意细节,花时间多一点是可以过的。

根据题目所述移动步骤逐步进行,开一个数组表示这个位置上有没有机器人,再开一个结构体记录下每个机器人当前的位置和方向。

然后这题比较坑的地方是地图的横纵和一般的是反的,然后因为这个样例调试了1h.

并且一步步操作比较安全,用增量数组一个个加,不会超时.

#include<iostream> 
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#define I inline
#define RI register int
#define maxn 1500
using namespace std;
I int read()
{
    RI res=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch)){res=(res<<1)+(res<<3)+(ch&15);ch=getchar();}
    return res*f;
}

int T,n,m,A,B;
int vis[maxn][maxn];
struct Robot{
    int x,y;
    int fx;
}a[maxn];
const int dx[4]={1,0,-1,0};//东南西北四个方向的增量数组
const int dy[4]={0,-1,0,1};

I bool check(int x,int y){//判断是不是越过边界
    if(x<1||x>A||y<1||y>B)return 0;
    return 1;   
}
I int L(int x){//左转
    if(x-1<0)return 3;
    return x-1;
}
I int R(int x){//右转
    if(x+1>3)return 0;
    return x+1;
}

int main()
{
    T=read();
    while(T--)
    {
        A=read();B=read();
        n=read();m=read();
        bool flag=0;
        memset(vis,0,sizeof(vis));
        for(RI i=1;i<=n;i++)
        {
            a[i].x=read();
            a[i].y=read();
            char c;cin>>c;
            if(c=='E')a[i].fx=0;
            else if(c=='S')a[i].fx=1;
            else if(c=='W')a[i].fx=2;
            else a[i].fx=3;
            vis[a[i].x][a[i].y]=i;
        }
        for(RI i=1;i<=m;i++)
        {
            int x;int rep;char c;
            cin>>x>>c>>rep;
            if(flag)continue;
            if(c=='L'){
              for(RI k=1;k<=(rep%4);k++)
              a[x].fx=L(a[x].fx);   
            }
            else if(c=='R'){
              for(RI k=1;k<=(rep%4);k++)
              a[x].fx=R(a[x].fx);   
            }
            else {
                for(RI k=1;k<=rep;k++)
                {
                int xx=dx[a[x].fx]+a[x].x;
                int yy=dy[a[x].fx]+a[x].y;
                if(!check(xx,yy)){
                    flag=1;
                    printf("Robot %d crashes into the wall\n",x);
                    break;
                }
                else {
                    if(vis[xx][yy]){
                        flag=1;
                        printf("Robot %d crashes into robot %d\n",x,vis[xx][yy]);
                        break;
                    }
                    else{
                        vis[a[x].x][a[x].y]=0;
                        vis[xx][yy]=x;
                        a[x].x=xx;a[x].y=yy;
                    }
                }
                }
            }
        }
        if(!flag)printf("OK\n");
    }
    return 0;
}
/*
4
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7

5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3

5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2

5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20
*/