Question link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 654
AC one hundred questions, not as comfortable as AC!
Question: An N * m map, * represents the lawn, # represents the wall, O represents the open space, and the O in the figure is placed in the robot. The robot can attack (up and down) in four directions, the attack range is infinite, and robots cannot attack each other. Robots cannot be placed on lawns, and attacks on robots can pass through lawns. However, attacks on robots cannot pass through the walls, for example, the "* o # O" line can contain two robots, and the "O * oo" line can only contain one. How many robots can be put at most?
I just learned the bipartite graph. I have made a few questions about the bipartite graph and don't need to practice it. After reading the question, I know that it is the largest independent set of the Bipartite Graph, however, the difficulty is how to create a graph.
I did not have any idea about this question I read last night. I thought about it again after I went back to the dormitory. I thought about how to create a diagram. Today I typed it and Wa ran it n times. I checked the problem, there is no error in creating a graph. I just missed a point that only horizontal parts are used as the X set, and the remaining points are the y set by default...
Ideas: Uses the idea of block to create a graph. What is block? The block attack scope can be achieved by one piece. "O * oo" is the block, and it is also one piece. "* o # O" is the two pieces, in layman's terms, this line of a map is separated by a wall and the continuous area with open space is called a "Block". Of course, according to the question, only one robot can be placed in one block, this is also true for vertical parts. blocks are numbered. All numbers of horizontal parts are X sets, and all numbers of vertical parts are y sets, of course, if the same "O" point is numbered twice, you can just create an edge.
Result accepted ID |
1654 Language |
C ++ time |
60 memery |
26720 |
Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> # include <algorithm> # include <math. h> # define Init (a) memset (A, 0, sizeof (A) # define PI ACOs (-1, 0) using namespace STD; const int maxn = 55; const int maxm = 2600; # define lson left, M, id <1 # define rson m + 1, right, id <1 | 1 # define min (A, B) (A> B )? B: A # define max (A, B) (A> B )? A: bint Mar [maxn] [maxn], Mac [maxn] [maxn]; int G [maxm] [maxm]; int line [maxm]; char map [maxn] [maxn]; bool vis [maxm]; int X, Y; int DFS (int u) {for (INT v = 1; v <= y; V ++) {If (G [u] [v] = 1 &&! Vis [v]) {vis [v] = 1; if (line [v] =-1 | DFS (line [v]) {line [v] = u; return 1 ;}} return 0 ;}int K_m () {memset (line,-1, sizeof (line )); int ans = 0; For (INT I = 1; I <= x; I ++) {Init (VIS); ans + = DFS (I);} return ans ;} void creat_g (int n, int m) {for (INT I = 0; I <n; I ++) // horizontally split into X sets {for (Int J = 0; j <m; j ++) {If (Map [I] [J] = 'O') {x ++; while (j <M & map [I] [J]! = '#') {Mar [I] [J] = x; // the same open space or lawn number is J ++ ;}}}} for (INT I = 0; I <m; I ++) // use vertical blocks as y sets {for (Int J = 0; j <n; j ++) {If (Map [J] [I] = 'O') {y ++; while (j <n & map [J] [I]! = '#') {Mac [J] [I] = y; j ++ ;}}} int main () {int t, n, m; scanf ("% d", & T); For (INT case = 1; Case <= T; Case ++) {Init (g); Init (MAR ); init (MAC); X = 0, y = 0; scanf ("% d", & N, & M); For (INT I = 0; I <N; I ++) scanf ("% s", map [I]); creat_g (n, m); // block for (INT I = 0; I <N; I ++) // create a graph {for (Int J = 0; j <m; j ++) {If (Map [I] [J] = 'O ') {G [Mar [I] [J] [Mac [I] [J] = 1; // connection between the building block and the block }}int ans = K_m (); printf ("case: % d \ n", Case, ANS );} return 0 ;}