#include using namespace std; int main() { //输入二维矩阵 int n, m; cin >> n >> m; vector> twoDimensionVec; for (int i = 0; i < n; i++) { vector oneDimensionVec; for (int j = 0; j < m; j++) { int c; cin >> c; oneDimensionVec.push_back(c); } twoDimensionVec.push_back(oneDimensionVec); } //记录两个要消除的坐标 vector, pair>> 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> 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; }