【策略】UVa 278 - Chess

时间:2023-09-18 13:52:38
Chess 

Almost everyone knows the problem of putting eight queens on an 【策略】UVa 278 - Chess 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 【策略】UVa 278 - Chess 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 ( 【策略】UVa 278 - Chess ) and n ( 【策略】UVa 278 - Chess ), 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

题意:在一个m*n的棋盘上最多能放置多少个c类型的棋子。棋子间保证不互相攻击。

攻击方式为国际象棋规则,首先简单科普一下:

Q(Queen):按照八皇后攻击规则,即一行,一列,对角线不能存在棋子。可知最多能放八个棋子。

K(King):国王攻击周围八个棋子。最优方案为行列间隔放置。

r (Rook):战车攻击方式为直线攻击。所以最多能放行列的最小值。

k(Knight):骑士的攻击方式为日字攻击,但不会“蹩马腿”。骑士的方案需要分情况:

  1、当只有一行(列)时,当然可以放全部棋子。

  2、当有两行(列)时,最优方案时田字放置。盗图一张。【策略】UVa 278 - Chess

  3、当大于两行(列)时,最优方案是隔列放置。

附代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define error 1e-8
using namespace std;
const int maxn = ;
int chess[maxn][maxn];
int main()
{
int T; scanf("%d", &T);
while(T--)
{
char kind[]; int r, c;
int ans;
scanf("%s%d%d", kind, &r, &c);
if(kind[] == 'r' || kind[] == 'Q') ans = min(r, c);
else if(kind[] == 'K')
{
ans = ((r+)/)*((c+)/);
}
else if(kind[] == 'k')
{
int m = max(r, c), n = min(r, c);
if(r == || c == ) ans = m;
else if(r == || c == )
{
ans = m/* + m%*;
}
else
{
ans = (n/)*(m/+(m+)/) + (n%)*((m+)/);
}
}
printf("%d\n", ans);
}
return ;
}