代码如下:
#include <bits/stdc++.h> using namespace std; //消除游戏 int main(){ int n, m; cin >> n >> m; vector< vector<int> > v(n, vector<int>(m)); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ cin >> v[i][j]; } } vector< pair<int, int> > index; for(int i = 0; i < n; i++){ for(int j = 0; j < m - 2; j++){ if(v[i][j] == v[i][j+1] && v[i][j+1] == v[i][j+2]){ index.push_back(pair<int, int>(i, j)); index.push_back(pair<int, int>(i, j+1)); index.push_back(pair<int, int>(i, j+2)); } } } for(int j = 0; j < m; j++){ for(int i = 0; i < n - 2; i++){ if(v[i][j] == v[i+1][j] && v[i+1][j] == v[i+2][j]){ index.push_back(pair<int, int>(i, j)); index.push_back(pair<int, int>(i+1, j)); index.push_back(pair<int, int>(i+2, j)); } } } for(int i = 0; i < index.size(); i++){ int a = index[i].first, b = index[i].second; v[a][b] = 0; } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ cout << v[i][j] << " "; } cout << endl; } return 0; }