Advertisement
Guest User

Untitled

a guest
May 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. using namespace std;
  5. int main() {
  6. ios_base::sync_with_stdio(false);
  7. int n, m;
  8. cin >> n >> m;
  9. vector<vector<int>> v(n, vector<int>(m));
  10. for (int i = 0; i < n; ++i)
  11. for (int j = 0; j < m; ++j)
  12. cin >> v[i][j];
  13. int c = n + m;
  14. stack <int> s;
  15. bool change = true;
  16. vector<bool> b(n + m,true);
  17. while (change) {
  18. change = false;
  19. for (int i = 0; i < n; ++i) {
  20. if (v[i][0] != -1) {
  21. int a = 0,cur;
  22. while ((a < m) && (v[i][a] == 0)) ++a;
  23. if (a < m) cur = v[i][a];
  24. while ((a < m) && (v[i][a] == cur)) ++a;
  25. if (a == m) {
  26. bool f = true;
  27. for (int j = 0; j < m; ++j) {
  28. if (f&&v[i][j] > 0) {
  29. s.push(v[i][j]);
  30. f = false;
  31. }
  32. v[i][j] = 0;
  33. }
  34. --c;
  35. }
  36. }
  37. }
  38. for (int i = 0; i < m; ++i) {
  39. int a = 0;
  40. while (a < n) while ((v[a][i] == v[a-1][i]) || (v[a][i] == 0)) ++a;
  41. if (a == n) {
  42. bool f = true;
  43. for (int j = 0; j < n; ++j) {
  44. if (f&&v[j][i] > 0) {
  45. s.push(v[j][i]);
  46. f = false;
  47. }
  48. v[j][i] = 0;
  49. }
  50. --c;
  51. }
  52. }
  53. }
  54. while (!s.empty())
  55. {
  56. cout << s.top() << " ";
  57. s.pop();
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement