题目类型 : 模拟
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct robots
{
int ix ;
int iy ;
int move ;
void remove(void);
bool event(int num , char e);
bool judge_wall(void);
bool judge_crash(int num);
} robot[120];
struct affairs
{
int robot; //事件发生的机器人编号
char run ; //事件类型
int time ; //事件执行次数
} affair[120];
struct maps
{
int mx ; //地图最大x坐标
int my ; //地图最大y坐标
int robot_num; //机器人数
int affair_num; //事件数
int node[120][120];
void init(void); //地图初始化
} map;
int main()
{
freopen("in.txt","r",stdin);
int ncase ;
bool safe ;
cin >> ncase ;
while(ncase --)
{
map.init();
safe = true ;
for(int k = 1, rr ; k <=map.affair_num&&safe; k++)
for(int tt = 1 ; tt <=affair[k].time; tt ++)
{
rr = affair[k].robot ;
map.node[robot[rr].ix][robot[rr].ix] = 0 ;
if(!robot[rr].event(rr,affair[k].run))
{
safe = false ;
break ;
}
map.node[robot[rr].ix][robot[rr].iy] = rr ;
}
if(safe)
cout<< "OK" <<endl;
}
return 0;
}
void maps::init(void)
{
char mm ;
memset(node,0,sizeof(node));
cin >> mx >> my ;
cin >> robot_num >> affair_num ;
for(int i = 1 ; i <= robot_num ; i ++)
{
cin >> robot[i].ix >> robot[i].iy >> mm ;
node[robot[i].ix][robot[i].iy] = i ; //将字符型的方向改为数值的方向
if(mm == 'N')
robot[i].move = 1 ;
else if(mm == 'E')
robot[i].move = 2 ;
else if(mm == 'S')
robot[i].move = 3 ;
else if(mm == 'W')
robot[i].move = 4 ;
}
for(int i = 1 ; i <= affair_num ; i ++)
cin >> affair[i].robot >> affair[i].run >> affair[i].time ;
return ;
}
void robots::remove(void)
{
switch(move)
{
case 1 :
iy ++ ;
break ;
case 2 :
ix ++ ;
break ;
case 3 :
iy -- ;
break ;
case 4 :
ix -- ;
break ;
}
return ;
}
bool robots::judge_crash(int num)
{
if(map.node[ix][iy] == 0 )
return true ;
cout<<"Robot "<<num<<" crashes into robot "<<map.node[ix][iy]<<endl;
return false ;
}
bool robots::event(int num,char e)
{
switch(e)
{
case 'L' :
move -- ;
if(move < 1)
move += 4 ;
break ;
case 'R' :
move ++ ;
if(move > 4)
move -= 4 ;
break ;
case 'F' :
remove();
if(!judge_wall())
{
cout<<"Robot "<<num<<" crashes into the wall"<<endl;
return false ;
}
if(!judge_crash(num))
{
return false;
}
break;
}
return true;
}
bool robots::judge_wall(void)
{
if(ix > map.mx || ix < 1 ||iy > map.my || iy < 1)
return false ;
else
return true ;
}