黑白染色 最小點覆蓋 奇偶分組 ACM HDU 3360
National Treasures
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1134 Accepted Submission(s): 402
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 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.
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.
黑白染色(即奇偶數)+最小點覆蓋
/*------------------黑白染色+最小點覆蓋---------------------
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;
}
|