(最小路径覆盖) poj 2446

E - Chessboard
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int mp[1230][1230],link[1230],mark[1230],g[35][35],ans,opt[35][35];
int n,m,k,temp;
bool dfs(int x)
{
    for(int i=1;i<=temp;i++)
    {
        if(mark[i]==-1&&mp[x][i])
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int x,y;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        temp=0;
        ans=0;
        memset(link,-1,sizeof(link));
        memset(g,0,sizeof(g));
        memset(opt,0,sizeof(opt));
        for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&y,&x);
            opt[x][y]=1;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(!opt[i][j])
                {
                    g[i][j]=++temp;
                }
            }
        }
        memset(mp,0,sizeof(mp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(g[i][j]!=0)
                {
                    if(i>1&&g[i-1][j]!=0)
                        mp[g[i][j]][g[i-1][j]]=1;
                    if(i<n&&g[i+1][j]!=0)
                        mp[g[i][j]][g[i+1][j]]=1;
                    if(j>1&&g[i][j-1]!=0)
                        mp[g[i][j]][g[i][j-1]]=1;
                    if(j<m&&g[i][j+1]!=0)
                        mp[g[i][j]][g[i][j+1]]=1;
                }
            }
        }
        for(int i=1;i<=temp;i++)
        {
            memset(mark,-1,sizeof(mark));
            if(dfs(i))
                ans++;
        }
        if(ans==temp)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

  

posted @ 2015-04-27 18:31  waterfull  阅读(283)  评论(0编辑  收藏  举报