Ultraviolet A 639-don't get rooked

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.