Chess 

Almost everyone knows the problem of putting eight queens on an  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  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 (  ) and n (  ), 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、当有两行(列)时,最优方案时田字放置。盗图一张。

  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 ;
}

【策略】UVa 278 - Chess的更多相关文章

  1. Uva 11538 - Chess Queen

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. uva 11538 Chess Queen<计数>

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  3. 组合数学 UVa 11538 Chess Queen

    Problem A Chess Queen Input: Standard Input Output: Standard Output You probably know how the game o ...

  4. 【基本计数方法---加法原理和乘法原理】UVa 11538 - Chess Queen

    题目链接 题意:给出m行n列的棋盘,当两皇后在同行同列或同对角线上时可以互相攻击,问共有多少种攻击方式. 分析:首先可以利用加法原理分情况讨论:①两皇后在同一行:②两皇后在同一列:③两皇后在同一对角线 ...

  5. 【组合计数】UVA - 11538 - Chess Queen

    考虑把皇后放在同一横排或者统一纵列,答案为nm(m-1)和nm(n-1),显然. 考虑同一对角线的情况不妨设,n<=m,对角线从左到右依次为1,2,3,...,n-1,n,n,n,...,n(m ...

  6. UVa 11538 Chess Queen (排列组合计数)

    题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...

  7. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  8. 【策略】UVa 1344 - Tian Ji -- The Horse Racing(田忌赛马)

    Here is a famous story in Chinese history. That was about 2300 years ago. General Tian Ji was a high ...

  9. 【策略】UVa 11389 - The Bus Driver Problem

    题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线.给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r.问如何分配路线才能使加班费最少. 虽然代码看起来 ...

随机推荐

  1. Java设计模式----组合模式(Composit )

    1.  组合模式定义: 组合模式,又叫合成模式,有时又叫部分-整体模式,主要用来描述部分与整体的关系. 定义:将对象组合成树形结构以示" 部分--整体 "的层次结构,使得用户对单个 ...

  2. POJ 3660 Cow Contest (最短路dijkstra)

    MPI Maelstrom 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/G Description BIT has recen ...

  3. POJ 1502 MPI Maelstrom(最短路)

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4017   Accepted: 2412 Des ...

  4. Umbraco Forms 中的Recaptcha遇到的问题

    在Umbraco Form中添加Recaptcha时,不能把它设置成Mandatory, 否则就会出错

  5. BNU 51276 - 道路修建 Small (并查集)

    题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=51276 具体题意不描述了,一眼看过去就是并查集,关键是添加边以后更新答案.我是开个二维的数组an ...

  6. OGNL stack value 值栈(主要参考官方手册)

    The framework uses a standard naming context to evaluate OGNL expressions. The top level object deal ...

  7. ThinkPHP3.1.3的单字母函数汇总

    A函数: 用于实例化Action 格式:[项目://][分组/]模块 /** * A函数用于实例化Action 格式:[项目://][分组/]模块 * @param string $name Acti ...

  8. List、ArrayList、Vector及map、HashTable、HashMap分别的区别

    一.List与ArrayList的区别      List->AbstractList->ArrayList     (1) List是一个接口,ArrayList是一个实现了List接口 ...

  9. CUDA从入门到精通

    http://blog.csdn.net/augusdi/article/details/12833235 CUDA从入门到精通(零):写在前面 在老板的要求下.本博主从2012年上高性能计算课程開始 ...

  10. 很值得学习的java 画图板源码

    很值得学习的java 画图板源码下载地址:http://download.csdn.net/source/2371150 package minidrawpad; import java.awt.*; ...