POJ antenna Placement (minimum side coverage)

Source: Internet
Author: User

Test instructions: There are some points in a matrix that cover these points with a small matrix of 1*2, and the minimum number of small matrices required;

Reference: http://blog.csdn.net/lyy289065406/article/details/6647040

Idea: Non-graph minimum edge coverage = number of vertices-maximum number of matches/2;

Each of the points to be matched is represented by a unique number, which is equivalent to discretization and easy to build;

Each point to be matched is built with an adjacent point;

Since the map is the matrix as a binary graph, a point is split into two points, so the maximum number of matches to be divided by two;

Because unmatched points also need to be overwritten, the result is the number of vertices-the maximum number of matches/2;

#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;intn,m,t,cnt;Chartemp;intmm[550][550];intvis[5050],link[5005];intmark[505][550];intdir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};intDfsintx) {     for(intI=1; i<=cnt;i++){       if(!vis[i]&&Mark[x][i]) {Vis[i]=1; if(link[i]==-1||DFS (Link[i])) {Link[i]=x; return 1; }       }    }    return 0;}intHungary () {intsum=0; memset (Link,-1,sizeof(link));  for(intI=1; i<=cnt;i++) {memset (Vis,0,sizeof(VIS)); if(Dfs (i)) sum++; }    returnsum;}intMain () {intans,i,j,k;  while(SCANF ("%d", &t)! =EOF) {         while(t--) {CNT=0; memset (MM,0,sizeof(mm)); memset (Mark,0,sizeof(Mark)); scanf ("%d%d",&n,&m);  for(i=0; i<n;i++)            {                 for(j=0; j<m;j++) {scanf ("%c",&temp); if(temp=='*') {Mm[i][j]=++cnt;//each point is represented by a number                    }                }            }             for(i=0; i<n;i++)            {                 for(j=0; j<m;j++)                {                    if(Mm[i][j]) for(k=0;k<4; k++){                       intxx=i+dir[k][0]; intyy=j+dir[k][1]; if(mm[xx][yy]&&xx>=0&&yy>=0&AMP;&AMP;XX&LT;N&AMP;&AMP;YY&LT;M) {//adjacent build edgesmark[mm[xx][yy]][mm[i][j]]=1; MARK[MM[I][J]][MM[XX][YY]]=1; } }}} printf ("%d\n", Cnt-hungary ()/2); }    }    return 0;}

POJ antenna Placement (minimum side coverage)

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.