fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int n, m, tar;
  8. bool a[10][10];
  9. set<string> seen;
  10.  
  11. string to_string_matrix(bool mat[10][10]) {
  12. string res;
  13. for (int i = 0; i < n; i++) {
  14. for (int j = 0; j < m; j++) {
  15. res += a[i][j] ? '1' : '0';
  16. }
  17. }
  18. return res;
  19. }
  20.  
  21. string rotate_90(const string &s) {
  22. string t(n * m, '0');
  23. for (int i = 0; i < n; i++) {
  24. for (int j = 0; j < m; j++) {
  25. t[j * n + (n - 1 - i)] = s[i * m + j];
  26. }
  27. }
  28. return t;
  29. }
  30.  
  31. string reflect_h(const string &s) {
  32. string t = s;
  33. for (int i = 0; i < n; i++) {
  34. reverse(t.begin() + i * m, t.begin() + (i + 1) * m);
  35. }
  36. return t;
  37. }
  38.  
  39. string reflect_v(const string &s) {
  40. string t = s;
  41. for (int i = 0; i < n; i++) {
  42. for (int j = 0; j < m; j++) {
  43. t[(n - 1 - i) * m + j] = s[i * m + j];
  44. }
  45. }
  46. return t;
  47. }
  48.  
  49. string min_representation(string s) {
  50. vector<string> variants;
  51. variants.push_back(s);
  52. variants.push_back(reflect_h(s));
  53. variants.push_back(reflect_v(s));
  54.  
  55.  
  56. string r = rotate_90(s);
  57. variants.push_back(r);
  58. variants.push_back(reflect_h(r));
  59. variants.push_back(reflect_v(r));
  60.  
  61. r = rotate_90(r);
  62. variants.push_back(r);
  63. variants.push_back(reflect_h(r));
  64. variants.push_back(reflect_v(r));
  65.  
  66. return *min_element(variants.begin(), variants.end());
  67. }
  68.  
  69. bool check() {
  70. for (int d = 1; d <= min(n, m) - 1; d++) {
  71. for (int i = 0; i + d < n; i++) {
  72. for (int j = 0; j + d < m; j++) {
  73. if (a[i][j] && a[i + d][j] && a[i][j + d] && a[i + d][j + d]) {
  74. return false;
  75. }
  76. }
  77. }
  78. }
  79. return true;
  80. }
  81.  
  82. int valid_count = 0;
  83.  
  84. void dfs(int i, int j, int k) {
  85. if (j == m) {
  86. i++;
  87. j = 0;
  88. }
  89.  
  90. if (i == n) {
  91. if (k == tar && check()) {
  92. string key = to_string_matrix(a);
  93. string rep = min_representation(key);
  94. if (seen.count(rep)) return; // 對稱解出現過就跳過
  95.  
  96. seen.insert(rep);
  97. valid_count++;
  98.  
  99. for (int x = 0; x < n; x++) {
  100. for (int y = 0; y < m; y++) {
  101. cout << a[x][y] << " ";
  102. }
  103. cout << "\n";
  104. }
  105. cout << "--------\n";
  106. }
  107. return;
  108. }
  109.  
  110. a[i][j] = 0;
  111. dfs(i, j + 1, k);
  112.  
  113. if (k < tar) {
  114. a[i][j] = 1;
  115. dfs(i, j + 1, k + 1);
  116. }
  117. }
  118.  
  119. int main() {
  120. cin >> n >> m >> tar;
  121. dfs(0, 0, 0);
  122. cout << "合法代表組合總數: " << valid_count << "\n";
  123. }
  124.  
Success #stdin #stdout 0.01s 5324KB
stdin
4 5 14
stdout
0 0 1 1 1 
1 1 1 0 1 
1 0 1 1 0 
1 1 0 1 1 
--------
0 1 0 1 1 
1 1 1 0 1 
1 0 1 1 1 
1 1 0 1 0 
--------
0 1 1 1 0 
0 1 0 1 1 
1 1 1 0 1 
1 0 1 1 1 
--------
0 1 1 1 0 
1 0 0 1 1 
1 0 1 0 1 
1 1 1 1 1 
--------
0 1 1 1 0 
1 0 0 1 1 
1 1 1 0 1 
1 0 1 1 1 
--------
0 1 1 1 0 
1 1 0 0 1 
1 0 1 0 1 
1 1 1 1 1 
--------
0 1 1 1 0 
1 1 0 0 1 
1 0 1 1 1 
1 1 1 0 1 
--------
0 1 1 1 0 
1 1 0 1 0 
1 0 1 1 1 
1 1 1 0 1 
--------
0 1 1 1 0 
1 1 0 1 1 
0 1 1 0 1 
1 0 1 1 1 
--------
0 1 1 1 0 
1 1 0 1 1 
1 0 1 0 1 
1 0 1 1 1 
--------
0 1 1 1 0 
1 1 0 1 1 
1 0 1 0 1 
1 1 1 0 1 
--------
0 1 1 1 0 
1 1 0 1 1 
1 0 1 1 0 
1 1 1 0 1 
--------
0 1 1 1 1 
1 1 0 0 1 
1 0 0 1 1 
1 1 1 1 0 
--------
1 0 0 1 1 
1 1 0 0 1 
1 0 1 1 1 
1 1 1 0 1 
--------
1 0 0 1 1 
1 1 0 1 0 
1 0 1 1 1 
1 1 1 0 1 
--------
1 0 0 1 1 
1 1 1 0 1 
1 0 1 1 1 
1 1 0 0 1 
--------
1 0 1 1 1 
1 1 0 0 1 
0 1 0 1 1 
1 1 1 0 1 
--------
1 0 1 1 1 
1 1 0 0 1 
1 0 0 1 1 
1 1 1 0 1 
--------
1 0 1 1 1 
1 1 0 1 0 
0 1 0 1 1 
1 1 1 0 1 
--------
1 0 1 1 1 
1 1 0 1 0 
1 0 0 1 1 
1 1 1 0 1 
--------
1 0 1 1 1 
1 1 1 0 1 
0 1 0 1 1 
0 1 1 1 0 
--------
1 0 1 1 1 
1 1 1 0 1 
0 1 0 1 1 
1 1 0 0 1 
--------
1 0 1 1 1 
1 1 1 0 1 
1 0 0 1 1 
1 1 0 0 1 
--------
1 1 0 0 1 
0 1 0 1 1 
1 1 1 0 1 
1 0 1 1 1 
--------
1 1 0 0 1 
1 0 0 1 1 
1 1 1 0 1 
1 0 1 1 1 
--------
1 1 0 0 1 
1 0 1 1 1 
1 1 1 0 1 
1 0 0 1 1 
--------
1 1 0 1 1 
0 1 1 0 1 
1 0 1 1 1 
1 1 1 0 0 
--------
1 1 1 0 1 
0 1 0 1 1 
1 1 0 0 1 
1 0 1 1 1 
--------
1 1 1 0 1 
0 1 0 1 1 
1 1 0 1 0 
1 0 1 1 1 
--------
1 1 1 0 1 
1 0 0 1 1 
1 1 0 0 1 
1 0 1 1 1 
--------
1 1 1 0 1 
1 0 0 1 1 
1 1 0 1 0 
1 0 1 1 1 
--------
1 1 1 0 1 
1 0 1 1 1 
1 1 0 0 1 
1 0 0 1 1 
--------
1 1 1 0 1 
1 0 1 1 1 
1 1 0 1 0 
1 0 0 1 1 
--------
1 1 1 1 0 
1 0 0 1 1 
1 1 0 0 1 
0 1 1 1 1 
--------
合法代表組合總數: 34