[Problem description] There was once upon a time a kingdom Whose castle was a rectangle divided into m×n squares. Some squares are walls, while others are open spaces. The king of this kingdom has set some traps in the castle, and each trap occupies a blank space. One day, the king decided to set up guards in the castle, and he hoped to arrange as many guards as possible. The guards are strictly trained, so once they find someone in the same row or in the same column, they immediately shot at the person. Therefore, the King hoped to reasonably assign guards so that they could not see each other, so that they could not shoot each other. Guards can only be placed on open spaces, not traps or walls, and only one guard can be placed on one open space. If two guards are in the same row or column and there is no wall between them, they can see each other. (Guard is like a car in a chess game) Your task is to write a program, calculate the maximum number of guards you can assign Based on the given castle, and design the layout scheme. [Input] The two integers m and n (1 ≤ m, n ≤ 200) in the first line indicate the scale of the castle. Next, the integer in the N column in the m row describes the terrain of the castle. The number of column J in row I is represented by AI and J. AI, j = 0, indicating the square [I, j] is a blank space; AI, j = 1, indicating the square [I, j] is a trap; AI, j = 2, indicates that the square [I, j] is a wall. Output: an integer k In the first line, indicating that up to k guards can be configured. Next K rows, each line has two integers XI and Yi, describing the location of a guard. [Example] Guards. in3 42 0 0 02 2 2 10 1 0 2guards. out21 23 3 example data (black square is the wall, white square is the space, circle is the trap, G indicates the guard)
This is the maximum binary matching.
First, modeling is performed for the left half of the binary regression;
The right half of the second half will be created separately;
If the traffic between a region and a region is open space, a region is provided.
Then find the maximum match, and then extract all the matches.
Accode:
# Include <cstdio> # include <cstring> # include <cstdlib> # include <bitset> Using STD: bitset; const char fi [] = "Guards. in "; const char fo [] =" Guards. out "; const int maxn = 40010; const int maxr = 210; const int max = 0x3fffff00; const int min =-Max; struct edge {int DEST; edge * Next ;}; edge * edge [maxn]; bitset <maxn> marked; int HH [maxr] [maxr], LX [maxr] [maxr]; int map [maxr] [maxr]; int link [maxn]; int X [maxn], Y [Maxn]; int n, m, n, m, ans; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W ", stdout);} inline void insert (int u, int v) {edge * P = new edge; P-> DEST = V; P-> next = edge [u]; edge [u] = P;} void readdata () {scanf ("% d", & N, & M); For (INT I = 1; I <n + 1; ++ I) for (Int J = 1; j <m + 1; ++ J) scanf ("% d ", & map [I] [J]); For (INT I = 1; I <n + 1; ++ I) for (Int J = 1; j <m + 1; ++ J) I F (Map [I] [J]! = 2) {If (! HH [I] [J]) {x [++ N] = I; // record the seat mark of the specified region. HH [I] [J] = N; // records the primary keys of the records (I, j. For (int K = J + 1; k <m + 1; ++ K) {If (Map [I] [k] = 2) break; HH [I] [k] = N;} // find the region corresponding to this position to the right. For (int K = J-1; k; -- k) {If (Map [I] [k] = 2) break; HH [I] [k] = N;} // find the region corresponding to this position on the left.} If (! Lx [I] [J]) {Y [++ m] = J; // record the seat mark of the specified region. Lx [I] [J] = m; // The memory used by memory (I, j. For (int K = I + 1; k <n + 1; ++ K) {If (Map [k] [J] = 2) break; lx [k] [J] = m;} // locate the region that is located in the same position. For (int K = I-1; k; -- k) {If (Map [k] [J] = 2) break; lx [k] [J] = m;} // locate the region that is located at this position.} If (! Map [I] [J]) insert (HH [I] [J], LX [I] [J]); // (I, j) make sure that the space is open.} Bool find (int u) {for (edge * P = edge [u]; P = p-> next) {int v = p-> DEST; if (! Marked. Test (V) {marked. Set (V); If (! Link [v] | find (link [v]) {link [v] = u; return true ;}} return false;} void work () {for (INT I = 1; I <n + 1; ++ I) {marked. reset (); If (find (I) + + ans;} printf ("% d \ n", ANS); For (Int J = 1; j <m + 1; ++ J) if (link [J]) printf ("% d \ n", X [LINK [J], Y [J]);} int main () {init_file (); readdata (); Work (); exit (0 );}