http://www.lightoj.com/volume_showproblem.php?problem=1010
| 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.
Sample Input | Output for Sample Input |
| 3 8 8 3 7 4 10 | Case 1: 32 Case 2: 11 Case 3: 20 |
//几种可行的方法然后取最大值
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
const int SIZE=1e5+10;
const int maxn=500000;
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
int n,m;
scanf("%d%d",&n,&m);
printf("Case %d: ",cas);
int ans=n*m/2;
ans=max(ans,n*m-ans);
if(n==1||m==1)ans=max(ans,n*m);
else if(n==2||m==2)ans=max(ans,2*(2*(max(n,m)/4)+min(2,max(n,m)%4)));
printf("%d\n",ans);
}
return 0;
}
本文探讨了在给定尺寸的棋盘上放置骑士的问题,目标是最大化放置骑士的数量,同时确保没有任何两个骑士能够互相攻击。文章提供了一种算法实现方案,并通过几个样例输入输出展示了该算法的有效性。

757

被折叠的 条评论
为什么被折叠?



