National Treasures
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 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.
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 <= 212) representing the type of the artifact in that cell.
The last line of the input file has two zeros.
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.
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.
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.
#include<cstdio> #include<queue> #include<iostream> #include<cctype> #include<cstring> #include<string> #include<vector> #include<algorithm> #define pb push_back using namespace std; const int INF=0x3f3f3f3f; const int N=2505; const int d[12][2]={-1,-2,-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,0,0,1,1,0,0,-1}; int R,C,mp[55][55],vis[N],mx[N],my[N],dx[N],dy[N],dis; vector<int>G[N],bit; void fun(int x) { bit.clear(); while(x)bit.pb(x%2),x/=2; int len=bit.size(); for(int i=len;i<12;i++)bit.pb(0); } bool dfs(int u) { for(int len=G[u].size(),i=0;i<len;i++) { int v=G[u][i]; if(!vis[v]&&dy[v]==dx[u]+1) { vis[v]=1; if(my[v]!=-1&&dy[v]==dis)continue; if(my[v]==-1||dfs(my[v])) { my[v]=u; mx[u]=v; return 1; } } } return 0; } bool bfs() { queue<int>Q; dis=INF; memset(dx,-1,sizeof(dx)); memset(dy,-1,sizeof(dy)); for(int i=0;i<R*C;i++) { if(mx[i]==-1) Q.push(i),dx[i]=0; } while(!Q.empty()) { int u=Q.front(); Q.pop(); if(dx[u]>dis)break; for(int i=0,len=G[u].size();i<len;i++) { int v=G[u][i]; if(dy[v]==-1) { dy[v]=dx[u]+1; if(my[v]==-1)dis=dy[v]; else { dx[my[v]]=dy[v]+1; Q.push(my[v]); } } } } return dis!=INF; } int match() { int ans=0; memset(mx,-1,sizeof(mx)); memset(my,-1,sizeof(my)); while(bfs()) { memset(vis,0,sizeof(vis)); for(int i=0;i<R*C;i++) { if(mx[i]==-1&&dfs(i)) ans++; } } return ans; } int main() { int kase=0; while(scanf("%d%d",&R,&C),R|C) { for(int i=0;i<R*C;i++)G[i].clear(); for(int i=0;i<R;i++) for(int j=0;j<C;j++) scanf("%d",&mp[i][j]); for(int i=0;i<R;i++) for(int j=0;j<C;j++) if(mp[i][j]==-1)continue; else { fun(mp[i][j]); for(int k=0;k<12;k++) if(bit[k]==1) { int x=i+d[k][0],y=j+d[k][1]; if(x<0||y<0||x>=R||y>=C||mp[x][y]==-1)continue; int t=(i+j)%2; if(t)G[i*C+j].pb(x*C+y); else G[x*C+y].pb(i*C+j); } } printf("%d. %d ",++kase,match()); } return 0; }