• PKU 2446 Chessboard


    题目

        

    Chessboard
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 20511   Accepted: 6427

    Description

    Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of cards with size 1 * 2 to cover the board. However, she thinks it too easy to bob, so she makes some holes on the board (as shown in the figure below). 

    We call a grid, which doesn’t contain a hole, a normal grid. Bob has to follow the rules below: 
    1. Any normal grid should be covered with exactly one card. 
    2. One card should cover exactly 2 normal adjacent grids. 

    Some examples are given in the figures below: 
     
    A VALID solution.

     
    An invalid solution, because the hole of red color is covered with a card.

     
    An invalid solution, because there exists a grid, which is not covered.

    Your task is to help Bob to decide whether or not the chessboard can be covered according to the rules above.

    Input

    There are 3 integers in the first line: m, n, k (0 < m, n <= 32, 0 <= K < m * n), the number of rows, column and holes. In the next k lines, there is a pair of integers (x, y) in each line, which represents a hole in the y-th row, the x-th column.

    Output

    If the board can be covered, output "YES". Otherwise, output "NO".

    Sample Input

    4 3 2
    2 1
    3 3
    

    Sample Output

    YES

    大意

        

    一个棋盘内,有些地方有洞,有些地方没有,没有洞的地方可以放东西,求是否可以用1*2的长方形填满所有格子(除了洞)。当然长方形不能相互覆盖。

    分析

    显然,每一块矩形相当于把相邻的两个点匹配起来。 根据前面题目的启发,应该可以相当实用黑白染色,点的编号类似。然后依然是黑点连白点。 如果相邻的点不是洞,那么就连一条边。显然这是一个二分图。 然后跑这个二分图最大匹配。假设有k对匹配对,洞的数目为c,说明有2*k个点匹配成功。 如果满足k*2+c=n*m,那么有解。 连边后如图,其中1-4,3-6,5-8,7-10,11-12即为一组大小为5的最大匹配。

      

    代码 

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<vector>
     5 using namespace std;
     6 int n,m,x,y,ans,link[4005],s[2050][2050],ss;
     7 vector<int> f[4005];
     8 bool cover[4005],a[2050][2050];
     9 int fx[4][2]={{1,0},{0,1},{0,-1},{-1,0}};//4个方向
    10 bool find(int i)
    11 {
    12      for (int k=0;k<f[i].size();k++)
    13        if (!cover[f[i][k]])
    14        {
    15            int j=f[i][k];
    16           cover[j]=true;
    17              int q=link[j];
    18             link[j]=i;
    19            if (q==0||find(q)) return true;
    20            link[j]=q;
    21         }
    22     return false;
    23 }
    24 int main()
    25  {
    26      cin>>n>>m>>ss;
    27     for(int i=1;i<=n;i++)
    28      for(int j=1;j<=m;j++)
    29       s[i][j]=m*(i-1)+j;
    30     for (int i=1;i<=ss;i++)
    31      {
    32          cin>>x>>y;
    33        a[y][x]=1;  //attention!!!!!
    34     }
    35      for (int i=1;i<=n;i++)
    36      {
    37          for (int j=1;j<=m;j++)
    38          {
    39              if (!a[i][j])
    40              {
    41                  int ax,ay;
    42                    for(int kk=0;kk<4;kk++)//4个方向
    43                    {
    44                        ax=i+fx[kk][0],ay=j+fx[kk][1];
    45                        if(ax<1||ax>n||ay<1||ay>m) continue;//判断是否越界
    46                        if(a[ax][ay]) continue;//判断该可攻击的点是否被移除
    47                        f[s[i][j]].push_back(s[ax][ay]);//连边
    48                    }
    49              }
    50                 
    51          }
    52      }
    53      int ans=0;
    54      for (int i=1;i<=n*m;i++)
    55              {
    56                  memset(cover,0,sizeof(cover));
    57                  ans+=find(i);
    58              }
    59      if (2*ans+ss==n*m) cout<<"YES";
    60      else cout<<"NO";     
    61  }
    为何要逼自己长大,去闯不该闯的荒唐
  • 相关阅读:
    你认为做好测试计划工作的关键是什么?
    App压力测试MonkeyRunner整理
    小米2S手机
    APP耗电量专项测试整理
    App功能测试
    渗透测试_利用Burp爆破用户名与密码
    docker介绍
    redis-cluster
    Redis配置文件详情
    Redis-Sentinel哨兵
  • 原文地址:https://www.cnblogs.com/zjzjzj/p/10160300.html
Copyright © 2020-2023  润新知