National Treasures
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1213 Accepted Submission(s): 434
Problem 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.
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.
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.
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. 2HintThe picture below shows the solution of the second test case where the two artifacts in the middle are replaced by guards.
题意:在一个n*m的矩阵里面有一些珠宝,每个珠宝有一个对应的价值,这个价值对应的二进制数上某一个为如果为1,代表着上面图一对应的数字需要一个护卫
有时候护卫全部放满也不能保护所有的珠宝,所以我们可以移除一些珠宝来放护卫,现在问你最少需要移除多少珠宝可以保护剩下的护卫
思路:仔细想想的话会发现是一个最小顶点覆盖的题型,我们把所有护卫的位置跟珠宝连线,最后就是要求所有的边都覆盖到的顶点个数(并且这个顶点要是珠宝,如果是-1就不需要移除珠宝了)。这里构造二分图可以用黑白染色法,如果珠宝在黑点,护卫一定在白点,反之亦然。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 2505
int dir[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 ma[51][51];
int line[N],vis[N];
int n,m;
struct Edge
{
int v,next;
} edge[N*12];
int cnt,head[N];
void init()
{
memset(line,-1,sizeof(line));
memset(head,-1,sizeof(head));
cnt=0;
}
void addedge(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void build()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(ma[i][j]==-1) continue;
int t=(i+j)%2;
for(int k=0; k<12; k++)
{
if(!(ma[i][j]&(1<<k))) continue;
int x=i+dir[k][0],y=j+dir[k][1];
if(x<1||x>n||y<1||y>m||ma[x][y]==-1) continue;
if(t) addedge((i-1)*m+j,(x-1)*m+y);
else addedge((x-1)*m+y,(i-1)*m+j);
}
}
}
}
int can(int t)
{
for(int i=head[t]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
vis[v]=1;
if(line[v]==-1||can(line[v]))
{
line[v]=t;
return 1;
}
}
}
return 0;
}
int Maxmatch()
{
int ans=0;
for(int i=1; i<=n*m; i++)
{
memset(vis,0,sizeof(vis));
if(can(i)) ans++;
}
return ans;
}
int main()
{
int tot=1;
while(~scanf("%d %d",&n,&m)&&(n+m))
{
init();
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&ma[i][j]);
build();
int ans=Maxmatch();
printf("%d. %d\n",tot++,ans);
}
return 0;
}