HDU 3360 National Treasures

National Treasures

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 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.

代码参考自:http://www.cnblogs.com/jackge/archive/2013/03/16/2963207.html

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 2550;
const int M = 110;
int lx[N];
bool vis[N];

int n,m,mp[M][M];
vector<int> g[N];

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 u )
{
    for(int i = 0 ;i < g[u].size(); ++i ){
        int v = g[u][i] ;
        if(!vis[v] ){
            vis[v] = 1;
            if( lx[v] == -1 || dfs( lx[v] ) ){
                lx[v] = u ;
                return true;
            }
        }
    }
    return false;
}
int solve()
{
    int res=0 ;
    memset( lx , -1 ,sizeof lx );
    for( int i = 0 ; i < m*n ;++i){
        memset( vis , false , sizeof vis );
        if( dfs( i ) ) res ++;
    }
    return res;
}

int main()
{
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif
    int cas =1 ;

    while(~scanf("%d%d",&n,&m))
    {
        if( !n && !m )break;

        for( int i = 0 ; i < N ; ++i )
            g[i].clear();

        memset( mp, -1 ,sizeof mp);

        for( int i = 0 ;i < n ;++ i){
            for( int j = 0 ;j < m ;++j ){
                scanf("%d",&mp[i][j]);
            }
        }
        for(int i = 0; i< n ;++i ){
            for(int j = 0; j < m ;++j ){
                if( mp[i][j] == -1 ) continue;
                for( int k = 0 ;k < 12 ; ++k){
                    if( mp[i][j] & (1<<k) ){
                        int x = i + dir[k][0] , y = j + dir[k][1];
                        if( x >= 0 && y >= 0 && x < n && y < m && mp[x][y] != -1){
                            g[ x* m +y ].push_back( i*m + j );
                            g[ i* m +j ].push_back( x*m + y );
                        }
                    }
                }
            }
        }
        printf("%d. %d\n",cas++,solve()/2);
    }
    return  0;
}
时间: 2024-11-06 03:41:08

HDU 3360 National Treasures的相关文章

HDU 3360 National Treasures 奇偶匹配最小点覆盖

题目来源:HDU 3360 National Treasures 题意:如果a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 如果对应位是1 说明在那里放一个守卫可以看住a[i][j]位置上的这个东西 思路:明显死最小点覆盖 奇偶匹配建图 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 55; in

HDU 3360 National Treasures 奇偶匹配的最低点覆盖

标题来源:pid=3360">HDU 3360 National Treasures 意甲冠军:假设a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 假设相应位是1 说明在那里放一个守卫能够看住a[i][j]位置上的这个东西 思路:明显死最小点覆盖 奇偶匹配建图 #include <cstdio> #include <cstring> #include <vector> using namespace std; con

Hdu 3687 National Day Parade(暴力)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3687 思路:暴力,枚举每列(矩形开始的列),将同一行的人按列从小到大排序,则每个人的移动距离为abs(g[j][k]-i-k)(第j行的第k个人移动到合适位置距离),枚举累加取最最小值即可. #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include&

hdu 3360 最小点覆盖

题意:给你一个图,图中有宝物和保安两种元素.每个宝物需要周围的某些位置同时安放保安(如果那些位置有宝物,可以把宝物替换成保安)问你最少需要再安置多少保安,可以使所有宝物满足要求. 题意有点难懂 链接:点我 直接建无向图,少判断个奇偶性,最后除个2即可 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath>

HDU 3687 National Day Parade (暴力)

题意:给定 n 个人,在 n 列,问你移动最少的距离,使得他们形成一个n*n的矩阵. 析:这个题本来是要找中位数的,但是有特殊情况,所以改成暴力了,时间也很短,就是从第一个能够放左角的位置开始找,取最大值,挺简单暴力. 我一个同学竟然读对了题,WA了,然后又重新读题,把题意读错了,就是AC了.... 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #i

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基