Contest02-2 Cover——动态规划

Contest02-2 Cover

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Tom wants to cover a rectangular floor by indentical L-shape tiles without overlap. As shown below, the floor can be split into many small squares, and the L-shape tile consists of exactly four small squares. The floor of 3*8 can be completely covered by 6 L-shape tiles, but the floor of 3*7 is impossible. See Figure 1 and Figure 2. 


Tom would like to know whether an arbitrary floor with n*m small squares can be completely covered or not. He is sure that when n and m are small he can find the answer by paper work, but when it comes to larger n and m, he has no idea to find the answer. Can you tell him?

输入

The input file will consist of several test cases. Each case consisits of a single line with two positive integers m and n (1<=m<=15,1<=n<=40). 

The input is ended by m=n=0.

输出

For each case, print the word "YES" in a single line if it is possible to cover the m*n floor, print "NO" otherwise.

示例输入

3 8
3 7
0 0

示例输出

YES
NO


可以知道的是,根据L形状的瓷砖可以组成两种基本形态,一种是图中给出的3*8的形态(需要6块瓷砖),一种是2*4的形态(需要2块瓷砖)。
找到了两个基本的状态,还以为求余就能够水得过去,没想到这两种基本相态还可以组成第三种形态,所以整个地板的大矩形就需要被这三种基本形态所分割。交了很多遍,看来是水不过去了,请教了宁哥,学会了动态规划这种方法。
首先初始化,地板的面积不可能是0,所以将横向纵向的第0行初始化为真,用来做DP的初始状态。
接下来就是找动态转移方程:就本题而言,对于一个n*m的地板来说能够被瓷砖完全覆盖的条件是地板矩形的右下角恰好能够放下三种形态的一种,并且这种形态的另外三个角也可以分别放下其中一种形态,以此类推。。。。。。

#include <stdio.h>

int dx[6] = {2,4,3,8,5,8};
int dy[6] = {4,2,8,3,8,5};

int main()
{
    bool dp[20][50];
    for(int i = 0;i < 20;i++)
    {
        dp[i][0] = true;
    }
    for(int i = 0;i < 50;i++)
    {
        dp[0][i] = true;
    }
    for(int i = 1;i <= 15;i++)
    {
        for(int j = 1;j <= 40;j++)
        {
            dp[i][j] = false;
            for(int k = 0;k < 6;k++)
            {
                if(i-dx[k]>=0&&j-dy[k]>=0&&dp[i-dx[k]][j]&&dp[i][j-dy[k]]&&dp[i-dx[k]][j-dy[k]])
                {
                    dp[i][j] = true;
                    break;
                }
            }
        }
    }
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n == 0 && m == 0)
            break;
        if(dp[n][m])
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值