您的位置:首页 > 其它

lightoj1010 Knights in Chessboard(找规律)

2017-12-21 11:42 357 查看

题目: 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: