Files
python/TangDou/XiTi/1340.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

68 lines
1.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int main() {
//输入二维矩阵
int n, m;
cin >> n >> m;
vector<vector<int>> twoDimensionVec;
for (int i = 0; i < n; i++) {
vector<int> oneDimensionVec;
for (int j = 0; j < m; j++) {
int c;
cin >> c;
oneDimensionVec.push_back(c);
}
twoDimensionVec.push_back(oneDimensionVec);
}
//记录两个要消除的坐标
vector<pair<pair<int, int>, pair<int, int>>> vec;
//按行遍历
for (int i = 0; i < n; i++) {
//感觉要用双指针
for (int j = 0; j < m; j++) {
//从j开始的最大相等长度
int p = j + 1;
while (p < m && twoDimensionVec[i][p] == twoDimensionVec[i][j]) {
p++;
}
if (p - j >= 3) vec.push_back(make_pair(make_pair(i, j), make_pair(i, p - 1)));
}
}
//按列遍历
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
//从i开始的最大相等长度
int p = i + 1;
while (p < n && twoDimensionVec[p][j] == twoDimensionVec[i][j]) {
p++;
}
if (p - i >= 3) vec.push_back(make_pair(make_pair(i, j), make_pair(p - 1, j)));
}
}
//遍历vec对原数组进行清零
for (int i = 0; i < vec.size(); i++) {
pair<pair<int, int>, pair<int, int>> a = vec[i];
int x1 = a.first.first;
int y1 = a.first.second;
int x2 = a.second.first;
int y2 = a.second.second;
for (int p = x1; p <= x2; p++) {
for (int q = y1; q <= y2; q++) {
twoDimensionVec[p][q] = 0;
}
}
}
//输出结果
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << twoDimensionVec[i][j] << " ";
}
cout << endl;
}
return 0;
}