跳到正文

题解

题解:P11864 「o.OI R1」α+β Problem

lichuhan约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟
题目
P11864

版本

  1. v1当前

方法思路

我们需要构造一个推箱子游戏的地图,使得机器人按照给定的移动路径操作后,所有箱子都被推动过,且箱子的数量尽可能多。同时,每一步操作必须合法。具体步骤如下:

  1. 模拟机器人路径:

    • 首先,在空旷地图上模拟机器人的移动,记录每一步的位置和方向。这有助于确定机器人在无阻碍情况下的移动轨迹。
  2. 初始化地图:

    • 将整个地图初始化为墙壁(|)。然后,根据模拟得到的路径,将机器人经过的位置设为空地(.),确保路径畅通。
  3. 放置箱子(S):

    • 对于每个向前移动(W)的操作,在机器人即将到达的位置放置箱子。需确保推动箱子后的位置是合法的(即不是墙或已有箱子),必要时将该位置设为空地。
  4. 避免冲突:

    • 使用一个标记数组记录已经被推动过的位置,防止重复放置箱子导致非法操作。

解决代码

C++
#include <bits/stdc++.h>
using namespace std;

const int dx[] = {-1, 0, 1, 0};  // 上、左、下、右
const int dy[] = {0, -1, 0, 1};

vector<pair<int, int>> positions;  // 记录每一步的位置
vector<int> directions;            // 记录每一步后的方向

// 模拟机器人的移动路径,记录位置和方向
void simulate(int x, int y, const string& s) {
    positions.clear();
    directions.clear();
    x--; y--;  // 转换为0-based索引
    positions.emplace_back(x, y);
    directions.push_back(0);  // 初始方向朝上(0)
    int current_dir = 0;      // 当前方向
    for (char op : s) {
        if (op == 'A') {      // 左转
            current_dir = (current_dir - 1 + 4) % 4;
        } else if (op == 'D') { // 右转
            current_dir = (current_dir + 1) % 4;
        } else { // 处理移动操作W或S
            int move_dir = current_dir;
            if (op == 'S') {  // S操作是向后移动,方向相反
                move_dir = (current_dir + 2) % 4;
            }
            // 计算新位置
            int nx = positions.back().first + dx[move_dir];
            int ny = positions.back().second + dy[move_dir];
            positions.emplace_back(nx, ny);
        }
        directions.push_back(current_dir); // 记录当前方向
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        int n, m, x, y, k;
        string s;
        cin >> n >> m >> x >> y >> k >> s;

        // 模拟路径,得到所有位置和方向
        simulate(x, y, s);
        pair<int, int> initial_pos = {x-1, y-1}; // 初始位置

        // 初始化地图为全墙,并标记路径为空地
        vector<vector<char>> grid(n, vector<char>(m, '|'));
        vector<vector<bool>> used(n, vector<bool>(m, false)); // 标记推动后的位置

        // 将机器人路径上的所有位置设为空地
        for (const auto& pos : positions) {
            if (pos.first >= 0 && pos.first < n && pos.second >= 0 && pos.second < m) {
                grid[pos.first][pos.second] = '.';
            }
        }

        // 设置机器人的初始位置为X
        grid[initial_pos.first][initial_pos.second] = 'X';

        // 处理每个W操作,尝试放置箱子
        for (int i = 0; i < k; ++i) {
            if (s[i] != 'W') continue; // 只处理前进操作

            int current_step = i;
            int dir = directions[current_step]; // 当前方向
            pair<int, int> current_pos = positions[current_step];
            pair<int, int> next_pos = positions[current_step + 1]; // W操作后的位置

            // 初始位置不能放箱子
            if (next_pos == initial_pos) continue;

            // 计算推动后的位置(即箱子被推到的地方)
            int nx = next_pos.first + dx[dir];
            int ny = next_pos.second + dy[dir];

            // 检查该位置是否在地图外或已被使用
            if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
            if (used[nx][ny]) continue;

            // 确保当前的位置是空地(路径中的位置)
            if (grid[next_pos.first][next_pos.second] == '.') {
                grid[next_pos.first][next_pos.second] = 'S'; // 放置箱子
                // 如果推动后的位置是墙,则改为空地
                if (grid[nx][ny] == '|') {
                    grid[nx][ny] = '.';
                }
                used[nx][ny] = true; // 标记该位置已被使用
            }
        }

        // 输出地图
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                cout << grid[i][j];
            }
            cout << '\n';
        }
    }

    return 0;
}

代码解释

  1. 模拟路径:

    • simulate 函数通过处理每个移动指令(W, S, A, D),记录机器人的每一步位置和方向。
  2. 地图初始化:

    • 初始时,地图全为墙壁。随后,根据模拟得到的路径将机器人经过的位置设为空地,确保路径畅通。
  3. 放置箱子:

    • 对于每个W操作,计算机器人前进后的位置。若该位置可放置箱子,则在推动后的位置确保为空地,并标记该位置,避免重复使用。
  4. 合法性检查:

    • 使用used数组确保每个箱子被推动后到达的位置唯一,防止多个箱子被推到同一位置导致非法操作。

该方法确保所有箱子被推动且数量最大化,同时满足所有操作合法性要求。 不抄~~~~

原文