POJ 2446 Chessboard(匈牙利算法)

Chessboard
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19211 Accepted: 6063

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


【思路】

将棋盘像国际象棋棋盘那样分割为黑白块,那么用纸片覆盖时就可以视作匹配没有洞的黑块和白块,用匈牙利算法求二分图的最大匹配即可。具体可以这么建图,二分图的左右点集都是黑白块1到N*M,若黑块可能与周边白块相匹配,则从左点集对应的黑块与右点集对应的白块连一条线。跑一遍匈牙利算法后,看二倍最大匹配数是否等于N*M-K就好了。


【代码】

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

const int MAXN=100;
const int DIR[4][2]={{0,-1},{-1,0},{0,1},{1,0}};

int m,n,k;
bool visited[MAXN*MAXN];
bool hole[MAXN][MAXN];
int xmatch[MAXN*MAXN],ymatch[MAXN*MAXN];
vector<int> connect[MAXN];

bool dfs(int u)
{
    for(int i=0;i<connect[u].size();i++){
        int j=connect[u][i];
        if(!visited[j]){
            visited[j]=true;
            if(ymatch[j]==-1||dfs(ymatch[j])){
                xmatch[u]=j;
                ymatch[j]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int ans=0;
    memset(xmatch,0xff,sizeof(xmatch));
    memset(ymatch,0xff,sizeof(ymatch));
    for(int i=1;i<=n*m;i++){
        memset(visited,false,sizeof(visited));
        if(dfs(i))ans++;
    }
    return ans;
}

int main()
{
    memset(hole,false,sizeof(hole));
    scanf("%d %d %d",&m,&n,&k);
    for(int i=1;i<=k;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        hole[y][x]=true;
    }
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            if(!hole[i][j])
                for(int k=0;k<4;k++){
                    int y=i+DIR[k][0],x=j+DIR[k][1];
                    if(1<=y&&y<=m&&1<=x&&x<=n&&!hole[y][x]){
                        connect[(i-1)*n+j].push_back((y-1)*n+x);
                    }
                }
    if(hungary()==n*m-k)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值