贴下我写的第二题的代码,ac了。把二维坐标映射成一维坐标应该还可以继续优化一下。 ```cpp #include <iostream> (720)#include <algorithm> using namespace std; const int MAXN = 100; int n, m, c; int C[MAXN]; int A[MAXN][MAXN]; bool judge(int row, int col, int c) { if (row > 0) { if (A[row - 1][col] == c) return false; } if (col > 0) { if (A[row][col - 1] == c) return false; } return true; } int flag; void dfs(int row, int col) { if (flag != 0) return; if (row == n && col == 0) { flag = 1; return; } for (int t = 0; t < c; t++) { if (C[t] == 0) continue; if (!judge(row, col, t)) continue; A[row][col] = t; C[t]--; if (col + 1 < m) { dfs(row, col + 1); } else { dfs(row + 1, 0); } C[t]++; A[row][col] = -1; } } int main() { int T; cin >> T; while (T--) { flag = 0; fill(A[0], A[0] + MAXN * MAXN, -1); cin >> n >> m >> c; for (int i = 0; i < c; i++) { cin >> C[i]; } dfs(0, 0); cout << (flag == 1 ? "YES" : "NO") << endl; } return 0; } ```
点赞 评论

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务