问题描述
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int n, m, a[31][31], x; 6 int flag[31][31] = { 0 }; 7 cin >> n >> m; 8 for (int i = 1; i <= n; i++) 9 for (int j = 1; j <= m; j++) 10 cin >> a[i][j]; 11 for (int i = 1; i <=n; i++) 12 for (int j = 1; j <= m; j++) { 13 if (a[i - 1][j] == a[i][j] && a[i][j] == a[i + 1][j]) { 14 x = a[i][j]; 15 flag[i][j] = 1; 16 for (int k = i + 1;; k++) { 17 if (a[k][j] == x)flag[k][j] = 1; 18 else break; 19 } 20 for (int k = i - 1;; k--) { 21 if (a[k][j] == x)flag[k][j] = 1; 22 else break; 23 } 24 } 25 if (a[i][j - 1] == a[i][j] && a[i][j] == a[i][j + 1]) { 26 x = a[i][j]; 27 flag[i][j] = 1; 28 for (int k = j + 1;; k++) { 29 if (a[i][k] == x)flag[i][k] = 1; 30 else break; 31 } 32 for (int k = j - 1;; k--) { 33 if (a[i][k] == x)flag[i][k] = 1; 34 else break; 35 } 36 } 37 } 38 for (int i = 1; i <= n; i++) { 39 for (int j = 1; j <= m; j++) { 40 if (flag[i][j] == 1)a[i][j] = 0; 41 } 42 } 43 44 for (int i = 1; i <= n; i++) { 45 for (int j = 1; j <= m; j++) 46 cout << a[i][j] << " "; 47 cout << endl; 48 } 49 return 0; 50 }
原文:https://www.cnblogs.com/cqy1245053939/p/10137037.html