# LightOJ 1010

115人阅读 评论(0)

1010 - Knights in Chessboard
 Time Limit: 1 second(s) Memory Limit: 32 MB

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.

# Output for Sample Input

3

8 8

3 7

4 10

Case 1: 32

Case 2: 11

Case 3: 20

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
int t, ncase=1;
cin>>t;
while(t--)
{
int m, n;
scanf("%d %d", &m, &n);
printf("Case %d: ",ncase++);
if(m==1||n==1)
{
printf("%d\n",n*m);
}
else if(n==2||m==2)
{
int x=(m*n)%8, y=((m*n)/8)*4;
if(x>4)
{
x=4;
}
printf("%d\n",x+y);
}
else
{
if((m*n)&1)
{
printf("%d\n",(m*n)/2+1);
}
else
{
printf("%d\n",(m*n)/2);
}
}
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
最新评论