小 Q 最近发现了一款新游戏游戏的目标是从一个新手修炼成为武功高强的大侠。面对错综复杂的游戏世界小 Q 要对他面临的每件事情做出谨慎的选择。例如是否参加一个陌生人邀请的比武;同意或是拒绝用宝剑交换他人的武功秘籍而小 Q 做出的每一个选择都有可能影响到他以后的发展:面对一个高手若主动与之比武很可能会损失惨重;但若不去比武也许今后就再也见不到这个高手了。对着这个游戏小 Q 玩了很多次仍然玩不出他想要的
#include
using namespace std;
int main() { int n, m; cin >> n >> m;
vector<int> variables(m + 1, 0); // Variables, indexed from 1 to m
vector<string> events(n + 1); // Events, indexed from 1 to n
// Read events
for (int i = 1; i <= n; i++) {
cin >> events[i];
}
int currentEvent = 1; // Current event index
vector<int> choices; // Choices made during execution
while (currentEvent <= n) {
string event = events[currentEvent];
if (event[0] == 'v') {
// Variable event
int variableIndex;
stringstream(event.substr(1)) >> variableIndex;
if (event.find('+') != string::npos) {
// Increase variable
int amount;
stringstream(event.substr(event.find('+') + 1)) >> amount;
variables[variableIndex] += amount;
} else if (event.find('-') != string::npos) {
// Decrease variable
int amount;
stringstream(event.substr(event.find('-') + 1)) >> amount;
variables[variableIndex] -= amount;
}
currentEvent++;
} else if (event[0] == 's') {
// Choice jump event
int choice1, choice2;
stringstream(event.substr(1)) >> choice1 >> choice2;
// Get player's choice
int choice;
cin >> choice;
choices.push_back(choice);
// Update current event index based on player's choice
currentEvent = (choice == 1) ? choice1 : choice2;
} else if (event[0] == 'i') {
// Condition jump event
int variable1, variable2, choice1, choice2;
stringstream(event.substr(1)) >> variable1 >> variable2 >> choice1 >> choice2;
// Check condition
if (variables[variable1] < variables[variable2]) {
currentEvent = choice1;
} else {
currentEvent = choice2;
}
}
}
// Output choices made during execution
for (int i = 0; i < choices.size(); i++) {
cout << choices[i] << endl;
}
return 0;
原文地址: https://www.cveoy.top/t/topic/ituP 著作权归作者所有。请勿转载和采集!