ZOJ-1654 place the robots split row split column diagram + binary matching or maximum independent point set + TLE

Source: Internet
Author: User

Given a square, each point in the grid is composed of an open space, lawn, and a wall. A robot can be placed in each blank space, and each robot can scan the laser in four directions, therefore, a solution should be given to place enough robots on the board. The laser can pass through the lawn but cannot pass through the wall.

Solution: in fact, this is a classic binary match diagram. The practice is to split rows and columns, that is, if a row contains a wall, when the front and back of the wall are treated as different rows and columns are processed in the same way, each blank space needs to obtain a new row and column attribute, traverse the entire graph to allocate a row and column number to each blank space. The allocation should be as compact as possible. For any blank space, one row and one column are required (meaning that the row and the column cannot accommodate other spaces). For each blank space, divide the corresponding row number and column number into two parts, forming a bipartite graph. The edge in the bipartite graph means that a row matches a column, that is, a group of edges corresponds to a group of coordinates, and a blank space can be placed as a pawn, the maximum match is the maximum number of pawns that can be placed when each row matches each column at most once.

Serial numbers are as follows:

 

The array of row and column numbers accidentally uses the char array, and the segment fails to crash ......

The Code is as follows:

View code

# Include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> using namespace STD; const int maxn = 60; int n, m, rnum, cnum; char MP [maxn] [maxn]; int rn [maxn] [maxn], CN [maxn] [maxn]; // RN is used to record the row number after a point is split, similarly, CN is used to record a column number void getr () {// This function can allocate the row number memset (RN, 0xff, sizeof (RN); rnum = 1; for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <m; ++ J) {If (MP [I] [J] = 'O') {rn [I] [J] = r Num; while (++ j <M & MP [I] [J]! = '#') {If (MP [I] [J] = 'O') Rn [I] [J] = rnum ;} // process a row that is located in the same row and starts with an open space as a row, where some lawns have connectivity -- J, ++ rnum ;}}} void GETC () {// This function can assign a column number memset (CN, 0xff, sizeof (CN); cnum = 1; for (Int J = 0; j <m; ++ J) {for (INT I = 0; I <n; ++ I) {If (MP [I] [J] = 'O ') {CN [I] [J] = cnum; while (++ I <n & MP [I] [J]! = '#') {If (MP [I] [J] = 'O') Cn [I] [J] = cnum;} -- I, + + cnum ;}}} char G [maxn * maxn] [maxn * maxn], vis [maxn * maxn]; int match [maxn * maxn]; void build () {getr (); GETC (); memset (G, 0, sizeof (g); For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <m; ++ J) {If (MP [I] [J] = 'O ') {G [Rn [I] [J] [CN [I] [J] = 1; // one empty space occupies one row and one column }}} bool path (int u) {for (INT I = 1; I <cnum; ++ I) {If (! G [u] [I] | Vis [I]) continue; vis [I] = 1; if (! Match [I] | path (Match [I]) {match [I] = u; return true;} return false;} int query () {int ret = 0; memset (match, 0, sizeof (MATCH); For (INT I = 1; I <rnum; ++ I) {memset (VIS, 0, sizeof (VIS); If (path (I) {++ RET ;}} return ret ;} /* 5 5O *** ##### * oo # Oo *** # O ** O4 */INT main () {int T, CA = 0; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); For (INT I = 0; I <n; ++ I) {scanf ("% s", MP [I]);} printf ("case: % d \ n ", ++ ca, (build (), query ();} return 0 ;}

 

 

Paste the largest TLE code:

View code

# Include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> using namespace STD; int n, m, Nd [2505], idx, G [2505] [2505]; char MP [55] [55]; bool check (int A, int B) {int X1 = Nd [a]/n, y1 = Nd [a] % N; int X2 = Nd [B]/n, y2 = Nd [B] % N; If (x1! = X2 & Y1! = Y2) return true; If (x1 = x2) {// if it is in the same row for (INT I = Y1 + 1; I <Y2; ++ I) {If (MP [X1] [I] = '#') {return true ;}} else if (Y1 = Y2) {for (INT I = X1 + 1; I <X2; ++ I) {If (MP [I] [Y1] = '#') {return true ;}}return false;} void build () {memset (G, 0, sizeof (g); For (INT I = 0; I <idx; ++ I) {for (Int J = I + 1; j <idx; ++ J) {G [I] [J] = G [J] [I] = check (I, j) ;}} int ret, CNT [2505], St [2505]; void DFS (int x, int num) {for (INT I = x + 1; I <idx; ++ I) {If (! G [x] [I]) continue; If (CNT [I] + num <= RET) return; int flag = true; For (Int J = 0; j <num; ++ J) {If (! G [I] [st [J]) {flag = false; break ;}} if (FLAG) {st [num] = I; DFS (I, num + 1) ;}}if (Num> RET) {ret = num;} int query () {ret = 0; For (INT I = idx-1; i> = 0; -- I) {st [0] = I; DFS (I, 1); CNT [I] = ret;} return ret;} int main () {int T, CA = 0; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); idx = 0; For (INT I = 0; I <n; ++ I) {scanf ("% s", MP [I]); for (Int J = 0; j <m; ++ J) {If (MP [I] [J] = 'O ') {Nd [idx ++] = I * n + J ;}} build (); printf ("case: % d \ n", ++ ca, query ();} 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.