UVa 696 how Many Knights (idea title)

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.