fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int d = 0;
  8.  
  9. bool cheo1[30], cheo2[30], matrix[30][30];
  10.  
  11. void reset() {
  12. for (int i = 0; i < 30; i++) {
  13. cheo1[i] = cheo2[i] = true;
  14. for (int j = 0; j < 30; j++)
  15. matrix[i][j] = true;
  16. }
  17. }
  18. void solve(int n, int k, int i) {
  19. if (i >= k) {
  20. d++;
  21. return;
  22. }
  23. for (int x = 0; x < n; x++)
  24. for (int y = 0; y < n; y++)
  25. if (matrix[x][y] && cheo1[x + y] && cheo2[x - y + n]) {
  26. matrix[x][y] = cheo1[x + y] = cheo2[x - y + n] = false;
  27. solve(n, k, i + 1);
  28. matrix[x][y] = cheo1[x + y] = cheo2[x - y + n] = true;
  29. }
  30. }
  31. int main() {
  32. int t, n, k;
  33. cin>> t;
  34. for (int i = 0; i < t; i++) {
  35. cin>> n>> k;
  36. d = 0;
  37. reset();
  38. solve(n, k, 0);
  39.  
  40. cout<< "Ban co "<< n<< " x "<< n<< " co "<< d<< " cach dat "<< n<< " quan tuong."<< endl;
  41. }
  42. }
Success #stdin #stdout 0.11s 5284KB
stdin
5
4 2
5 3
6 5
2 1
5 5
stdout
Ban co 4 x 4 co 184 cach dat 4 quan tuong.
Ban co 5 x 5 co 6744 cach dat 5 quan tuong.
Ban co 6 x 6 co 4761600 cach dat 6 quan tuong.
Ban co 2 x 2 co 4 cach dat 2 quan tuong.
Ban co 5 x 5 co 404160 cach dat 5 quan tuong.