HDOJ 1507-Uncle Tom's Inherited Land *

Source: Internet
Author: User

Question:

Place a rectangle of 1*2 in a matrix... some points cannot be placed in a rectangle. How many rectangles can be placed at most ....

Question:

My first response to this question is status compression DP .. but check the range. okay .. bipartite Graph Matching .. but there is a problem .. if point-to-point side is directly implemented .. there will be confusion .. and it does not conform to the basic model of the 2-Chart (the points on the same side do not have any direct relationship )... so do the bipartite graph .. the first thing is to divide the points into two heaps that will not be directly affected internally .. in this example, (x + y) is an odd or even number .. the rest is bare ..

Program:

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<queue>#define ll long long#define MAXN 105using namespace std;int n,match[MAXN],hash[MAXN][MAXN],w[MAXN*MAXN][2];bool arc[MAXN][MAXN],used[MAXN],s[MAXN][MAXN]; bool dfs(int x){       int i;       for (i=1;i<=n;i++)          if (arc[x][i] && !used[i])          {                 used[i]=true;                 if (!match[i] || dfs(match[i]))                 {                       match[i]=x;                       return true;                 }          }              return false; }int getmax(){       int sum=0;       memset(match,0,sizeof(match));       for (int i=1;i<=n;i++)       {               memset(used,false,sizeof(used));               sum+=dfs(i);       }       return sum;}int main(){       int i,j,x,y,m,num,cases=0;         while (~scanf("%d%d",&n,&m) && n)       {                 memset(s,true,sizeof(s));                scanf("%d",&num);                while (num--) { scanf("%d%d",&x,&y),s[x][y]=false; };                num=0;                memset(hash,0,sizeof(hash));                for (x=1;x<=n;x++)                  for (y=1;y<=m;y++)                     if (s[x][y]) hash[x][y]=++num,w[num][0]=x,w[num][1]=y;                memset(arc,false,sizeof(arc));                for (x=1;x<=n;x++)                   for (y=1;y<=m;y++)                      if ((x+y)%2 && s[x][y])                      {                             if (x!=1 && s[x-1][y]) arc[hash[x][y]][hash[x-1][y]]=true;                             if (y!=1 && s[x][y-1]) arc[hash[x][y]][hash[x][y-1]]=true;                             if (x!=n && s[x+1][y]) arc[hash[x][y]][hash[x+1][y]]=true;                             if (y!=m && s[x][y+1]) arc[hash[x][y]][hash[x][y+1]]=true;                      }                n=num;                if (cases) printf("\n");                cases++;                printf("%d\n",getmax());                for (i=1;i<=n;i++)                  if (match[i])                      printf("(%d,%d)--(%d,%d)\n",w[match[i]][0],w[match[i]][1],w[i][0],w[i][1]);       }       return 0;}

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.