LightOJ1010---Knights in Chessboard (规律题)

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

Problem Setter: Jane Alam Jan

规律题

假设n?m是偶数且都大于2

答案是n?m/2

假设n和m都是奇数且都大于2

答案就是一半的行放m/2+1个,还有一半放m/2个

假设n m 都小于等于2

n(m)为1,答案就是m(n)

m(n)有一个为2,那么我们能够考虑能够分出多少个2*2的格子

设有x个,答案是(x/2+x % 2)?4

剩下来可能有一个2*1的,这时假设x是奇数,不能放,假设x是偶数能够放

/*************************************************************************
    > File Name: LightOJ1010.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年06月08日 星期一 14时24分24秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        int n, m;
        scanf("%d%d", &n, &m);
        int ans = 0;
        if (n >= 3 && m >= 3) {
            if (n * m % 2 == 0) {
                ans = n * m / 2;
            }
            else {
                int s = m / 2 + 1;
                ans += (n / 2 + 1) * s;
                ans += (n / 2) * (s - 1);
            }
        }
        else {
            if (n == 1) {
                ans = m;
            }
            else if (m == 1) {
                ans = n;
            }
            else {
                if (m == 2) {
                    swap(n, m);
                }
                if (m <= 3) {
                    ans = 4;
                }
                else {
                    int cnt = m / 2;
                    ans = (cnt / 2 + cnt % 2) * 4;
                    if (m % 2 && cnt % 2 == 0) {
                        ans += 2;
                    }
                }
            }
        }
        printf("Case %d: %d\n", icase++, ans);
    }
    return 0;
}
时间: 2025-01-06 00:49:34

LightOJ1010---Knights in Chessboard (规律题)的相关文章

LightOJ 1010 Knights in Chessboard (规律)

题意:给定一个m*n的棋盘,问最多放多少个马,使得他们不相互攻击. 析:很明显可以从上图看出来了马放在白格,或者黑格,不会攻击,不过行或者列为1,2时是特殊的,我们只要特殊判断一下就行了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cm

ZOJ 3798 Abs Problem(规律题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3798 Abs Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Alice and Bob is playing a game, and this time the game is all about the absolute value! Alice has

HDU 4279 Number 规律题

题意: 定义函数F(x) : 区间[1,x]上的y是满足:GCD(x,y)>1 && x%y>0的 y的个数. 问:对于任意区间[l,r] 上的F(l···r) 有几个函数值是奇数的. 打表找规律. 打的是[1,x]区间的结果 把所有结果不相同的值打出来(因为结果是递增的,所以只观察不相同的结果) 发现:ans = x/2-2 || x/2-1 再把所有结果不同 && x/2-1的值打出来 发现 sqrt(x) &1 == 1 得到:ans = x/2-

hdu5351 MZL&#39;s Border(规律题,java)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 905    Accepted Submission(s): 295 Problem Description As is known to all,

HDU_1098 Ignatius&#39;s puzzle[规律题]

Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal

HDU 4937 Lucky Number 规律题_(:зゝ∠)_

把所有合法的进制打出来会发现合法的进制都是在 n/3 n/4 n/5的边上 然后暴力边上的进制数.. #include <cstdio> #include <set> typedef long long ll; bool ok(ll x, ll y) { ll v; while (x > 0) { v = x % y; if (v != 3 && v != 4 && v != 5 && v != 6) return false;

HDU 4940 Destroy Transportation system 规律题

答案只有2种情况,所以ans = rand()%2; if(ans)puts("happy") else puts("unhappy"); == 想过无源汇的网络流,还是比较麻烦的,然后没往下想... 设s点集有一些点, 多加一个点一定是y增加比较快_(:зゝ∠)_ 然后设s点集只有一个点 #include <cstdio> #include <map> #include <set> #include <algorithm&

HDU 4952 Number Transformation 规律题

打表可以知道到后面增量都一样了,, 推论就是  i 和 i+1 互质 #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const ll mx = 120000; int main() { int cas = 0; ll x, k, y, dis, i; while (

Codeforces 10C Digital Root 规律题

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<string> #include<stdlib.h> #include<algorithm> using nam