Advertisement
Dmaxiya

LITS 游戏 参考代码

Mar 5th, 2025
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 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. vector<vector<string>> G = {
  7.     // L
  8.     {
  9.         "10",
  10.         "10",
  11.         "11"
  12.     },
  13.     {
  14.         "111",
  15.         "100"
  16.     },
  17.     {
  18.         "11",
  19.         "01",
  20.         "01"
  21.     },
  22.     {
  23.         "001",
  24.         "111"
  25.     },
  26.     // I
  27.     {
  28.         "1111"
  29.     },
  30.     {
  31.         "1",
  32.         "1",
  33.         "1",
  34.         "1"
  35.     },
  36.     // T
  37.     {
  38.         "111",
  39.         "010"
  40.     },
  41.     {
  42.         "01",
  43.         "11",
  44.         "01"
  45.     },
  46.     {
  47.         "010",
  48.         "111"
  49.     },
  50.     {
  51.         "10",
  52.         "11",
  53.         "10"
  54.     },
  55.     // S
  56.     {
  57.         "011",
  58.         "110"
  59.     },
  60.     {
  61.         "10",
  62.         "11",
  63.         "01"
  64.     }
  65. };
  66.  
  67. int T, n, nn, x;
  68. bool flag;
  69. int status[12] = {0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3};
  70. char ch[13] = "LLLLIITTTTSS";
  71. char str[maxn][maxn];
  72. int pos[12][4][2];
  73.  
  74. void init() {
  75.     for (int i = 0; i < 12; ++i) {
  76.         int idx = 0;
  77.         for (int j = 0; j < G[i].size(); ++j) {
  78.             for (int k = 0; k < G[i][j].length(); ++k) {
  79.                 if (G[i][j][k] == '1') {
  80.                     pos[i][idx][0] = j;
  81.                     pos[i][idx][1] = k;
  82.                     ++idx;
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. bool in(int x, int y) {
  90.     return x >= 0 && x < n && y >= 0 && y < n;
  91. }
  92.  
  93. bool canPut(int x, int y, int idx) {
  94.     for (int i = 0; i < 4; ++i) {
  95.         int xx = x + pos[idx][i][0];
  96.         int yy = y + pos[idx][i][1];
  97.         if (!in(xx, yy) || str[xx][yy] != '*') {
  98.             return false;
  99.         }
  100.     }
  101.     return true;
  102. }
  103.  
  104. void put(int x, int y, int idx, char ch) {
  105.     for (int i = 0; i < 4; ++i) {
  106.         int xx = x + pos[idx][i][0];
  107.         int yy = y + pos[idx][i][1];
  108.         str[xx][yy] = ch;
  109.     }
  110. }
  111.  
  112. void dfs(int depth, int s) {
  113.     if (depth == nn) {
  114.         if (s == 15) {
  115.             flag = true;
  116.         }
  117.         return ;
  118.     }
  119.     int x = depth / n;
  120.     int y = depth % n;
  121.     for (int i = 0; i < 12; ++i) {
  122.         if ((s & (1 << status[i])) == 0 && canPut(x, y, i)) {
  123.             put(x, y, i, ch[i]);
  124.             dfs(depth + 1, s | (1 << status[i]));
  125.             if (flag) {
  126.                 return ;
  127.             }
  128.             put(x, y, i, '*');
  129.         }
  130.     }
  131.     dfs(depth + 1, s);
  132. }
  133.  
  134. int main() {
  135. #ifdef ExRoc
  136.     freopen("test.txt", "r", stdin);
  137. #endif // ExRoc
  138.     ios::sync_with_stdio(false);
  139.  
  140.     init();
  141.     cin >> T;
  142.     while (T--) {
  143.         cin >> n;
  144.         nn = n * n;
  145.         for (int i = 0; i < n; ++i) {
  146.             for (int j = 0; j < n; ++j) {
  147.                 cin >> x;
  148.                 str[i][j] = (x == 1 ? '*' : '.');
  149.             }
  150.         }
  151.         flag = false;
  152.         dfs(0, 0);
  153.         cout << (flag ? "Yes" : "No") << endl;
  154.     }
  155.  
  156.     return 0;
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement