The key to this question is the distribution of your location. You can see that it is staggered, so you can use a bipartite graph.
National Treasures
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 854 accepted submission (s): 281
Problem descriptionthe great hall of the National Museum has been robbed few times recently. everyone is now worried about the security of the treasures on display. to help secure the hall, the museum contracted with a private security company to provide additional guards to stay in the Great Hall and keep an eye on the specified ent artifacts. the museum wocould like to hire the minimum number of additional guards so that the great hall is secured.
The Great Hall is represented as a two dimen1_grid of R × c cells. some cells are already occupied with the museum's guards. all remaining cells are occupied by artifacts of different types (statues, sculptures ,... etc .) which can be replaced by new hired guards. for each artifact, few other cells in the hall are identified as critical points of the artifact depending on the artifact value, type of vault it is kept inside, and few other factors. in other words, if this artifact is going to stay in the hall then all of its critical points must have guards standing on them. A guard standing in a critical position of Multiple artifacts can keep an eye on them all. A guard, however,
Can not stand in a cell which contains an artifact (Instead, you may remove the artifact to allow the guard to stay there ). also you can not remove an artifact and leave the space free (you can only replace an artifact with a new hired Guard ).
Surveying all the artifacts in the great hall you figured out that the critical points of any artifact (marked by a) are always a subset of the 12 neighboring cells as shown in the grid below.
Accordingly, the type of an artifact can be specified as a non-negative integer where the I-th bit is 1 only if critical point number I from the picture abve is a critical point of that artifact. for example an artifact of Type 595 (in binary 1001010011) can be pictured as shown in the figure below. note that bits are numbered from right to left (the right-most bit is bit number 1 .) if a critical point of an artifact lies outside the Hall grid then it is considered secure.
You are given the layout of the Great Hall and are asked to find the minimum number of additional guards to hire such that all remaining artifacts are secured.
Inputyour program will be tested on one or more test cases. Each test case is specified using R + 1 lines.
The first line specifies two integers (1 <= r, C <= 50) which are the dimensions of the museum hall. the next R lines contain c integers separated by one or more spaces. the J-th integer of the I-th row is-1 if cell (I, j) already contains one of the museum's guards, otherwise it contains an INTEGER (0 <= T <= 212) representing the type of the artifact in that cell.
The last line of the input file has two zeros.
Outputfor each test case, print the following line:
K. G
Where k is the test case number (starting at one,) and G is the minimum number of additional guards to hire such that all remaining artifacts are secured.
Sample input1 3
512-1 2048
2 3
512 2560 2048
512 2560 2048
0 0
Sample output1. 0
2. 2
Hint
The picture below shows the solution of the second test case where the two artifacts in the middle are replaced by guards.
Source2009 anarc
Recommendlcy
# Include <stdio. h> # Include < String . H> # Include <Iostream> Using Namespace STD; # Define N 2505 # Define M 2500*50 Struct Node { Int To, next;} edge [m]; Int Map [ 5 ] [ 5 ] = {{0 , 2 , 0 , 3 , 0 },{ 1 , 0 , 9 , 0 , 4 },{ 0 , 12 , 0 , 10 ,0 },{ 8 , 0 , 11 , 0 , 5 },{ 0 , 7 , 0 , 6 , 0 }}; Int N, m; Int Save [55 ] [ 55 ]; Int G [ 55 ] [ 55 ], G1 [ 55 ] [ 55 ]; Int CNT, pre [N], FRT [N], Mark [N]; Int Ver [N], ver1 [N]; Int Vcnt, vcnt1; Void Init () {vcnt = 0 ; Vcnt1 = 0 ; CNT = 0 ; Memset (PRE, - 1 , Sizeof (Pre )); Int TMP = 0 ; For ( Int I = 1 ; I <= m; I ++ ) {Save [ 1 ] [I] = TMP; TMP ^ = 1 ;} For ( Int I = 2 ; I <= N; I ++ ){ For ( Int J = 1 ; J <= m; j ++ ) Save [I] [J] = Save [I-1 ] [J] ^ 1 ;}} Void Add_edge ( Int U, Int V) {edge [CNT]. = V; edge [CNT]. Next = Pre [u]; Pre [u] = CNT ++ ;} Void Link ( Int X, Int Y, Int W ){ Int TMP, id = 0 ; Int TX, Ty; Int Flag; While (W) {TMP = W & 1 ; W = W> 1 ; ID ++ ; If (TMP = 0 ) Continue ; Flag = 0 ; For ( Int I = 0 ; I < 5 ; I ++ ){ For ( Int J = 0 ; J <5 ; J ++ ){ If (Map [I] [J] = ID) {TX = X + I- 2 ; Ty = Y + J- 2 ; Flag = 1 ; Break ;} If (FLAG) Break ;} If (FLAG) Break ;} If (TX> = 1 & TX <= N) & (TY> = 1 & Ty <= M )){ If (G [TX] [ty] =- 1 ) Continue ; Add_edge (G1 [x] [Y], G1 [TX] [ty]); add_edge (G1 [TX] [ty], g1 [x] [Y]) ;}} Int DFS ( Int S ){ Int V; For ( Int P = pre [s]; P! =- 1 ; P = Edge [p]. Next) {v = Edge [p].; If (MARK [v] = 1 ) Continue ; Mark [v] =1 ; If (FRT [v] =- 1 | DFS (FRT [v]) = 1 ) {FRT [v] = S; Return 1 ;}} Return 0 ;} Int Main (){ Int Tt = 1 ; While (Scanf ( " % D " , & N, & M) & (n + M) {Init (); For ( Int I = 1 ; I <= N; I ++ ) For ( Int J = 1 ; J <= m; j ++) {Scanf ( " % D " ,& G [I] [J]);} Int Id = 0 ; For ( Int I = 1 ; I <= N; I ++ ) For ( Int J =1 ; J <= m; j ++ ){ If (G [I] [J] =- 1 ) Continue ; ID ++ ; G1 [I] [J] = ID; If (Save [I] [J] = 0 ) {Ver [ ++ Vcnt] = ID ;} Else {Ver1 [ ++ Vcnt1] = ID ;}} For ( Int I = 1 ; I <= N; I ++ ){ For ( Int J = 1 ; J <= m; j ++ ){ If (G [I] [J] =- 1 ) Continue ; Link (I, j, G [I] [J]);} Int Sum = 0 ; Memset (FRT, - 1 , Sizeof (FRT )); For ( Int I = 1 ; I <= vcnt; I ++ ) {Memset (mark, 0 ,Sizeof (Mark); sum + = DFS (ver [I]);} printf ( " % D. " , TT ++ ); Printf ( " % D \ n " , Sum );} Return 0 ;}