Square destroyer
Time limit:10 seconds Memory limit:32768 KB
The left figure below shows a complete 3*3 grid made with 2 * (3*4) (= 24) matchsticks. the lengths of all matchsticks are one. you can find squares of different sizes in the grid. the size of a square is the length of its side. in the grid shown in
Left figure, there are 9 squares of size one, 4 squares of size two, and 1 square of size three.
Each matchstick of the complete grid is identified with a unique number which is assigned from left to right and from top to bottom as shown in the left figure. if you take some matchsticks out from the complete grid, then some squares in the grid will be destroyed,
Which results in an incomplete 3*3 grid. the right figure has strates an incomplete 3*3 grid after removing three matchsticks numbered with 12, 17 and 23. this removal destroys 5 squares of size one, 3 squares of size two, and 1 square of size three. consequently,
The incomplete grid does not have squares of size three, but still has 4 squares of size one and 1 square of size two.
As input, you are given a (complete or incomplete) N * n grid made with no more than 2n (n + 1) matchsticks for a natural number n <= 5. your task is to compute the minimum number of matchsticks taken out to destroy all the squares existing in the input N * n grid.
Input
The input consists of T test cases. the number of test cases (t) is given in the first line of the input. each test case consists of two lines: the first line contains a natural number N, not greater than 5, which implies you are given a (complete or incomplete)
N * n grid as input, and the second line begins with a nonnegative integer k, the number of matchsticks that are missing from the complete N * n grid, followed by K numbers specifying the matchsticks. note that if K is equal to zero, then the input grid is
Complete N * n grid; otherwise, the input grid is an incomplete N * n grid such that the specified K matchsticks are missing from the complete N * n grid.
Output
Print exactly one line for each test case. The line shoshould contain the minimum number of matchsticks that have to be taken out to destroy all the squares in the input grid.
Sample Input
2
2
0
3
3 12 17 23
Output for the sample input
3
3
Source:Asia 2001, Taejon (South Korea)
Question: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 31
Poj exactly the same question: http://poj.org/problem? Id = 1084
Analysis: you only need to use existing squares as columns and matchsticks as rows. The key is creating a graph, which is quite tangled ~~~
Code:
# Include <cstdio> # include <cstring> # define mm 111111 # define Mn 1111int U [mm], d [mm], L [mm], R [mm], c [mm]; int H [Mn], s [Mn], G [Mn] [Mn], Q [Mn]; bool V [Mn], X [Mn], E [Mn] [Mn]; int n, m, size, ANS, R; void prepare (int r, int c) {for (INT I = 0; I <= C; ++ I) {s [I] = 0; U [I] = d [I] = I; L [I + 1] = I; R [I] = I + 1;} R [c] = 0; while (r) H [R --] =-1;} void remove (INT C) {for (INT I = d [c]; I! = C; I = d [I]) L [R [I] = L [I], R [L [I] = R [I];} void resume (INT c) {for (INT I = U [c]; I! = C; I = U [I]) L [R [I] = R [L [I] = I;} int F () {int I, j, c, ret = 0; For (C = R [0]; C = R [c]) V [c] = 1; for (C = R [0]; c; C = R [c]) if (V [c]) for (V [c] = 0, ++ ret, I = d [c]; I! = C; I = d [I]) for (j = R [I]; J! = I; j = R [J]) V [C [J] = 0; return ret;} void Dance (int K) {If (K + f ()> = ans) return; If (! R [0]) {ans = K; return;} int I, j, C, TMP = mm; for (I = R [0]; I; I = R [I]) if (s [I] <TMP) TMP = s [c = I]; for (I = d [c]; I! = C; I = d [I]) {remove (I); For (j = R [I]; J! = I; j = R [J]) Remove (j); Dance (k + 1); For (j = L [I]; J! = I; j = L [J]) Resume (j); resume (I) ;}} void Link (INT R, int C) {++ s [C [++ size] = C]; d [size] = d [c]; U [d [c] = size; U [size] = C; d [c] = size; If (H [R] <0) H [R] = L [size] = R [size] = size; else {R [size] = R [H [R]; L [R [H [R] = size; L [size] = H [R]; R [H [R] = size ;}} bool OK (INT X1, int Y1, int X2, int Y2) {int I, j, ID; r = 0; for (I = x1; I <= x2; ++ I) {If (X [ID = G [I <1] [(Y1 <1) -1]) Q [R ++] = ID; else return 0; If (X [ID = G [I <1] [(Y2 <1) + 1]) Q [R ++] = ID; else return 0 ;}for (j = Y1; j <= Y2; ++ J) {If (X [ID = G [(X1 <1)-1] [j <1]) Q [R ++] = ID; else return 0; if (X [ID = G [(X2 <1) + 1] [j <1]) Q [R ++] = ID; else return 0 ;} return 1 ;}int main () {int I, J, K, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N); M = N * (n + 1) * 2; for (I = 0; I <= m; ++ I) X [I] = 1; for (k = 0, I = 1; I <= n + 1; ++ I) {for (j = 1; j <= N; ++ J) g [I * 2-1] [J * 2] = ++ K; For (j = 1; j <= n + 1; ++ J) G [I * 2] [J * 2-1] = ++ K;} scanf ("% d", & K); While (k --) scanf ("% d", & I), X [I] = 0; memset (E, 0, sizeof (e); For (size = k = 0; k <n; ++ K) for (I = 1; I + k <= N; ++ I) for (j = 1; j + k <= N; ++ J) if (OK (I, j, I + k, J + k) {++ size; while (r --) E [Q [R] [size] = 1;} prepare (M, size); for (I = 1; I <= m; ++ I) for (j = 1; j <= size; ++ J) if (E [I] [J]) Link (I, j); ans = m; dance (0); printf ("% d \ n", ANS);} return 0 ;}