Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stack>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false);
- 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];
- int c = n + m;
- stack <int> s;
- bool change = true;
- vector<bool> b(n + m,true);
- while (change) {
- change = false;
- for (int i = 0; i < n; ++i) {
- if (v[i][0] != -1) {
- int a = 0,cur;
- while ((a < m) && (v[i][a] == 0)) ++a;
- if (a < m) cur = v[i][a];
- while ((a < m) && (v[i][a] == cur)) ++a;
- if (a == m) {
- bool f = true;
- for (int j = 0; j < m; ++j) {
- if (f&&v[i][j] > 0) {
- s.push(v[i][j]);
- f = false;
- }
- v[i][j] = 0;
- }
- --c;
- }
- }
- }
- for (int i = 0; i < m; ++i) {
- int a = 0;
- while (a < n) while ((v[a][i] == v[a-1][i]) || (v[a][i] == 0)) ++a;
- if (a == n) {
- bool f = true;
- for (int j = 0; j < n; ++j) {
- if (f&&v[j][i] > 0) {
- s.push(v[j][i]);
- f = false;
- }
- v[j][i] = 0;
- }
- --c;
- }
- }
- }
- while (!s.empty())
- {
- cout << s.top() << " ";
- s.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement