#include<iostream> #include<cmath> using namespace std; int chess[30][30];//棋盘 int main() { int n, m; cin >> n >> m; //初始化棋盘 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> chess[i][j]; } } //行标记 for (int i = 0; i < n; i++) { for (int j = 0; j < m-2; j++) { if (abs(chess[i][j]) == abs(chess[i][j + 1])&& abs(chess[i][j + 1]) == abs(chess[i][j + 2])) { if (chess[i][j] > 0) chess[i][j] = -chess[i][j]; if (chess[i][j+1] > 0) chess[i][j+1] = -chess[i][j+1]; if (chess[i][j+2] > 0) chess[i][j+2] = -chess[i][j+2]; } } } //列标记 for (int i = 0; i < m; i++) { for (int j = 0; j < n - 2; j++) { if (abs(chess[j][i]) == abs(chess[j + 1][i])&& abs(chess[j + 1][i]) == abs(chess[j + 2][i])) { if (chess[j][i] > 0) chess[j][i] = -chess[j][i]; if (chess[j+1][i] > 0) chess[j + 1][i] = -chess[j + 1][i]; if (chess[j+2][i] > 0) chess[j + 2][i] = -chess[j + 2][i]; } } } //输出结果 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (j != 0) cout << " "; if (chess[i][j] < 0) cout << 0; else cout << chess[i][j]; } cout << endl; } system("pause"); return 0; }
原文:https://www.cnblogs.com/syq816/p/12405053.html