POJ2446 CHESSBOARD

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

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.

题解:

说一下我的做法(很扯):我们把棋盘染色(对,就是染色),按照对角线染黑白把棋盘转化成如图所示的情况,这样就把棋盘变成了二分图。很容易证明每块砖都是盖住一黑一白两个点,这样的话我们在读入的时候把黑色点附近的能盖住的白色点记为连通,用匈牙利算法跑一遍,如果最大覆盖的砖块数量*2恰好等于棋盘格子数减去hole的数量那么输出yes,反之则是no

include

include

include

include

define maxn 40

using namespace std;

bool g[maxn*maxn][maxn*maxn],state[maxn*maxn],hole[maxn][maxn];
int result[maxn*maxn];
int m,n,s,v,cnt=0,k;

bool find(int k1)
{
int x1=k1/m+1;
int y1=k1%m;
if(y1==0){
y1=m;
x1–;
}
if(y1>1)
{
int k2=k1-1;
if(!state[k2]&&g[k1][k2])
{
state[k2]=1;
if(result[k2]==0||find(result[k2]))
{
result[k2]=k1;
return true;
}
}
}

if(y1<m)
{
    int k2=k1+1;
    if(!state[k2]&&g[k1][k2])
    {
        state[k2]=1;
        if(result[k2]==0||find(result[k2]))
        {
            result[k2]=k1;
            return true;
        }
    }
}
if(x1>1)
{
    int k2=k1-m;
    if(!state[k2]&&g[k1][k2])
    {
        state[k2]=1;
        if(result[k2]==0||find(result[k2]))
        {
            result[k2]=k1;
            return true;
        }
    }
}
if(x1<n)
{
    int k2=k1+m;
    if(!state[k2]&&g[k1][k2])
    {
        state[k2]=1;
        if(result[k2]==0||find(result[k2]))
        {
            result[k2]=k1;
            return true;
        }
    }
}

return false;

}

int main()
{
while(~scanf(“%d %d %d”,&n,&m,&k))
{
cnt=0;
memset(g,0,sizeof(g));
memset(hole,0,sizeof(hole));
memset(state,0,sizeof(state));
memset(result,0,sizeof(result));
for(int i=1;i<=k;i++)
{
int a,b;
scanf(“%d%d”,&a,&b);
hole[b][a]=1;
}
for(int i=1;i<=n;i++){

   for(int j=1;j<=m;j++)
   {
     if(hole[i][j])continue;
     int k1=(i-1)*m+j;
     if((i+j)%2==0)
     {
       if(!hole[i-1][j]&&i>1)
       {  
          int k2=(i-2)*m+j;
          g[k1][k2]=1;
       }

       if(!hole[i][j-1]&&j>1)
       {  
          int k2=(i-1)*m+j-1;
          g[k1][k2]=1;

       }
       if(!hole[i+1][j]&&i<n)
       {  
          int k2=i*m+j;
          g[k1][k2]=1;

       }    

       if(!hole[i][j+1]&&j<m)
       {  
          int k2=(i-1)*m+j+1;
          g[k1][k2]=1;

       }

     }
   }
}
   for(int i=1;i<=n;i++)
   for(int j=1;j<=m;j++)
   {
    memset(state,0,sizeof(state));

     if(hole[i][j])continue;
     if(find((i-1)*m+j))cnt+=2;
   }
  if(m*n-cnt==k)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
 }

return 0;

}

//uva1395 uva1151

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值