LightOJ-1010-Knights in Chessboard(数学)

链接:

https://vjudge.net/problem/LightOJ-1010

题意:

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.

思路:

存在一行时,直接乘,存在两行时,每8个用四个,多的最多用4个。
其他的m*2/2.

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
const int MOD = 1e9+7;

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

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11817271.html