码迷,mamicode.com
首页 > 其他好文 > 详细

[TJOI2013]攻击装置

时间:2019-01-10 20:53:55      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:int   strong   ios   efi   algorithm   ret   code   putchar   lin   

Description
给定一个01矩阵,其中你可以在0的位置放置攻击装置。每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y-1),(x+1,y-2),(x+2,y-1),(x-1,y+2),(x-2,y+1), (x+1,y+2),(x+2,y+1)
求在装置互不攻击的情况下,最多可以放置多少个装置。

Input
第一行一个整数N,表示矩阵大小为N*N。接下来N行每一行一个长度N的01串,表示矩阵。

Output
一个整数,表示在装置互不攻击的情况下最多可以放置多少个装置。

Sample Input
3
010
000
100

Sample Output
4

HINT
100%数据 N<=200

首先对棋盘黑白染色,然后对于每个点,向其能攻击到的点连边,然后求出最大独立点集即可

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1; char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1; char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=2e2;
const int dx[8]={-2,-2,-1,-1,1,1,2,2};
const int dy[8]={-1,1,-2,2,-2,2,-1,1};
int pre[(sqr(N)<<2)+10],now[sqr(N)+10],child[(sqr(N)<<2)+10];
int path[sqr(N)+10],vis[sqr(N)+10];
int n,Time,tot;
char map[N+10][N+10];
bool in_map(int x,int y){return x>0&&x<=n&&y>0&&y<=n;}
int G(int x,int y){return (x-1)*n+y;}
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
bool Extra(int x){
    for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
        if (vis[son]==Time) continue;
        vis[son]=Time;
        if (!~path[son]||Extra(path[son])){
            path[son]=x;
            return 1;
        }
    }
    return 0;
}
int main(){
    memset(path,255,sizeof(path));
    n=read();
    for (int i=1;i<=n;i++)  scanf("%s",map[i]+1);
    int All=0,Ans=0;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            All+=map[i][j]=='0';
            if ((i+j)&1){
                if (map[i][j]=='1') continue;
                for (int k=0;k<8;k++){
                    int tx=i+dx[k],ty=j+dy[k];
                    if (!in_map(tx,ty)||map[tx][ty]=='1')   continue;
                    join(G(i,j),G(tx,ty));
                }
            }
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++){
            if ((i+j)&1){
                ++Time;
                if (Extra(G(i,j)))  Ans++;
            }
        }
    }
    printf("%d\n",All-Ans);
    return 0;
}

[TJOI2013]攻击装置

标签:int   strong   ios   efi   algorithm   ret   code   putchar   lin   

原文地址:https://www.cnblogs.com/Wolfycz/p/10252254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!