代码:
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#undef _CRT_SECURE_NO_WARNINGS
using namespace std;
int n, k;
class Command;
class Trigger;
class Robot
{
public:
int Index = -1, LeftHand = -1, RightHand = -1;
vector<Command*> commands;
void print() const
{
printf("Index = %d\tLeftHand = %d\tRightHand = %d\nCommand:", Index, LeftHand, RightHand);
for (size_t i = 0; i < commands.size(); i++)
{
printf("Command id=%llu on %p\n", i, commands[i]);
}
}
void run(bool intrigger);
};
Robot robots[105];
class Tools
{
public:
static int& LorR(Robot& rbt, int h)
{
if (h == 0) return rbt.LeftHand;
else if (h == 1) return rbt.RightHand;
else
{
printf("Error in function 'LorR'!\n'h' = %d is invaild!\n'h' must in {0,1}!\nRobot infomation:\n", h);
rbt.print();
while (true)
{
printf("");
}
}
}
static int& LorR(int id, int h) { return LorR(robots[id], h); }
static long long readll() { long long num; scanf("%lld", &num); return num; }
static int readint() { int num; scanf("%d", &num); return num; }
static string readstr() { string str; cin >> str; return str; }
static void getcheck() { if ((--k) == 0) exit(0); }
};
class CommandData
{
public:
int robotid = -1, h = -1, x = -1, y = -1, z = -1;
string COMMANDNAME = "None";
Command* COMMAND = nullptr;
string MyName = "COMMAND";
void print()
{
printf("robotid = %d, h = %d, x = %d, y = %d, z = %d, <COMMANDNAME> = %s, MyName = %s, <COMMAND> = %p", robotid, h, x, y, z, COMMANDNAME.c_str(), MyName.c_str(), COMMAND);
}
};
class Command
{
public:
CommandData data;
Command() = delete;
Command(const CommandData& dt) : data(dt) {}
virtual void run(bool intrigger)
{
printf("Error!\nCommand{");
data.print();
printf("}::run() shouldn't be called!\n");
while (true)
{
printf("");
}
}
virtual void chgrbt(int rbtid)
{
data.robotid = rbtid;
if (data.COMMAND != nullptr) data.COMMAND->chgrbt(rbtid);
}
virtual void do_trigger(bool intrigger) const;
};
class SlackOff : public Command
{
public:
using Command::Command;
void run(bool intrigger) { printf("Robot %d slacks off.\n", data.robotid); Tools::getcheck(); do_trigger(intrigger); }
};
class Move : public Command
{
public:
using Command::Command;
void run(bool intrigger)
{
int h = data.h, z = data.z;
Robot& rbt = robots[data.robotid];
printf("Robot %d moves its ", rbt.Index);
int& rbtHand = Tools::LorR(rbt, h);
rbtHand = (rbtHand + z) % n;
if (h == 0) printf("left");
else printf("right");
printf(" hand towards Robot %d.\n", h == 0 ? rbt.LeftHand : rbt.RightHand);
Tools::getcheck();
do_trigger(intrigger);
}
};
class Swap : public Command
{
public:
using Command::Command;
void run(bool intrigger);
};
class Mirror : public Command
{
public:
using Command::Command;
void run(bool intrigger)
{
int h = data.h, x = data.x;
Robot& it = robots[Tools::LorR(data.robotid, h)];
Command& cmd = *it.commands[x];
while (cmd.data.COMMANDNAME != "None") cmd = *cmd.data.COMMAND;//说明是TRIGGER
cmd.data.h ^= 1;
printf("Robot %d modifies a line of command of Robot %d.\n", data.robotid, it.Index);
Tools::getcheck();
do_trigger(intrigger);
}
};
class Replace : public Command
{
public:
using Command::Command;
void run(bool intrigger);
};
class Activate : public Command
{
public:
using Command::Command;
void run(bool intrigger)
{
Robot& it = robots[Tools::LorR(data.robotid, data.h)];
printf("Robot %d activates Robot %d.\n", data.robotid, it.Index);
Tools::getcheck();
it.run(intrigger);
do_trigger(intrigger);
}
};
class Trigger : public Command
{
public:
using Command::Command;
int CommandLine;
bool deleted = false;
void run(bool intrigger)
{
printf("Robot %d t-tr-tri-trig-trigg-trigge-trigger-trigger!\n", data.robotid);
}
void realrun(bool intrigger) const
{
data.COMMAND->run(true);
do_trigger(intrigger);
}
friend bool operator<(const Trigger& x, const Trigger& y) { return x.CommandLine > y.CommandLine; }
};
Command* ReadCommand(int index, int cmdline)
{
string CmdName;
cin >> CmdName;
Command* cmd = nullptr;
if (CmdName == "SLACKOFF") cmd = new SlackOff({ index });
if (CmdName == "MOVE") cmd = new Move({ index, Tools::readint(), -1, -1, Tools::readint() });
if (CmdName == "SWAP") cmd = new Swap({ index, Tools::readint(), Tools::readint(), Tools::readint() });
if (CmdName == "MIRROR") cmd = new Mirror({ index, Tools::readint(), Tools::readint() });
if (CmdName == "REPLACE")
{
//于是,好玩的事情发生了。
cmd = new Replace({ index, Tools::readint(), Tools::readint() });
cmd->data.COMMAND = ReadCommand(Tools::LorR(index, cmd->data.h), cmd->data.x);
}
if (CmdName == "ACTIVATE") cmd = new Activate({ index, Tools::readint() });
if (CmdName == "TRIGGER")
{
Trigger* tmp = new Trigger(
CommandData{ index, -1, -1, -1, -1,
Tools::readstr(), ReadCommand(index, -1) });
tmp->data.COMMANDNAME = tmp->data.COMMANDNAME.substr(0, tmp->data.COMMANDNAME.size() - 1);
tmp->CommandLine = cmdline;
cmd = tmp;
}
if (cmd == nullptr) abort();
cmd->data.MyName = CmdName;
return cmd;
}
int main()
{
//freopen(R"(E:\gugu\P9966\down\down\5.out)", "w", stdout);
int m;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++)
{
Robot& rbt = robots[i];
rbt.Index = i;
scanf("%d%d", &rbt.LeftHand, &rbt.RightHand);
for (int j = 1; j <= m; j++)
{
rbt.commands.push_back(ReadCommand(i, j));
//rbt.commands.back()->data.print();
//printf("\n");
}
}
//printf("Read OK!!\n");
while (true)
{
for (int i = 0; i < n; i++)
{
robots[i].run(false);
}
}
return 0;
}
void Robot::run(bool intrigger)
{
for (Command* it : commands)
{
if (it->data.MyName != "TRIGGER") it->run(intrigger); //如果是TRIGGER不执行
}
}
void Command::do_trigger(bool intrigger) const
{
if (Tools::LorR(data.robotid, 1) == data.robotid) return;
Robot& rbt = robots[Tools::LorR(data.robotid, 1)];
for (Command* ncmd : rbt.commands)
{
if (ncmd->data.MyName == "TRIGGER" && (ncmd->data.COMMANDNAME == data.MyName || ncmd->data.COMMANDNAME == "TRIGGER" && intrigger))
{
((Trigger*)ncmd)->realrun(intrigger);
return;
}
}
return;
}
void Swap::run(bool intrigger)
{
int h = data.h, x = data.x, y = data.y;
Robot& me = robots[data.robotid];
Robot& it = robots[Tools::LorR(data.robotid, h)];
//Command& a = *me.commands[y - 1];
//Command& b = *it.commands[x - 1];
//swap(a, b);
swap(me.commands[y - 1], it.commands[x - 1]);
int historymr = me.commands[y - 1]->data.robotid, historyir = it.commands[x - 1]->data.robotid;
me.commands[y - 1]->chgrbt(historyir);
it.commands[x - 1]->chgrbt(historymr);
//swap(a.data.robotid, b.data.robotid);
printf("Robot %d swaps a line of command with Robot %d.\n", me.Index, it.Index);
Tools::getcheck();
do_trigger(intrigger);
}
void Replace::run(bool intrigger)
{
int h = data.h, x = data.x;
Command* COMMAND = data.COMMAND;
Robot& it = robots[Tools::LorR(data.robotid, h)];
Command* cmd = it.commands[x - 1];
//delete cmd;
it.commands[x - 1] = COMMAND;
it.commands[x - 1]->data.robotid = it.Index;
printf("Robot %d replaces a line of command of Robot %d.\n", data.robotid, it.Index);
Tools::getcheck();
do_trigger(intrigger);
}
Visual Studio 提示 vector subscript out of range,应该是数组越界,但是我找不出来 ToT。