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. jdk的wsimport方法实现webservice客户端调用服务

    1.配置好jdk环境,打开命令行,输入wsimport回车能看到很多该命令的参数, -s:要生成客户端代码的存储路径 -p:对生成的代码从新打包 这两个最常用. 在打开的命令行中输入:wsimport ...

  2. hdu 1443 Joseph (约瑟夫环)

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. [iOS 多线程 & 网络 - 2.3] - 解析xml

    A.XML基本知识 1.xml概念 什么是XML全称是Extensible Markup Language,译作“可扩展标记语言”跟JSON一样,也是常用的一种用于交互的数据格式一般也叫XML文档(X ...

  4. MFC实现数独(2)

    主要功能描述: 运行程序后对话框会显示一个9x9的待输入数独区域,并提供随机生成数独和生成数独按钮,生成数独按钮后会创建数独并随机显示其中一个至数独区域,随机生成数独会从已生成的数独中随机获取一个并显 ...

  5. IEBrowse学习笔记

    //登录 private void toolStripButton1_Click(object sender, EventArgs e) { //ie.ExecuteScript("aler ...

  6. IT职场生存法则

    转!!!!!!!!!!!!! 摘要我在IT职场打滚超过15年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说我的亲身体会,希望大家能在IT职场上战无不 ...

  7. 当LinkButton无效时,光标不显示为手型

    在Flex组件LinkButton里,我们可以用useHandCursor属性来控制是否使用手型光标.现在我们要实现在LinkButton的enable=false时,useHandCursor=fa ...

  8. 一个无聊的实验:验证网站是否通过web容器还是微服务部署

    一般来说一台web服务器会部署多个实例(且共享80端口),举个栗子例如nginx通常部署多个站点,每个站点都有自己的端口 例如 8091,8092之类的. 通过nginx进行代理.(前提微服务直接使用 ...

  9. 搭建sentry(一个分布式日志聚合系统)

    简介: Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建. Sentry 可以帮助你将 Python 程序的所有 exception 自动记录下来,然后在一个好用的 UI 上呈现 ...

  10. php openssl 生成公私钥,根据网上文章整理的

    linux下没有问题,win下有报错 <?php$configargs = array("config" => "/usr/local/php/ext/ope ...