摘要:本题难在麻烦二字,主要是需要注意第一次碰撞之后立刻要停止模拟.利用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--)
{
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;
}