LightOJ-1010-Knights in Chessboard [规律]

来源:互联网 发布:淘宝客学生采集群 编辑:程序博客网 时间:2024/06/05 00:39

题目传送门


题意:象棋中马是走日字形,问在m*n的棋盘中最多可以放多少个马,使他们不能互相攻击。

思路:规律题。
- 如果只有一行或者一列,则所有的棋盘均可以放马。
- 如果有两行或者两列,则一个田字型可以放2*2个马,然后空出一个田字型。
- 其他情况则可以放总面积的一半。

#include <bits/stdc++.h>using namespace std;int main(void){    int T, cas=1;    scanf("%d", &T);    while (T--)    {        int x, y;        scanf("%d %d", &x, &y);        if (x==1 || y==1)        {            printf("Case %d: %d\n", cas++, max(x, y));            continue;                   }        if (x==2 || y==2)        {            int p = max(x, y);            printf("Case %d: %d\n", cas++, p/4*4+(p%4>=2?4:p%4*2));            continue;        }        printf("Case %d: %d\n", cas++, (x*y+1)/2);    }    return 0;}