LightOJ-1010-Knights in Chessboard [規律]


題目傳送門


題意:象棋中馬是走日字形,問在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;
}
發佈了101 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章