lightoj1010 Knights in Chessboard(找规律)

题目: 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.

解题思路:
  • 当宽为1,长为n时,最大数目为n个。
  • 已知最多是有2*2个旗子可以相连,当宽为2时,当长度为n为奇数时, 最大数目为2 *(n/2 + 1)。
  • 当n * m时,根据图中黑白格的情况推知,当旗子占据黑色位置时, 不可放的地方是白色位置, 以此可以推知,旗子可以占据黑色或者白色的位置,当n * m为奇数时,最大数目是n*m/2+1。
#include <stdio.h>  
#include <math.h>  

int main() {  
    int T, row, col, sum, count, num;
    scanf("%d", &T);
    count = 0;
    while (T--) {
        scanf("%d%d", &row, &col);
        sum = 0;
        if (row == 1 || col == 1) {
            sum = (row == 1) ? col : row;
        } else if (row == 2 || col == 2) {
            num = (row == 2) ? col : row;
            if (num % 4 == 0) {
                sum = 2 * num / 2;
            } else {
                sum = 2 * ((num / 2) + 1);
            }
        } else {
            sum = (row * col) % 2 ? (row * col) / 2 + 1 : (row * col) / 2;
        }
        printf("Case %d: %d\n", ++count, sum);
    }
    return 0;  
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值