UVa 278 Chess (想法题)

278 - Chess

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=214

Almost everyone knows the problem of putting eight queens on an tex2html_wrap_inline30 chessboard such that no Queen can take another Queen. Jan Timman (a famous Dutch chessplayer) wants to know the maximum number of chesspieces of one kind which can be put on an tex2html_wrap_inline32 board with a certain size such that no piece can take another. Because it's rather difficult to find a solution by hand, he asks your help to solve the problem.

He doesn't need to know the answer for every piece. Pawns seems rather uninteresting and he doesn't like Bishops anyway. He only wants to know how many Rooks, Knights, Queens or Kings can be placed on one board, such that one piece can't take any other.

Input

The first line of input contains the number of problems. A problem is stated on one line and consists of one character from the following set rkQK, meaning respectively the chesspieces Rook, Knight, Queen or King. The character is followed by the integers m ( tex2html_wrap_inline36 ) and n ( tex2html_wrap_inline40 ), meaning the number of rows and the number of columns or the board.

Output

For each problem specification in the input your program should output the maximum number of chesspieces which can be put on a board with the given formats so they are not in position to take any other piece.

Note: The bottom left square is 1, 1.

Sample Input

2
r 6 7
k 8 8

Sample Output

6
32

此题是UVa 696 的升级版。

r和Q都是max(m,n)

k的公式详见这里

K就是一个点周围8个不能有其他点。


完整代码:

/*0.009s*/

#include <cstdio>

int cal(char ch, int m, int n)
{
	if (m > n) return cal(ch, n, m);
	if (ch == 'r' || ch == 'Q') return m;
	if (ch == 'K') return ((n + 1) >> 1) * ((m + 1) >> 1);
	if (m == 1) return n;
	if (m == 2) return (n >> 2 << 2) + ((n % 4 == 3 ? 2 : n % 4) << 1);
	return (n * m + 1) >> 1;
}

int main()
{
	int t, m, n;
	char ch;
	scanf("%d", &t);
	getchar();
	while (t--)
	{
		scanf("%c%d%d", &ch, &m, &n);
		getchar();
		printf("%d\n", cal(ch, m, n));
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值