"POJ 3020" antenna Placement

Source: Internet
Author: User

"POJ 3020" antenna Placement

The maximal independent set problem of binary graphs
' O ' means that the break point requires that all * connections be connected to one or two consecutive lines per route *
The maximum match can satisfy only the longest path that can be formed by only two consecutive connections, and the points that are not connected need to be circled separately.
i.e. N (total * number)-m (maximum match) +m (maximum match)/2
(due to the fact that the maximum match that is obtained by a bidirectional path actually represents the total number of points connected after 22 connections)

The code is as follows:

#include <cstdlib> #include <cstdio> #include <cstring> #include <iostream> using namespace std;
Char map[41][13];
BOOL mp[555][555],vis[555];
int tp,n,link[555];
int dirx[] = {-1, 0, 1, 0};
int diry[] = {0,-1, 0, 1};

int dir[4];
    BOOL can (int p) {int i,j;
        for (i = 0; i < 4; ++i) {if (P + Dir[i] < 0 && p + dir[i] >= N) continue;
        j = p + dir[i];
            if (!vis[j] && mp[p][j]) {vis[j] = true;
                if (link[j] = = 1 | | can (LINK[J])) {link[j] = p;
            return 1;
}}} return 0;
    } int main () {int h,t,w,i,j,k,xx,yy,cnt,sum;
    scanf ("%d", &t);
        while (t--) {memset (Mp,false,sizeof (MP));
        TP = 0;
        scanf ("%d%d", &h,&w);
        for (i = 1; I <= h; ++i) {scanf ("%s", map[i]+1);
        } n = h*w;
        Dir[0] =-W;
       DIR[1] =-1; DIR[2] = W;
        DIR[3] = 1;
        sum = n; for (i = 1, i <= h; ++i) {for (j = 1; J <= W; ++j,++tp) {if (map[i][j
                    ] = = ' O ') {sum--;
                Continue
                    } for (k = 0; k < 4; ++k) {xx = i + dirx[k];
                    yy = j + Diry[k]; if (TP + dir[k] >= 0 && TP + dir[k] < n) {if (map[xx][yy] = = ' * '
                     ) Mp[tp][tp+dir[k]] = true;
        }}}} memset (Link,-1,sizeof (link));
        CNT = 0;
            for (i = 0; i < n; ++i) {memset (vis,0,sizeof (VIS));
        if (Can (i)) cnt++;
    } printf ("%d\n", SUM-CNT+CNT/2);
} 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.