HDU5100 Chessboard

本文探讨了一个关于使用k×1大小的多米诺骨牌来填充n×n棋盘的问题,并提供了一种高效的算法来确定可以被填充的最大方格数量。

Chessboard
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 799 Accepted Submission(s): 335

Problem Description
Consider the problem of tiling an n×n chessboard by polyomino pieces that are k×1 in size; Every one of the k pieces of each polyomino tile must align exactly with one of the chessboard squares. Your task is to figure out the maximum number of chessboard squares tiled.

Input
There are multiple test cases in the input file.
First line contain the number of cases T (T≤10000).
In the next T lines contain T cases , Each case has two integers n and k. (1≤n,k≤100)

Output
Print the maximum number of chessboard squares tiled.

Sample Input
2
6 3
5 3

Sample Output
36
24

不一定是按照
这里写图片描述
这样的填充再是剩余最小在r=n%k r>k/2的时候将不适用
需要把其余的填满再剩余一个(r+k)*(r+k)的区域然后用风车型填充类似于这样
这里写图片描述
对于样例 5 3
这里写图片描述
这样并不最优可以看到r=2,k=3。r>k/2;
这时我们这样填充
这里写图片描述
这样就是最优的方案没有覆盖的区域变为(k-r)*(k-r)
不费话了下面是代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>

using namespace std;
int n;
int k;
int main(){
    int T; scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&k);
        if(k>n)
            printf("0\n");
        else{
            int r = n%k;
            if(r<=k/2)//k为奇数的时候向下取整这是r还是小于k/2所以要加上等于
                printf("%d\n",n*n-r*r);
            else{
                printf("%d\n",n*n-(k-r)*(k-r));
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值