A - National Treasures
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status
FreeJourney (2011-08-03) System Crawler (2013-05-30)
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 3512 -1 20482 3512 2560 2048512 2560 20480 0
Sample Output
1. 02. 2
Hint
The picture below shows the solution of the second test case where the two artifacts in the middle are replaced by guards.
思路:KM,
#include<cstdio>#include<iostream>#include<cstring>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))#define ll(x) (1<<x)using namespace std;const int mm=55*55;const int dx[]={-1,-2,-2,-1,1,2,2,1,-1,0,1,0};const int dy[]={-2,-1,1,2,2,1,-1,-2,0,1,0,-1};class Edge{ public:int v,next;}e[mm*55];int head[mm],edge;int X[mm],g[mm][mm];bool S[mm],T[mm];void init(){ clr(head,-1);edge=0;}void add(int u,int v){ e[edge].v=v;e[edge].next=head[u];head[u]=edge++;}bool dfs(int u){ int v; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(T[v])continue; T[v]=1; if(X[v]==-1||dfs(X[v])) { X[v]=u;return 1; } } return 0;}int R,C;///加双向边,结果除2void getEdge(int u,int v,int w){ int tx,ty; for(int i=0;i<12;++i) if(ll(i)&w) { tx=u+dx[i];ty=v+dy[i]; if(tx<1||tx>R||ty<1||ty>C||g[tx][ty]==-1)continue; add((u-1)*C+v,(tx-1)*C+ty); add((tx-1)*C+ty,(u-1)*C+v); }}void getans(){ clr(X,-1); int ret=0; FOR(i,1,R*C) { clr(T,0); if(dfs(i)) ++ret; } printf("%d\n",ret/2);}int main(){ int a,cas=0; while(~scanf("%d%d",&R,&C)) { init(); if(R==0&&C==0)break; FOR(i,1,R)FOR(j,1,C) { scanf("%d",&g[i][j]); } FOR(i,1,R)FOR(j,1,C) if(g[i][j]!=-1) getEdge(i,j,g[i][j]); printf("%d. ",++cas); getans(); }}