Segmentation fault with invalid memory reference.
#include<bits/stdc++.h>
using namespace std;
struct Slackoff;
struct Move;
struct Swap;
struct Mirror;
struct Replace;
struct Activate;
struct Trigger;
int n,m,k,last;
struct Counter{
int now;
Counter(){}
inline void init(){now=k;}
void operator--(){if(!--now)exit(0);}
void operator--(int){return --*this;}
}cnt;
enum{
SLACKOFF=1,
MOVE =2,
SWAP =3,
MIRROR =4,
REPLACE =5,
ACTIVATE=6,
TRIGGER =7
};
inline int getcmdtype(string s)
{return s[0]=='S'?(s[1]=='L'?SLACKOFF:SWAP):(s[0]=='M'?(s[1]=='O'?MOVE:MIRROR):(s[0]=='R'?REPLACE:(s[0]=='A'?ACTIVATE:TRIGGER)));}
struct Command;
struct Robot{
Robot(){}
int id,l,r;
vector<Command*> c;
}rob[505];
struct Command{
int type,x,y,z;
bool h;
Command *cmd;
Command(){}
virtual ~Command(){};
virtual void init() = 0;
virtual void recall(int){};
virtual Command *clone() = 0;
virtual Command *mirror() = 0;
// virtual void trigger(Robot&,int){};
};
inline void trigger(int x,int tp){
// cerr<<"now trigger:"<<x<<" "<<tp<<'\n';
for(int i=0;i<m;i++)
if((rob[x].c[i]->type)==TRIGGER&&((rob[x].c[i]->x)==tp||(last&&(rob[x].c[i]->x)==TRIGGER))){
// cerr<<"trigger:\n",
last=1;
rob[x].c[i]->cmd->recall(x);
if(rob[x].r!=x)
trigger(rob[x].r,rob[x].c[i]->cmd->type);
return ;
}
}
struct Slackoff : public Command{
Slackoff(){}
~Slackoff(){}
inline void init(){
type=SLACKOFF;
x=y=z=h=0;
}
inline void recall(int x){
cout<<"Robot "<<x<<" slacks off.\n";
--cnt;
}
inline Slackoff *clone(){
return (new Slackoff(*this));
}
inline Slackoff *mirror(){
return this->clone();
}
};
struct Move : public Command{
Move(){}
~Move(){}
inline void init(){
type=MOVE;
cin>>h>>z;
x=y=0;
}
inline void recall(int id){
int& hand=(h?(rob[id].r):(rob[id].l));
hand=(hand+z)%n;
cout<<"Robot "<<id<<" moves its "<<(h?"right":"left")<<" hand towards Robot "
<<hand
<<".\n";
--cnt;
}
inline Move *clone(){
return (new Move(*this));
}
inline Move *mirror(){
Move *reans=this->clone();
reans->h^=1;
return reans;
}
};
struct Swap : public Command{
Swap(){}
~Swap(){}
inline void init(){
type=SWAP;
cin>>h>>x>>y;
z=0;
}
inline void recall(int id){
cout<<"Robot "<<id<<" swaps a line of command with Robot "<<(h?(rob[id].r):(rob[id].l))<<".\n";
--cnt;
swap(rob[id].c[(this->y)-1],rob[(h?(rob[id].r):(rob[id].l))].c[(this->x)-1]);
}
inline Swap *clone(){
return (new Swap(*this));
}
inline Swap *mirror(){
Swap *reans=this->clone();
reans->h^=1;
return reans;
}
};
struct Mirror : public Command{
Mirror(){}
~Mirror(){}
inline void init(){
type=MIRROR;
cin>>h>>x;
y=z=0;
}
inline void recall(int id){
cout<<"Robot "<<id<<" modifies a line of command of Robot "<<(h?(rob[id].r):(rob[id].l))<<".\n";
--cnt;
rob[(h?(rob[id].r):(rob[id].l))].c[(this->x)-1]=(rob[(h?(rob[id].r):(rob[id].l))].c[(this->x)-1])->mirror();
}
inline Mirror *clone(){
return (new Mirror(*this));
}
inline Mirror *mirror(){
Mirror *reans=this->clone();
reans->h^=1;
return reans;
}
};
struct Replace : public Command{
Replace(){}
~Replace(){delete cmd;}
inline void init();
inline void recall(int id){
cout<<"Robot "<<id<<" replaces a line of command of Robot "<<(h?(rob[id].r):(rob[id].l))<<".\n";
--cnt;
rob[(h?(rob[id].r):(rob[id].l))].c[(this->x)-1]=cmd;
}
inline Replace *clone(){
return (new ::Replace(*this));
}
inline Replace *mirror(){
Replace *reans=this->clone();
reans->h^=1;
return reans;
}
};
struct Activate : public Command{
Activate(){}
~Activate(){}
inline void init(){
type=ACTIVATE;
cin>>h;
x=y=z=0;
}
inline void recall(int id){
cout<<"Robot "<<id<<" activates Robot "<<(h?(rob[id].r):(rob[id].l))<<".\n";
--cnt;
int hand=(h?(rob[id].r):(rob[id].l));
for(int i=0;i<m;i++){
if(rob[hand].c[i]->type==TRIGGER)
continue;
last=0;
rob[hand].c[i]->recall(hand);
trigger(rob[hand].r,rob[hand].c[i]->type);
}
}
inline Activate *clone(){
return (new Activate(*this));
}
inline Activate *mirror(){
Activate *reans=this->clone();
reans->h^=1;
return reans;
}
};
struct Trigger : public Command{
Trigger(){}
~Trigger(){delete cmd;}
inline void init(){
type=TRIGGER;
string cmt;
cin>>cmt;
cmt.pop_back();
x=getcmdtype(cmt);
cin>>cmt;
switch(getcmdtype(cmt)){
case SLACKOFF:
cmd=new Slackoff;
break;
case MOVE:
cmd=new Move;
break;
case SWAP:
cmd=new Swap;
break;
case MIRROR:
cmd=new Mirror;
break;
case REPLACE:
cmd=new Replace;
break;
case ACTIVATE:
cmd=new Activate;
break;
case TRIGGER:
cmd=new Trigger;
break;
}
cmd->init();
y=z=0;
}
inline Trigger *clone(){
return (new Trigger(*this));
}
inline Trigger *mirror(){
Trigger *reans=this->clone();
reans->cmd=(reans->cmd)->mirror();
return reans;
}
};
inline void Replace::init(){
type=REPLACE;
string cmt;
cin>>h>>x>>cmt;
switch(getcmdtype(cmt)){
case SLACKOFF:
cmd=new Slackoff;
break;
case MOVE:
cmd=new Move;
break;
case SWAP:
cmd=new Swap;
break;
case MIRROR:
cmd=new Mirror;
break;
case REPLACE:
cmd=new Replace;
break;
case ACTIVATE:
cmd=new Activate;
break;
case TRIGGER:
cmd=new Trigger;
break;
}
cmd->init();
y=z=0;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>m>>k;
cnt.init();
for(int i=0;i<n;i++){
string cmt;
cin>>rob[i].l>>rob[i].r;
rob[i].c.resize(m);
for(int j=0;j<m;j++){
cin>>cmt;
switch(getcmdtype(cmt)){
case SLACKOFF:
rob[i].c[j]=new Slackoff;
break;
case MOVE:
rob[i].c[j]=new Move;
break;
case SWAP:
rob[i].c[j]=new Swap;
break;
case MIRROR:
rob[i].c[j]=new Mirror;
break;
case REPLACE:
rob[i].c[j]=new Replace;
break;
case ACTIVATE:
rob[i].c[j]=new Activate;
break;
case TRIGGER:
rob[i].c[j]=new Trigger;
break;
}
rob[i].c[j]->init();
}
}
for(int i=0;;(++i)%=n)
for(int j=0;j<m;j++){
if(rob[i].c[j]->type==TRIGGER)
continue;
last=0;
rob[i].c[j]->recall(i);
if(rob[i].r!=i)
trigger(rob[i].r,rob[i].c[j]->type);
}
return 0;
}