題意:象棋中馬是走日字形,問在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;
}