696-how Many Knights
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php? option=onlinejudge&page=show_problem&problem=637
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 to 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.
In this problem your are to determine the largest number of knights so can be placed on a board with M rows and N columns So this no knight is attacking the other. M and N 'll each is 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 so can be Appropriat Ely placed.
Sample Input
2 3
5 5
4 7 0 0
Sample Output
4 Knights May is placed on a 2 row 3 column board.
Knights May is placed on a 5 row 5 column board.
Knights May is placed on a 4 row 7 column board.
Ideas:
As shown in the figure, the following three cases (see Code)
Complete code:
/*0.029s*/
#include <cstdio>
inline int cal (int m, int n)
{
if (M > N) return cal (n, m);
if (m = = 1) return n;
if (m = = 2) return ((n >> 2 << 1) + (n% 4 = = 3? 2:n% 4)) << 1;
Return (M * n + 1) >> 1;
}
int main (void)
{
int m, n;
while (scanf ("%d%d", &m, &n), m)
printf ("%d knights is placed on a%d row%d column board.\n", Cal
( M, N), M, n);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/