UVA-10615 Rooks (binary image matching)

Source: Internet
Author: User

The main topic: In a nxn box, some locations have cars, to give each car color, so that the same row and the same column of any two car color, to find a need for a minimum number of color coloring scheme.

Title Analysis: The minimum number of colors required is obviously possible, assuming that the minimum number of colors is K. If the position (I,J) is a car, then even a side i->j, get a two-dimensional graph, the K-match can be constructed out of the solution.

AC Code:

# include<iostream># include<cstdio># include<vector># include<cstring># include< Algorithm>using namespace std;# define REP (i,s,n) for (int i=s;i<n;++i) # define CL (b) memset (A,b,sizeof (a)) # Define CLL (A,b,n) Fill (a,a+n,b) const int N=105;int Inr[n],inc[n],vis[n],link[n],ans[n][n],n;char p[n][n];vector<        Int>g[n];bool dfs (int x) {REP (I,0,g[x].size ()) {int y=g[x][i];        if (Vis[y]) continue;        Vis[y]=1; if (link[y]==-1| |            DFS (Link[y])) {link[y]=x;        return true; }} return false;}    void Match () {CL (link,-1);        REP (i,0,n) {CL (vis,0);    DFS (i);    }}int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        REP (I,0,n) g[i].clear ();        REP (i,0,n) scanf ("%s", P[i]);        CL (inr,0);        CL (inc,0);            Rep (I,0,n) Rep (j,0,n) if (p[i][j]== ' * ') {++inr[i];            ++INC[J];        G[i].push_back (j); } int maxn=0;       REP (i,0,n) Maxn=max (Maxn,max (Inr[i],inc[i])); REP (I,0,n) if (INR[I]&LT;MAXN) {for (int. j=0;j<n&&inr[i]<maxn;++j) {while (inr[i]<                    MAXN&AMP;&AMP;INC[J]&LT;MAXN) {++inr[i];                    ++INC[J];                G[i].push_back (j);        }}} CL (ans,0);            REP (k,1,maxn+1) {match ();                REP (i,0,n) {int r=link[i];                if (p[r][i]== ' * ') ans[r][i]=k;                    REP (J,0,g[r].size ()) if (g[r][j]==i) {g[r].erase (G[r].begin () +j);                Break        }}} printf ("%d\n", MAXN); Rep (I,0,n) Rep (j,0,n) printf ("%d%c", Ans[i][j], (j==n-1)? '    \ n ': '); } return 0;}

  

UVA-10615 Rooks (binary image matching)

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.