Question: In the square of N * n, put a few firearms, they will attack the points in the same column, and ask how many can be put.
Analysis: Graph Theory, search, and bipartite graph matching. This question can be solved by searching. Here we use bipartite graph matching.
Create a graph and regard different consecutive intervals in each column of the source image as the points XI and YJ in a new graph respectively;
The edge <Xi, YJ> indicates the point at the corresponding position in the source image. The points that can attack each other in the source image correspond to the same XI and YJ in the new image;
The maximum matching of the Bipartite Graph in the new graph is the maximum number of places in the original graph that cannot attack each other.
Note: The Key to graph theory is graph creation.
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;char maps[5][5];int g[17][17];int line[5][5],row[5][5];int used[17],link[17];int dfs(int s, int n){for (int i = 0 ; i <= n ; ++ i) if (g[s][i] && !used[i]) { used[i] = 1; if (link[i] == -1 || dfs(link[i], n)) { link[i] = s; return 1;} } return 0;}int main(){int n,x,y;while (~scanf("%d",&n) && n) {for (int i = 0 ; i < n ; ++ i)scanf("%s",maps[i]);memset(line, -1, sizeof(line));memset(row, -1, sizeof(row));int line_count = -1,row_count = -1;for (int i = 0 ; i < n ; ++ i)for (int j = 0 ; j < n ; ++ j) {if (j == 0 || maps[i][j] != '.')line_count ++;if (maps[i][j] == '.')line[i][j] = line_count;if (j == 0 || maps[j][i] != '.')row_count ++;if (maps[j][i] == '.')row[j][i] = row_count;}memset(g, 0, sizeof(g));for (int i = 0 ; i < n ; ++ i)for (int j = 0 ; j < n ; ++ j)if (maps[i][j] == '.')g[line[i][j]][row[i][j]] = 1;int sum = 0; memset(link, -1 ,sizeof(link)); for(int i = 0 ; i <= line_count ; ++ i) { memset(used, 0, sizeof(used)); sum += dfs(i, row_count);} printf("%d\n",sum);}return 0;}
Ultraviolet A 639-don't get rooked