思路:
使用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;
}
}