LightOJ1010 棋盘上能放的最多马数

99 篇文章 2 订阅

Knights in Chessboard

LightOJ - 1010 

 Given an Those who are not familiar with chess knights, note that a chess knight can attack 8 positions in the board as shown in the picture below.

Input

Input starts with an integer T (≤ 41000), denoting the number of test cases.

Each case contains two integers m, n (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively.

Output

For each case, print the case number and maximum number of knights that can be placed in the board considering the above restrictions.

Sample Input

3

8 8

3 7

4 10

Case 1: 32

Case 2: 11

Case 3: 20



做法:

对于棋盘,我们会发现,如果我们只把马放在白色的方格内,则任意两个马都不会互相攻击。

这样我们有了第一种放法,全放在白格内或者全放在黑格内。

但是有一些特殊情况。

假如n=min(n,m),m=max(n,m);

如果n=1,那么我们可以在棋盘上全放上棋子。

如果n=2,那么我们可以一次把一个田字格全部放上马,然后间隔一个田字格,然后再放马。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t,kcase=1;;
    scanf("%d", &t);
    while(t--)
    {
        int m,n,ans;
        scanf("%d%d", &m, &n);
        if(m == 1 || n == 1)
            ans = m*n;
        else if(m == 2 || n == 2)
        {
            if(m == 2)
                if(n%4 < 2)
                    ans = (n/4)*4 + (n%4)*2;
                else
                    ans = (n/4)*4 + 4;
            if(n == 2)
                if(m%4 < 2)
                    ans = (m/4)*4 + (m%4)*2;
                else
                    ans = (m/4)*4 + 4;
        }
        else
            ans = (m*n+1)/2;
        printf("Case %d: %d\n", kcase++, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值