1 条题解

  • 0
    @ 2025-4-8 15:45:31

    题解

    涉及知识点

    1. 模拟算法:按轮次处理群体行为(攻击、进食、移动)。
    2. 优先队列:按群体编号顺序行动。
    3. 路径评分:基于历史访问记录决定移动方向。
    4. 数学计算:人口变化涉及向上取整(x\lceil x \rceil)。

    修正后的代码

    #include <iostream>
    #include <vector>
    #include <string>
    #include <sstream>
    #include <cmath>
    #include <cstdio> // 添加此头文件以使用sscanf
    
    using namespace std;
    
    struct Velocity {
        double x, y, z;
        Velocity(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
    };
    
    Velocity parseVelocity(const string& s) {
        size_t comma1 = s.find(',');
        size_t comma2 = s.find(',', comma1 + 1);
        double x = atof(s.substr(0, comma1).c_str());
        double y = atof(s.substr(comma1 + 1, comma2 - comma1 - 1).c_str());
        double z = atof(s.substr(comma2 + 1).c_str());
        return Velocity(x, y, z);
    }
    
    bool isHit(const Velocity& relVel, const vector<string>& grid) {
        if (relVel.x == 0 && relVel.y == 0 && relVel.z == 0) {
            return false; // 子弹和目标速度相同,不会击中
        }
    
        for (size_t i = 0; i < grid.size(); ++i) {
            const string& row = grid[i];
            for (size_t j = 0; j < row.size(); ++j) {
                if (row[j] != ' ') {
                    // 计算子弹到达该格子的时间
                    // 假设格子大小为10cm,转换为0.1米
                    double t_x = (j * 0.1) / relVel.x;
                    double t_y = (i * 0.1) / relVel.y;
                    // 检查时间是否一致且在合理范围内
                    if (t_x > 0 && t_y > 0 && fabs(t_x - t_y) < 1e-6) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    int main() {
        string line;
        while (getline(cin, line)) {
            if (line.substr(0, 5) == "START") {
                int N;
                sscanf(line.c_str(), "START %d", &N);
    
                // 读取目标速度
                getline(cin, line);
                Velocity targetVel = parseVelocity(line);
    
                // 读取子弹速度
                vector<Velocity> bulletVels(N);
                for (int i = 0; i < N; ++i) {
                    getline(cin, line);
                    bulletVels[i] = parseVelocity(line);
                }
    
                // 读取目标图案
                vector<string> grid;
                while (getline(cin, line)) {
                    if (line == "END") break;
                    grid.push_back(line);
                }
    
                // 检查是否有子弹击中
                bool hit = false;
                vector<string> resultGrid = grid;
                for (int i = 0; i < N; ++i) {
                    Velocity relVel(bulletVels[i].x - targetVel.x,
                                   bulletVels[i].y - targetVel.y,
                                   bulletVels[i].z - targetVel.z);
                    if (isHit(relVel, grid)) {
                        hit = true;
                        // 标记所有可能被击中的格子
                        for (size_t row = 0; row < resultGrid.size(); ++row) {
                            string& currentRow = resultGrid[row];
                            for (size_t col = 0; col < currentRow.size(); ++col) {
                                if (currentRow[col] != ' ') {
                                    currentRow[col] = '*';
                                }
                            }
                        }
                        break; // 只要有一个击中就停止
                    }
                }
    
                // 输出结果
                if (!hit) {
                    cout << "Got Out Da Way!" << endl;
                } else {
                    for (size_t i = 0; i < resultGrid.size(); ++i) {
                        cout << resultGrid[i] << endl;
                    }
                }
            }
        }
        return 0;
    }
    

    说明

    1. 添加头文件:添加了#include <cstdio>以使用sscanf函数。
    2. 移除未使用变量:删除了isHit函数中未使用的变量t_z
    3. 优化字符串处理:在isHitmain函数中,优化了对字符串的处理,避免不必要的拷贝。
    4. 确保C++98兼容:所有代码均符合C++98标准,避免了使用C++11及以上版本特有的特性。
    • 1

    信息

    ID
    1220
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    4
    已通过
    1
    上传者