poj 2446 Chessboard

原创 2015年11月19日 21:50:46

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.

Key To Problem

题目大意:给定一个nm的方格,问是否可以用一些12的方块在有一些格点无法覆盖的情况下将剩余格点全部覆盖。
题解:将这个方格想象成类似国际象棋棋盘的东西,则每个黑格子与之相邻的必定是一个白格子,这样,就可以构造出一个二分图,如果方格中两个点相邻且没有坏点,就可以将这两个格子连边,最后求方格的最大匹配就是最多可以放上的方块数量。

Code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 35
using namespace std;
struct ss
{
    int next,to;
};
ss Edge[N*N*2];
int n,m,k,tot;
bool used[N*N];
bool map[N][N];
bool ll[N*N];
int head[N*N];
int f[N*N];

void clear()
{
    tot=0;
    memset(head,0,sizeof(head));
    memset(map,false,sizeof(map));
    memset(ll,0,sizeof(ll));
}

void add(int x,int y)
{
    Edge[++tot].next=head[x];
    Edge[tot].to=y;
    head[x]=tot;
}

bool dfs(int u)
{
    for(int i=head[u];i;i=Edge[i].next)
    {
        int to=Edge[i].to;
        if(!used[to])
        {
            used[to]=true;
            if(f[to]==-1||dfs(f[to]))
            {
                f[to]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int cnt=0;
    memset(f,-1,sizeof(f));
    for(int i=1;i<=n*m;i++)
    {
        if(ll[i])continue;
        memset(used,0,sizeof(used));
        if(dfs(i))cnt++;
    }
    return cnt;
}

int main()
{
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        clear();
        for(int i=1;i<=k;i++)
        {
            int x,y;
            scanf("%d%d",&y,&x);
            map[x][y]=true;
        }
        int pp=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                ll[++pp]=map[i][j];
                if(map[i][j])continue;
                if(i-1>0&&!map[i-1][j])
                {
                    int x=(i-2)*m+j;
                    if((i+j)&1)add(pp,x);
                    else add(x,pp);
                }
                if(j-1>0&&!map[i][j-1])
                {
                    int x=(i-1)*m+j-1;
                    if((i+j)&1)add(pp,x);
                    else add(x,pp);
                }
            }
        }
        if(hungary()*2==n*m-k)puts("YES");
        else puts("NO");
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

Chessboard_poj2446_匹配

Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M *...
  • jpwang8
  • jpwang8
  • 2016年06月21日 16:15
  • 217

POJ_2446_Chessboard(二分图匹配)

题意:给出一个矩形N*M棋盘,有K个格子是空洞,然后用2*1的矩形,对所有非空洞的格子进行覆盖,问能否完全覆盖。...

POJ2446 CHESSBOARD

Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15832 Accepted: 4911Descriptio...

POJ 2446 Chessboard

链接:http://poj.org/problem?id=2446 题目:

poj2446 Chessboard

判断是否是完全匹配,和poj3020相似,给格子编号,由于水平不够效率很低,很多大神用的完全匹配,有一点值得学习,就是根据i+j的奇偶性,把点分成两个集合。 #include #include ...

POJ2446:Chessboard(二分匹配)

Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10746   Accepted: 3340...

【最大匹配】Chessboard POJ2446

Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11075   Ac...

POJ2446 Chessboard(二分图)

题意: 一个n*m的棋盘上有t个坑,要求用1*2的纸条完全覆盖这个棋盘,纸条不能盖上坑。 要点: 这题是二分图,就是求二分图的最大匹配,看是否与棋盘格子数-坑数相等。但是具体的集合很难想,看了网...

poj--2446 Chessboard(二分图最大匹配)

Chessboard题解初次练习 匈牙利算法。 二分图最大匹配问题与匈牙利算法的核心思想 对于相邻的两个格子,其(行数+列数)的值必然是一奇一偶,这就可以划分为两个集合,在这两个集合上跑一遍匈牙利算...

[poj 2446]Chessboard

想法题,强烈推荐
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 2446 Chessboard
举报原因:
原因补充:

(最多只允许输入30个字)