0
点赞
收藏
分享

微信扫一扫

Light OJ 1010 - Knights in Chessboard【思维+规律】

guanguans 2023-02-07 阅读 26


Lightoj 1010 - Knights in Chessboard

1010 - Knights in Chessboard

  

​​PDF (English)​​

​​Statistics​​

​​Forum​​

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

 

 

 

题意:n*m的格子,可以放多少马,保证他们不相互进攻

分析:

给你提供的数据,可以猜测出答案ans=n*m%2==1?n*m/2+1:n*m/2;

具体证明的话就看图了

Light OJ 1010 - Knights in Chessboard【思维+规律】_c++

发现最多就是同一个颜色的格子全部放上马才是最大的。

但有坑啊,还有两个,

1.

n==1||m==1

一行都放上马 

2.

n==2||m==2

我们发现上述的结论也是不成立的,

你们规律是啥,其实枚举一下就能看出规律

假设2行m列

Light OJ 1010 - Knights in Chessboard【思维+规律】_ide_02

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+5;
int a[N];

int main(){
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
int ans;
if(n==1||m==1)
ans=n*m;
else if(n==2||m==2)
{
int maxx=max(n,m);
ans=maxx/4*4;
if(maxx%4==1)
ans+=2;
else if(maxx%4>=2)
ans+=4;
}
else
{
if((n*m)%2==0)
ans=n*m/2;
else
ans=n*m/2+1;
}
printf("Case %d: %d\n",++cas,ans);

}
}

 

举报

相关推荐

0 条评论