Given an m x n chessboard where you want to place chess knights. You have to find the number of maximum knights that can be placed in the chessboard such that no two knights attack each other.
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
Output for Sample Input
3
8 8
3 7
4 10
Case 1: 32
Case 2: 11
Case 3: 20
规律好找
具体的找法是一开始瞎画
后来发现了被攻击的点被占得次数越多越好
然后左上角那个点不论何时占住都很好
然后就有了这样的策略
从左上角的点找到不能走的点,从这个不能走的点延伸出所有要走的点
再把不能走的划掉
直到全图都有标记了为止
….
然后应该就是隔一个画一次
好了坑点来了
需要注意这么几种特殊情况
一种是一排的
这种全部都是满的
第二种是n或者m为2的
他可以一个田字格一个空田字格。。
这样最小化也是1/2
这就很坑很烦
很蓝受
虽然数据不大
但是cin的话会TLE
坑!
#include<iostream>
#include<cstdio>
#include<memory.h>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
int T;
cin>>T;
int u=0;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
if(n==1||m==1)
{
printf("Case %d: %d\n",++u,n*m);
continue;
}
if(n<=2&&m<=2)
{
printf("Case %d: %d\n",++u,n*m);
continue;
}
if(n==2)
{
int qq=m/2;
if(qq%2==0)
{
printf("Case %d: %d\n",++u,2*qq+m*n-4*qq);
continue;
}
else
{
printf("Case %d: %d\n",++u,4*((qq/2)+1));
continue;
}
}
if(m==2)
{
int qq=n/2;
if(qq%2==0)
{
printf("Case %d: %d\n",++u,2*qq+m*n-4*qq);
continue;
}
else
{
printf("Case %d: %d\n",++u,4*((qq/2)+1));
continue;
}
}
if(n%2==0||m%2==0)printf("Case %d: %d\n",++u,n*m/2);
else printf("Case %d: %d\n",++u,(n*m/2)+1);
}
return 0;
}