跳到正文

题解

题解:P1335 [NOI2013] 小 Q 的修炼(纯娱乐)

zhangzhening0121约 13 分钟
原文
文章信息与版本

文章信息

收录
2026年5月29日
更新
2026年5月29日
篇幅
约 13 分钟
获赞
1
题目
P1335

版本

  1. v1当前

这是一个有我与我的teacher共同努力完成的,好时6时1天,耗资巨大给同学买的冰红茶12元。题解如下:

这是一个典型的程序分析与控制流优化问题。由于题目是“提交答案型”,且输入文件 train1.in 到 train10.in 位于本地,所以运用一个完整的 C++ 求解器。

该求解器采用‌深度优先搜索 (DFS)‌ 结合‌记忆化剪枝‌(如果状态空间允许)或纯 DFS(针对小规模或无环/简单环结构)来寻找使变量 1(成就值)最大化的路径。考虑到游戏脚本可能包含循环,纯 DFS 可能会陷入死循环,因此需要检测访问状态或使用迭代加深/限制深度。但在大多数此类竞赛题中,脚本通常是有向无环图 (DAG) 或者循环次数有限。为了通用性,下面的代码实现了一个带有简单环路检测(基于执行步数上限)的 DFS 搜索。

‌注意‌:如果状态空间非常大(变量多且值域大),可能需要更高级的符号执行或约束求解。但对于一般规模的 train 数据,DFS 通常有效。

代码:

C++
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <map>
#include <stack>

using namespace std;

// 定义量的类型
enum QuantityType {
    CONST_Q,
    VAR_Q
};

struct Quantity {
    QuantityType type;
    int value; // 如果是常数,则是常数值;如果是变量,则是变量编号(1-based)
};

// 定义事件类型
enum EventType {
    OP_EVENT,      // 普通事件
    SELECT_EVENT,  // 选择跳转
    COND_EVENT     // 条件跳转
};

struct Event {
    EventType type;
    
    // 普通事件数据
    int var_id;    // 目标变量编号 (1-based)
    char op;       // '+' or '-'
    Quantity qty;  // 操作数

    // 选择跳转数据
    int target1;
    int target2;

    // 条件跳转数据
    Quantity cond_q1;
    Quantity cond_q2;
    int cond_target1;
    int cond_target2;
};

class GameSolver {
private:
    int n, m;
    vector<Event> events;
    vector<int> best_choices;
    long long max_score;
    
    // 用于防止无限循环,设置最大执行步数
    // 如果脚本中有复杂循环,可能需要更智能的检测,这里设一个较大的上限
    static const int MAX_STEPS = 1000000; 

    // 获取量的值
    long long getQtyValue(const Quantity& q, const vector<long long>& vars) {
        if (q.type == CONST_Q) {
            return q.value;
        } else {
            // 变量编号从1开始,数组从0开始
            if (q.value >= 1 && q.value <= m) {
                return vars[q.value - 1];
            }
            return 0; // 错误处理
        }
    }

    // DFS 搜索
    // pos: 当前事件编号 (1-based)
    // vars: 当前变量状态
    // choices: 当前已做的选择序列
    // steps: 当前执行步数,防止死循环
    void dfs(int pos, vector<long long>& vars, vector<int>& choices, int steps) {
        if (steps > MAX_STEPS) {
            return; // 超时/死循环保护
        }

        // 如果位置无效,游戏结束
        if (pos < 1 || pos > n) {
            long long score = vars; // 变量1的值
            if (score > max_score) {
                max_score = score;
                best_choices = choices;
            }
            return;
        }

        const Event& e = events[pos - 1];

        if (e.type == OP_EVENT) {
            long long val = getQtyValue(e.qty, vars);
            if (e.op == '+') {
                vars[e.var_id - 1] += val;
            } else {
                vars[e.var_id - 1] -= val;
            }
            dfs(pos + 1, vars, choices, steps + 1);
            // 回溯
            if (e.op == '+') {
                vars[e.var_id - 1] -= val;
            } else {
                vars[e.var_id - 1] += val;
            }
        } 
        else if (e.type == SELECT_EVENT) {
            // 选择 1
            choices.push_back(1);
            dfs(e.target1, vars, choices, steps + 1);
            choices.pop_back();

            // 选择 2
            choices.push_back(2);
            dfs(e.target2, vars, choices, steps + 1);
            choices.pop_back();
        } 
        else if (e.type == COND_EVENT) {
            long long v1 = getQtyValue(e.cond_q1, vars);
            long long v2 = getQtyValue(e.cond_q2, vars);
            
            int next_pos;
            if (v1 < v2) {
                next_pos = e.cond_target1;
            } else {
                next_pos = e.cond_target2;
            }
            dfs(next_pos, vars, choices, steps + 1);
        }
    }

public:
    void solve(const string& input_file, const string& output_file) {
        ifstream fin(input_file);
        if (!fin.is_open()) {
            cerr << "Error opening file: " << input_file << endl;
            return;
        }

        fin >> n >> m;
        events.resize(n);

        for (int i = 0; i < n; ++i) {
            string token;
            fin >> token;
            
            if (token == "v") {
                // 普通事件: v var_id op type val
                // 例如: v 1 + c 1
                int var_id;
                char op;
                string q_type;
                int q_val;
                
                fin >> var_id >> op >> q_type >> q_val;
                
                events[i].type = OP_EVENT;
                events[i].var_id = var_id;
                events[i].op = op;
                events[i].qty.type = (q_type == "c") ? CONST_Q : VAR_Q;
                events[i].qty.value = q_val;
            } 
            else if (token == "s") {
                // 选择跳转: s t1 t2
                int t1, t2;
                fin >> t1 >> t2;
                events[i].type = SELECT_EVENT;
                events[i].target1 = t1;
                events[i].target2 = t2;
            } 
            else if (token == "i") {
                // 条件跳转: i q1_type q1_val q2_type q2_val t1 t2
                string q1_type, q2_type;
                int q1_val, q2_val, t1, t2;
                
                fin >> q1_type >> q1_val >> q2_type >> q2_val >> t1 >> t2;
                
                events[i].type = COND_EVENT;
                events[i].cond_q1.type = (q1_type == "c") ? CONST_Q : VAR_Q;
                events[i].cond_q1.value = q1_val;
                events[i].cond_q2.type = (q2_type == "c") ? CONST_Q : VAR_Q;
                events[i].cond_q2.value = q2_val;
                events[i].cond_target1 = t1;
                events[i].cond_target2 = t2;
            }
        }
        fin.close();

        // 初始化状态
        vector<long long> vars(m, 0);
        vector<int> choices;
        max_score = LLONG_MIN;
        best_choices.clear();

        // 开始搜索
        dfs(1, vars, choices, 0);

        // 输出结果
        ofstream fout(output_file);
        for (int c : best_choices) {
            fout << c << endl;
        }
        fout.close();
        
        cout << "Solved " << input_file << " -> " << output_file 
             << " (Score: " << max_score << ", Choices: " << best_choices.size() << ")" << endl;
    }
};

int main() {
    GameSolver solver;
    
    // 处理 train1.in 到 train10.in
    for (int i = 1; i <= 10; ++i) {
        string in_file = "train" + to_string(i) + ".in";
        string out_file = "train" + to_string(i) + ".out";
        solver.solve(in_file, out_file);
    }

    return 0;
}

属实不易,不喜勿喷!!!

原文