LightOJ1010 棋盘上能放的最多马数

来源:互联网 发布:简历制作软件 app 编辑:程序博客网 时间:2024/06/02 05:04

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;}


0 0