Question: like Queen N, we do not consider diagonal conflicts, but consider the existence of walls, as long as there is a wall in the middle.
#include <cstdio> const int maxn = 5; char map[maxn][maxn]; int ans, n; bool isok(int x, int y) { for (int i = x + 1; i <= n && map[i][y] != 'X'; i++) if(map[i][y] == '0') return false; for (int i = x - 1; i >= 1 && map[i][y] != 'X'; i--) if(map[i][y] == '0') return false; for (int i = y; i <= n && map[x][i] != 'X'; i++) if (map[x][i] == '0') return false; for (int i = y - 1; i >= 1 && map[x][i] != 'X'; i--) if (map[x][i] == '0') return false; return true; } void dfs(int x, int y, int p) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (map[i][j] == '.' && isok(i, j)) { map[i][j] = '0'; dfs(i, j, p + 1); map[i][j] = '.'; } if (ans < p) ans = p; } int main() { while (scanf("%d", &n) && n) { gets(map[0]); for (int i = 1; i <= n; i++) gets(map[i] + 1); ans = 0; dfs(1, 1, 0); printf("%d\n", ans); } return 0; } #include <cstdio>const int maxn = 5;char map[maxn][maxn];int ans, n;bool isok(int x, int y) {for (int i = x + 1; i <= n && map[i][y] != 'X'; i++)if(map[i][y] == '0')return false;for (int i = x - 1; i >= 1 && map[i][y] != 'X'; i--)if(map[i][y] == '0')return false;for (int i = y; i <= n && map[x][i] != 'X'; i++)if (map[x][i] == '0')return false;for (int i = y - 1; i >= 1 && map[x][i] != 'X'; i--)if (map[x][i] == '0')return false;return true;}void dfs(int x, int y, int p) {for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)if (map[i][j] == '.' && isok(i, j)) {map[i][j] = '0';dfs(i, j, p + 1);map[i][j] = '.';}if (ans < p)ans = p;}int main() {while (scanf("%d", &n) && n) {gets(map[0]);for (int i = 1; i <= n; i++)gets(map[i] + 1);ans = 0;dfs(1, 1, 0);printf("%d\n", ans);}return 0;}
Conflict.
Queen N can only put one row, but this question is not good, so use the full picture to play the game, just go back to dfs, the question is up to 4*4, the range is very small.
When I first thought about putting a piece, I would mark other places that could not be placed, and then try again, later, it was found that if a point is marked as valid when it is marked repeatedly, it is checked instead. Because of the small amount of data, it does not take much time.
After that, you can use the accumulated tag to subtract the tag one by one...
Code: