UVa 696 How Many Knights (想法题)

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 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 3
5 5
4 7
0 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.

思路:

如图所示,分以下三种情况(详见代码)


完整代码:

/*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 may be placed on a %d row %d column board.\n", cal(m, n), m, n);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值