Advertisement
Dmaxiya

五子棋对弈 参考代码

Mar 29th, 2025
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 100 + 100;
  6. int ans;
  7. int board[maxn][maxn];
  8.  
  9. bool judge(int x, int y) {
  10.     if (x != 4 && y != 4) {
  11.         return true;
  12.     }
  13.  
  14.     bool allSame = true;
  15.     if (x == 4) {
  16.         for (int i = 0; i < 5; ++i) {
  17.             if (board[i][y] != board[x][y]) {
  18.                 allSame = false;
  19.                 break;
  20.             }
  21.         }
  22.         if (allSame) {
  23.             return false;
  24.         }
  25.     }
  26.  
  27.     allSame = true;
  28.     if (y == 4) {
  29.         for (int i = 0; i < 5; ++i) {
  30.             if (board[x][i] != board[x][y]) {
  31.                 allSame = false;
  32.                 break;
  33.             }
  34.         }
  35.         if (allSame) {
  36.             return false;
  37.         }
  38.     }
  39.  
  40.     allSame = true;
  41.     if (x == 4 && y == 0) {
  42.         for (int i = 0; i < 5; ++i) {
  43.             if (board[i][4 - i] != board[4][0]) {
  44.                 allSame = false;
  45.                 break;
  46.             }
  47.         }
  48.         if (allSame) {
  49.             return false;
  50.         }
  51.     }
  52.  
  53.     allSame = true;
  54.     if (x == 4 && y == 4) {
  55.         for (int i = 0; i < 5; ++i) {
  56.             if (board[i][i] != board[4][4]) {
  57.                 allSame = false;
  58.                 break;
  59.             }
  60.         }
  61.         if (allSame) {
  62.             return false;
  63.         }
  64.     }
  65.  
  66.     return true;
  67. }
  68.  
  69. void dfs(int depth, int white, int black) {
  70.     if (depth == 25) {
  71.         ++ans;
  72.         return ;
  73.     }
  74.     int x = depth / 5;
  75.     int y = depth % 5;
  76.     if (white > 0) {
  77.         board[x][y] = 0;
  78.         if (judge(x, y)) {
  79.             dfs(depth + 1, white - 1, black);
  80.         }
  81.     }
  82.     if (black > 0) {
  83.         board[x][y] = 1;
  84.         if (judge(x, y)) {
  85.             dfs(depth + 1, white, black - 1);
  86.         }
  87.     }
  88. }
  89.  
  90. int main() {
  91. #ifdef ExRoc
  92.     freopen("test.txt", "r", stdin);
  93. #endif // ExRoc
  94.     ios::sync_with_stdio(false);
  95.  
  96.     dfs(0, 13, 12);
  97.     cout << ans << endl;
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement