HDU 3360 National Treasures 黑白染色+最小点覆盖 ACM Steps 6.3.8

来源:互联网 发布:淘宝上网 编辑:程序博客网 时间:2024/05/16 07:31


黑白染色 最小点覆盖 奇偶分组 ACM HDU 3360


学习参考链接http://blog.csdn.net/u011686226/article/details/35671981

National Treasures

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1134    Accepted Submission(s): 402


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.
 

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.
 

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.

题意:
         
对于每个位置a[i][j],如果a[i][j] != -1 则转为二进制,最多12位,代表题目那张图的12个位置 。如果对应位是1 说明在那里放一个守卫可以看住a[i][j]位置上的这个东西

黑白染色(即奇偶数)+最小点覆盖


/*------------------黑白染色+最小点覆盖---------------------
HDU  3360 National Treasures  奇偶染色+最小点覆盖 ACM Steps 6.3.8
-----------------------------*/
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 51;
int vis[maxn*maxn];
int father[maxn*maxn];
vector <int> graph[maxn*maxn];//define graph
int n, m;
int map[maxn][maxn];
//define the guard status
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 };

bool dfs(int head){
 int i,now;
 int size = graph[head].size();
 for (i = 0; i < size; i++){
  now = graph[head][i]; //获得head位置中的某适合的guard
  if (!vis[now]){ //
   vis[now] = true;
   if (father[now] == -1 || dfs(father[now])){ //寻找增广路径是否行
    father[now] = head;
    return true;
   }
  }
 }
 return false;
}

int find(){
 int ans = 0, i = 0, j = 0;
 memset(father, -1, sizeof(father));
 for (; i < n; i++){
  for (j = 0; j < m; j++){
   if ((i + j) % 2){         //奇偶分组
    memset(vis, 0, sizeof(vis));
    if (dfs(m*i + j))
     ans++;
   }
  }
 }
 return ans;
}


int main()
{
 int cas = 1,i,j;
 while (scanf("%d %d", &n, &m) != EOF && n&& m){
  for ( i = 0; i < n; i++)
   for (j = 0; j < m; j++)
   scanf("%d", &map[i][j]);
 
  for (i = 0; i < n*m; i++)
   graph[i].clear();

  for (i = 0; i < n; i++)
  {
   for ( j = 0; j < m; j++)
   {
    int x = map[i][j];
    if (x == -1)
     continue;
    for (int k = 0; k < 12; k++, x >>= 1)
    {
     if (!x)//如果x==0就跳出
      break;
     if (!(x & 1))//判断能否站守卫 5&1==true,4&1==false
      continue;

     int xx = i + dir[k][0];
     int yy = j + dir[k][1];
     if (xx < 0 || xx >= n || yy < 0 || yy >= m)
      continue;
     if (map[xx][yy] == -1)
      continue;
     if ((i + j) % 2)
      graph[i*m + j].push_back(xx*m + yy);
     else
      graph[xx*m + yy].push_back(i*m + j);
    }
   }
  }
  printf("%d. %d\n", cas++, find());
 }
 return 0;
}





0 0
原创粉丝点击