UVA 696 - How Many Knights (规律)

来源:互联网 发布:win与mac键盘对照 编辑:程序博客网 时间:2024/05/21 21:42

  How Many Knights 

The knight is a piece used in chess, a game played on a board with squares arranged in rows and columns. A knight attacks pieces that are either (a) two rows and one column away from its position, or (b) one row and two columns away from its position. The following diagram illustrates this. The square marked N represents the position of the knight, and the squares marked X indicate the squares that are under attack.

 X X X   X  N  X   X X X 

In this problem you are to determine the largest number of knights that can be placed on a board with M rows and N columns so that no knight is attacking any other. M and N will each be no larger than 500.

Input 

The input consists of pairs of integers giving values for M and N, followed by a pair of zeroes.

Output 

For each input pair, display the number of rows and columns in the board, and the number of knights that can be appropriately placed.

Sample Input 

2 35 54 70 0

Sample Output 

4 knights may be placed on a 2 row 3 column board.13 knights may be placed on a 5 row 5 column board.14 knights may be placed on a 4 row 7 column board.

题意:给定n * m棋盘,问最多放几个马

思路:找规律, 分情况讨论, n || m == 1 n || m == 2 其他。三种情况。

代码:

#include <stdio.h>int n, m, ans;int main() {    while (~scanf("%d%d", &n, &m) && n + m) {if (n == 1) ans = m;else if (m == 1) ans = n;else if (n == 2) ans = m / 4 * 4 + ((m % 4 * 2) >= 4 ? 4 : (m % 4 * 2));else if (m == 2) ans = n / 4 * 4 + ((n % 4 * 2) >= 4 ? 4 : (n % 4 * 2));else ans = (m * n + 1) / 2;printf("%d knights may be placed on a %d row %d column board.\n", ans, n, m);    }    return 0;}