Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef vector<vector<int>> matrix;
  6.  
  7. bool check(const matrix m) {
  8. // проверим матрицу
  9. if (m.size() == 0 || m[0].size() == 0) return false;
  10. // первый проход
  11. for (int i = 0; i < m.size(); i++) {
  12. int count = 0;
  13. for (int j = 0; j < m[i].size(); j++) {
  14. if (m[i][j] == 0) count++;
  15. }
  16. if (count != 1) return false;
  17. }
  18.  
  19. for (int i = 0; i < m[0].size(); i++) {
  20. int count = 0;
  21. for (int j = 0; j < m.size(); j++) {
  22. if (m[j][i] == 0) count++;
  23. }
  24. if (count != 1) return false;
  25. }
  26.  
  27. return true;
  28. }
  29.  
  30. int main() {
  31. matrix p1 = { {0,4,2,2},{6,0,0,4},{0,0,6,4},{8,4,0,0}};
  32. matrix p2 = { {0,6,4,4},{4,0,3,4},{0,2,8,6},{6,4,5,0}};
  33. matrix p3 = { {0,6,4,4},{4,3,0,4},{1,0,8,6},{6,4,6,0}};
  34.  
  35. cout << "p1 = " << check(p1) << endl;
  36. cout << "p2 = " << check(p2) << endl;
  37. cout << "p3 = " << check(p3) << endl;
  38. return 0;
  39. }
  40.  
  41. #include <vector>
  42. #include <iostream>
  43. #include <iomanip>
  44.  
  45. using namespace std;
  46.  
  47. vector<vector<int>> p1 = { {0,4,2,2},{6,0,0,4},{0,0,6,4},{8,4,0,0}};
  48. vector<vector<int>> p2 = { {0,6,4,4},{4,0,0,4},{0,2,8,6},{6,4,0,0}};
  49.  
  50. vector<vector<int>> bipartee(const vector<vector<int>>& q)
  51. {
  52. vector<vector<int>> p;
  53. for(size_t r = 0; r < q.size(); ++r)
  54. {
  55. p.push_back(vector<int>());
  56. for(size_t c = 0; c < q.size(); ++c)
  57. if (q[r][c] == 0) p[r].push_back(c);
  58. }
  59. return p;
  60. }
  61.  
  62. bool try_kuhn(int v, vector<vector<int>>& g, vector<int>& mt, vector<bool>& used)
  63. {
  64. if (used[v]) return false;
  65. used[v] = true;
  66. for (size_t i=0; i<g[v].size(); ++i) {
  67. int to = g[v][i];
  68. if (mt[to] == -1 || try_kuhn(mt[to],g,mt,used))
  69. {
  70. mt[to] = v;
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76.  
  77. bool check(const vector<vector<int>>& p)
  78. {
  79. int n = p.size();
  80. vector<vector<int>> g = bipartee(p);
  81. vector<int> mt;
  82. vector<bool> used;
  83. mt.assign(n, -1);
  84. for(int v = 0; v < n; ++v) {
  85. used.assign(n,false);
  86. try_kuhn(v,g,mt,used);
  87. }
  88.  
  89. int sum = 0;
  90. for (int i=0; i<n; ++i)
  91. if (mt[i] != -1) ++sum;
  92. return (sum == n);
  93. }
  94.  
  95.  
  96. int main()
  97. {
  98. cout << "First matrix: " << check(p1) << endl;
  99. cout << "Second matrix: " << check(p2) << endl;
  100. }
  101.  
  102. vector<vector<int>> p1 = { {0, 4, 2, 2}, {6, 0, 0, 4}, {0, 0, 6, 4}, {8, 4, 0, 0}};
  103. vector<vector<int>> p2 = { {0, 6, 4, 4}, {4, 0, 0, 4}, {0, 2, 8, 6}, {6, 4, 0, 0}};
  104.  
  105. bool check(const vector<vector<int>>& p, int r = 0)
  106. {
  107. int n = p.size();
  108. static vector<bool> u;
  109. if (r == n) return true;
  110.  
  111. if (r == 0) u = vector<bool>(n,false);
  112.  
  113. for(int c = 0; c < n; ++c)
  114. {
  115. if (p[r][c] || u[c]) continue;
  116. u[c] = true;
  117. if (check(p,r+1)) return true;
  118. u[c] = false;
  119. }
  120. return false;
  121. }
  122.  
  123. int main()
  124. {
  125. cout << "First matrix: " << check(p1) << endl;
  126. cout << "Second matrix: " << check(p2) << endl;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement