Poj 2446 Chessboard 【二分匹配+经典建图模型】

Chessboard

Time Limit: 2000MS

 

Memory Limit: 65536K

Total Submissions: 16893

 

Accepted: 5273

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

Hint

 
A possible solution for the sample input.

Source

POJ Monthly,charlescpp

 

题目大意:在一个n*m的矩阵上,有k个洞 ,我们有1*2大小的木板,问在当前情况下是否能够通过1*2的木板铺上所有没有洞的格子并且使得没有板子重叠。


思路:


1、经典的二分匹配的模型:

①我们设定a【i】【j】==x,如果x==0,表示i,j这个坐标上的格子不是洞,如果x==1,表示i,j这个格子是洞。

②那么我们建图,枚举每一个不是洞的点,与其四周相邻的格子建边(当然要保证相邻的格子也不能是洞)。

③然后跑二分匹配,如果最大匹配数==n*m-k,那么其解就是YES,否则就是NO


2、注意数组大小,尽量开大一些。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int a[500][500];
int match[50*50*10];
int vis[50*50*10];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int n,m,k;
vector<int >mp[50*50*10];
void getmap()
{
    for(int i=1;i<=n*m;i++)mp[i].clear();



    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]==1)continue;
            else
            {
                for(int k=0;k<4;k++)
                {
                    int x=i+fx[k];
                    int y=j+fy[k];
                    if(x>=1&&x<=n&&y>=1&&y<=m)
                    if(a[x][y]==0)
                    {
                        mp[(i-1)*m+j].push_back((x-1)*m+y);
                    }
                }
            }
        }
    }
}
int find(int u)
{
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(match,-1,sizeof(match));
        memset(a,0,sizeof(a));
        for(int i=0;i<k;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[y][x]=1;
        }
        int output=0;
        getmap();
        for(int i=1;i<=n*m;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i)==1)output++;
        }
        if(output==n*m-k)printf("YES\n");
        else printf("NO\n");
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值