题解:P12456 [JOI2025 预选赛 R2] 纸带

· · 题解

思路

我们需要记录每个方块的两种状态:一个是方块的颜色,另一个是方块是否被贴上了遮蔽胶带。\ 我们只需要开两个二维数组 \text{col,prot},它们分别代表每个方块的颜色和方块是否被贴上胶带。\ 接着我们根据题意,暴力模拟即可。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int col[1145][1145];
bool prot[1145][1145];

int fx[4] = {0, 1, 0, 1};
int fy[4] = {0, 0, 1, 1};
int main(){
    int h, w, q;
    cin >> h >> w >> q;
    while(q--){
        int op;
        cin >> op;
        if(op == 1){ // 操作 1
            int x, y, c;
            cin >> x >> y >> c;
            for(int i = 0; i < 4; ++i){
                int tx = x + fx[i];
                int ty = y + fy[i];
                if(!prot[tx][ty]) col[tx][ty] = c; //记得判断是否被胶带贴住哦
            }
        }
        else{ //操作 2
            int x, y;
            cin >> x >> y;
            for(int i = 0; i < 4; ++i){
                int tx = x + fx[i];
                int ty = y + fy[i];
                prot[tx][ty] = true;
            }
        }
    }
    for(int i = 1; i <= h; ++i){
        for(int j = 1; j <= w; ++j){
            cout << col[i][j] << ' ';
        }
        cout << endl;
    }
    return 0;
}