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; }
相关文章推荐
- LightOJ 1010 Knights in Chessboard (找规律)
- LightOJ-1010-Knights in Chessboard [规律]
- LightOJ1010---Knights in Chessboard (规律题)
- LightOJ 1010 Knights in Chessboard(数学规律)
- lightoj 1010 - Knights in Chessboard(找规律)
- Lightoj1010——Knights in Chessboard(找规律)
- LightOJ1010---Knights in Chessboard (规律题)
- lightoj 1010 - Knights in Chessboard (找规律思维)
- LightOJ 1010 Knights in Chessboard (规律)
- lightoj 1010-Knights in Chessboard (规律)
- 1010 - Knights in Chessboard(找规律)
- LightOJ 1010 Knights in Chessboard
- lightoj 1010 - Knights in Chessboard 【数学思维】
- Lightoj 1010 - Knights in Chessboard
- Light oj 1010 Knights in Chessboard (思路,规律)
- lightoj-1010-Knights in Chessboard
- 1010 - Knights in Chessboard
- Light OJ 1010 - Knights in Chessboard【思维】
- LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
- light1010 - Knights in Chessboard【找规律】