EOJ:National Treasures

National Treasures

Time Limit: 6000MS Memory Limit: 65536K
Total Submits: 35 Accepted: 11

Description

The 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 ancient artifacts. The museum would like to hire the minimum number of additional guards so that the great hall is secured. 

The great hall is represented as a two dimensional 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 above 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.

 

Input

Your 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 < 2^12) representing the type of the artifact in that cell. 

The last line of the input file has two zeros.

 

Output

For 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 Input

1 3
512 -1 2048
2 3
512 2560 2048
512 2560 2048
0 0

 

Sample Output

1. 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. 
_____________________________________________________________________________________
黑白染色之后发现宝藏和它的守卫分别在黑色和白色的格子上。
建立二分图,黑色格子上的为X,白色格子上的为Y,然后连边。
如果两个点之间有一条边的话,那么这两个宝藏不能共存。
于是我们只要找到一个集合,使得集合里的所有点都没有边相连,那么集合里的元素个数就是最大的宝藏数。
这个集合即二分图的最大独立集,元素个数即最大独立数。
最大独立数=顶点数-最大匹配数
而由于我们要求的是守卫数,根据题目意思,格子里要么是守卫,要么是宝藏,所以守卫数就等于最大匹配数。
1 #include<stdio.h>
2 #include<memory.h>
3 #include<queue>
4  using namespace std;
5  const int tx[12] =
6 { -1, -2, -2, -1, 1, 2, 2, 1, -1, 0, 1, 0 };
7  const int ty[12] =
8 { -2, -1, 1, 2, 2, 1, -1, -2, 0, 1, 0, -1 };
9  int i, j, k, r, c, ans, t1, t2, x, y, lab1, lab2, runs;
10  int map[60][60], id[60][60];
11
12 const int MAXN = 3001;
13 const int INF = 1 << 28;
14 int g[MAXN][MAXN], Mx[MAXN], My[MAXN], Nx, Ny;
15 int dx[MAXN], dy[MAXN], dis;
16 bool vst[MAXN];
17 bool searchP(void)
18 {
19 queue<int> Q;
20 dis = INF;
21 memset(dx, -1, sizeof(dx));
22 memset(dy, -1, sizeof(dy));
23 for (int i = 0; i < Nx; i++)
24 if (Mx[i] == -1)
25 {
26 Q.push(i);
27 dx[i] = 0;
28 }
29 while (!Q.empty())
30 {
31 int u = Q.front();
32 Q.pop();
33 if (dx[u] > dis)
34 break;
35 for (int v = 0; v < Ny; v++)
36 if (g[u][v] && dy[v] == -1)
37 {
38 dy[v] = dx[u] + 1;
39 if (My[v] == -1)
40 dis = dy[v];
41 else
42 {
43 dx[My[v]] = dy[v] + 1;
44 Q.push(My[v]);
45 }
46 }
47 }
48 return dis != INF;
49 }
50 bool DFS(int u)
51 {
52 for (int v = 0; v < Ny; v++)
53 if (!vst[v] && g[u][v] && dy[v] == dx[u] + 1)
54 {
55 vst[v] = 1;
56 if (My[v] != -1 && dy[v] == dis)
57 continue;
58 if (My[v] == -1 || DFS(My[v]))
59 {
60 My[v] = u;
61 Mx[u] = v;
62 return 1;
63 }
64 }
65 return 0;
66 }
67 int MaxMatch(void)
68 {
69 int res = 0;
70 memset(Mx, -1, sizeof(Mx));
71 memset(My, -1, sizeof(My));
72 while (searchP())
73 {
74 memset(vst, 0, sizeof(vst));
75 for (int i = 0; i < Nx; i++)
76 if (Mx[i] == -1 && DFS(i))
77 res++;
78 }
79 return res;
80 }
81 int main()
82 {
83 runs = 0;
84 while (1)
85 {
86 runs++;
87 memset(g, 0, sizeof(g));
88 scanf("%d%d", &r, &c);
89 if (r == 0)
90 break;
91 Nx = -1;
92 Ny = -1;
93 for (i = 1; i <= r; i++)
94 for (j = 1; j <= c; j++)
95 {
96 scanf("%d", &map[i][j]);
97 if (map[i][j] != -1)
98 if ((i + j) % 2 == 0)
99 {
100 Nx++;
101 id[i][j] = Nx;
102 }
103 else
104 {
105 Ny++;
106 id[i][j] = Ny;
107 }
108 }
109 for (i = 1; i <= r; i++)
110 for (j = 1; j <= c; j++)
111 if (map[i][j] != -1)
112 for (k = 0; k < 12; k++)
113 if (((1 << k) & map[i][j]) == (1 << k))
114 {
115 x = i + tx[k];
116 y = j + ty[k];
117 lab1 = id[i][j];
118 lab2 = id[x][y];
119 if (x > 0 && x <= r && y > 0 && y <= c && map[x][y]
120 != -1)
121 if ((i + j) % 2 == 0)
122 g[lab1][lab2] = true;
123 else
124 g[lab2][lab1] = true;
125 }
126 Nx++;
127 Ny++;
128 ans = MaxMatch();
129 printf("%d. %d\n", runs, ans);
130 }
131 return 0;
132 }
posted on 2011-03-03 20:29  风也轻云也淡  阅读(216)  评论(0编辑  收藏  举报