You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}