zoukankan      html  css  js  c++  java
  • ACWing P372 棋盘覆盖 题解

    Analysis

    这是一个经典的二分图问题,我们将图进行奇偶染色,注意边界条件的判断。再跑一遍匈牙利算法就行了,跟上一题很像。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define maxn 110
     6 using namespace std;
     7 inline int read() 
     8 {
     9     int x=0;
    10     bool f=1;
    11     char c=getchar();
    12     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
    13     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
    14     if(f) return x;
    15     return 0-x;
    16 }
    17 inline void write(int x)
    18 {
    19     if(x<0){putchar('-');x=-x;}
    20     if(x>9)write(x/10);
    21     putchar(x%10+'0');
    22 }
    23 struct node
    24 {
    25     int to,nex;
    26 }edge[2*(maxn*maxn+10)];
    27 int n,m,t,cnt,ans;
    28 int head[2*(maxn*maxn+10)],match[2*(maxn*maxn+10)];
    29 bool map[maxn][maxn],book[2*(maxn*maxn+10)];
    30 int dir1[6]={0,1,-1,0,0},dir2[6]={0,0,0,1,-1};
    31 inline void add(int x,int y)
    32 {
    33     cnt++;
    34     edge[cnt].to=y;
    35     edge[cnt].nex=head[x];
    36     head[x]=cnt;
    37 }
    38 inline bool dfs(int u)
    39 {
    40     for(int i=head[u];i;i=edge[i].nex)
    41     {
    42         int v=edge[i].to;
    43         if(!book[v])
    44         {
    45             book[v]=1;
    46             if(!match[v]||dfs(match[v]))
    47             {
    48                 match[v]=u;
    49                 return true;
    50             }
    51         }
    52     }
    53     return false;
    54 }
    55 inline int calculation(int x,int y){return x*(n+2)+y;}
    56 int main()
    57 {
    58     n=read();t=read();
    59     for(int i=1;i<=t;i++)
    60     {
    61         int x,y;
    62         x=read();y=read();
    63         map[x][y]=true;
    64     }
    65     for(int i=1;i<=n;i++)
    66         for(int j=1;j<=n;j++)
    67             if(!map[i][j])
    68             {
    69                 for(int k=1;k<=4;k++)
    70                 {
    71                     int x=i+dir1[k],y=j+dir2[k];
    72                     if(x>0&&y>0&&x<=n&&y<=n&&!map[x][y]&&(x+y)%2==1)
    73                     {
    74                         add(calculation(i,j),calculation(x,y));
    75                     }
    76                 }
    77             }
    78     for(int i=1;i<=calculation(n,n);i++)
    79     {
    80         memset(book,0,sizeof(book));
    81         if(dfs(i))ans++;
    82     }
    83     write(ans);
    84     return 0;
    85 }
    请各位大佬斧正(反正我不认识斧正是什么意思)
  • 相关阅读:
    Rocket broker启动失败?
    jar下载慢,maven配置国内仓库
    Linux下搭建RocketMQ环境
    几种常见css布局
    原生JS快速实现拖放
    更换 grub 主题
    安装node环境以及cnpm
    用 hexo 快速搭建博客
    关于8080端口被占用问题的解决办法。
    idea的几个常用+实用的快捷键!
  • 原文地址:https://www.cnblogs.com/handsome-zyc/p/11243609.html
Copyright © 2011-2022 走看看