HDU1507 Uncle Tom's inherited land* binary map matching Hungarian algorithm black and white dyeing

Source: Internet
Author: User

Original link http://www.cnblogs.com/zhouzhendong/p/8254062.html

Topic Portal-HDU1507Test Instructions Summary

There is a n*m chess board, some of the points are obsolete.

Now let you use the 1*2 rectangle to cover all the non-scrap points, and do not overlap, ask the maximum number of 1*2 can be covered by the rectangle, the output scheme, there is SPJ.

Input Description:

Multiple sets of data, first two n,m per group (if n and M are 0, end program)

and give the K

Then we give the coordinates of the waste point by K two-tuple (x, y).

Solving

The algorithm that follows the first two posts is out of order because the scheme is wrong.

So we're going to dye it in black and white.

Connect the four-connected directions only from (x, y) (X+y is odd), and then match the binary graph.

Where the match array value is the matching object.

Code
#include <cstring> #include <algorithm> #include <cstdlib> #include <cmath> #include <cstdio >using namespace Std;const int n=105,k=55;int dx[4]={0, 0,-1, 1};int dy[4]={-1, 1, 0, 0};int n,m,k,cnt,pl[n][n],tn[n][ N],bj[k],x[k],y[k];int g[k][k],vis[k],match[k];bool Check (int x,int y) {return 1<=x&&x<=n&&1 <=y&&y<=m&&tn[x][y];} BOOL Match (int x) {for (int i=1;i<=cnt;i++) if (!vis[i]&&g[x][i]) {vis[i]=1;if (!match[i]| | Match (Match[i])) {Match[i]=x;return 1;}} return 0;} int Hungary () {int res=0;memset (match,0,sizeof match), for (int i=1;i<=cnt;i++) {if (!bj[i]) Continue;memset (vis,0, sizeof Vis); if (Match (i)) res++;} return res;} int main () {while (~scanf ("%d%d", &n,&m) && (n| | m) {scanf ("%d", &k), memset (pl,0,sizeof PL); for (int i=1,x,y;i<=k;i++) scanf ("%d%d", &x,&y), pl[x][y]=1 ; memset (tn,0,sizeof tn); cnt=0;for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (!pl[i][j]) {bj[tn[i][j]=++cnt]= (i +J) &1;x[Cnt]=i,y[cnt]=j;} memset (g,0,sizeof g); for (int. i=1;i<=n;i++) for (int. j=1;j<=m;j++) if ((i+j) &1) for (int d=0;d<4;d++) {int a=i +dx[d],b=j+dy[d];if (check (b)) g[tn[i][j]][tn[a][b]]=1;} int ans=hungary ();p rintf ("%d\n", ans), for (int i=1;i<=cnt;i++) if (Match[i]) printf ("(%d,%d)-(%d,%d) \ n", X[i],y[i] , X[match[i]],y[match[i]]);} return 0;}

  

HDU1507 Uncle Tom's inherited land* binary map matching Hungarian algorithm black and white dyeing

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.