fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // 2 quân hậu chiếu nhau nếu:
  5. // + chung hàng: x
  6. // + chung cột: x
  7. // + chung đường chéo
  8.  
  9. // (x1, y1) và (x2, y2) chung đường chéo nếu?
  10.  
  11. // |x1 - y1| = |x2 - y2|
  12.  
  13. // => x1 - y1 = x2 - y2 hoặc x1 + y1 = x2 + y2
  14. string a[8];
  15. int pos[8]; // pos[i] = Vị trí (toạ độ cột) của quân hậu được đặt ở hàng thứ i
  16.  
  17. int main() {
  18. ios::sync_with_stdio(0); cin.tie(0);
  19. for (int i = 0; i < 8; i++) cin >> a[i];
  20.  
  21. for (int i = 0; i < 8; i++) pos[i] = i;
  22.  
  23. int ans = 0;
  24. do {
  25. bool good = true;
  26. for (int i = 0; i < 8; i++) {
  27. if (a[i][pos[i]] == '*') good = false;
  28. }
  29.  
  30. map<int, bool> mp_sum, mp_sub;
  31. for (int i = 0; i < 8; i++) {
  32. int sum = i + pos[i];
  33. int sub = i - pos[i];
  34. if (mp_sum.count(sum) || mp_sub.count(sub)) good = false;
  35. mp_sum[sum] = mp_sub[sub] = 1;
  36. }
  37.  
  38. ans += good;
  39.  
  40. } while (next_permutation(pos, pos + 8));
  41.  
  42. cout << ans << '\n';
  43. }
  44.  
Success #stdin #stdout 0.06s 5292KB
stdin
........
........
..*.....
........
........
.....**.
...*....
........
stdout
65