Ok...
Topic link: https: //zoj.pintia.cn/problem-sets/91827364500/problems/91827364501
This question is to come out is a very simple dfs:
A 4 * 4 map to each point of the sort, as shown below:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
A set point for the k-th point, its coordinates (k / n, k% n), dfs performed according to this, when k == n * n is the exit dfs. If k <n * n, DFS continue, determines whether to set aside, that is, to put this space and the cross-point, things are not vertical, the last note back.
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 5 using namespace std; 6 7 int n, ans; 8 char g[5][5]; 9 10 inline bool canput(int x, int y){ 11 for(int i = x - 1; i >= 0 && g[i][y] != 'X'; i--){ 12 if(g[i][y] == 'O') return 0; 13 } 14 for(int i = y - 1; i >= 0 && g[x][i] != 'X'; i--){ 15 if(g[x][i] == 'O') return 0; 16 } 17 return 1; 18 } 19 20 inline void dfs(int k, int cnt){ 21 int x, y; 22 if(k == n * n){ 23 if(cnt > ans) ans = cnt; 24 return; 25 } 26 else{ 27 x = k / n; 28 y = k % n; 29 if(g[x][y] == '.' && canput(x, y)){ 30 g[x][y] = 'O'; 31 dfs(k + 1, cnt + 1); 32 g[x][y] = '.'; 33 } 34 dfs(k + 1, cnt); 35 } 36 } 37 38 int main(){ 39 while(~scanf("%d", &n) && n){ 40 ans = 0; 41 for(int i = 0; i < n; i++){ 42 for(int j = 0; j < n; j++){ 43 cin >> g[i][j]; 44 } 45 } 46 dfs(0, 0); 47 printf("%d\n", ans); 48 } 49 return 0; 50 }