LightOJ1010---Knights in Chessboard (规律题)
2016-01-07 09:51
453 查看
Given an m x n chessboard where you want to place chess knights. You have to find the number of maximum knights that can be placed in the chessboard such that no two knights attack each other.
Those who are not familiar with chess knights, note that a chess knight can attack 8 positions in the board as shown in the picture below.
Input
Input starts with an integer T (≤ 41000), denoting the number of test cases.
Each case contains two integers m, n (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively.
Output
For each case, print the case number and maximum number of knights that can be placed in the board considering the above restrictions.
Sample Input
Output for Sample Input
3
8 8
3 7
4 10
Case 1: 32
Case 2: 11
Case 3: 20
Problem Setter: Jane Alam Jan
规律题
假设n∗mn*m是偶数且都大于2
答案是n∗m/2n * m / 2
假设n和m都是奇数且都大于2
答案就是一半的行放m/2+1m / 2 + 1个,还有一半放m/2m / 2个
假设n m 都小于等于2
n(m)为1,答案就是m(n)
m(n)有一个为2,那么我们能够考虑能够分出多少个2*2的格子
设有x个,答案是(x/2+x(x / 2 + x % 2)∗42) * 4
剩下来可能有一个2*1的,这时假设x是奇数,不能放,假设x是偶数能够放
Those who are not familiar with chess knights, note that a chess knight can attack 8 positions in the board as shown in the picture below.
Input
Input starts with an integer T (≤ 41000), denoting the number of test cases.
Each case contains two integers m, n (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively.
Output
For each case, print the case number and maximum number of knights that can be placed in the board considering the above restrictions.
Sample Input
Output for Sample Input
3
8 8
3 7
4 10
Case 1: 32
Case 2: 11
Case 3: 20
Problem Setter: Jane Alam Jan
规律题
假设n∗mn*m是偶数且都大于2
答案是n∗m/2n * m / 2
假设n和m都是奇数且都大于2
答案就是一半的行放m/2+1m / 2 + 1个,还有一半放m/2m / 2个
假设n m 都小于等于2
n(m)为1,答案就是m(n)
m(n)有一个为2,那么我们能够考虑能够分出多少个2*2的格子
设有x个,答案是(x/2+x(x / 2 + x % 2)∗42) * 4
剩下来可能有一个2*1的,这时假设x是奇数,不能放,假设x是偶数能够放
/************************************************************************* > File Name: LightOJ1010.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年06月08日 星期一 14时24分24秒 ************************************************************************/ #include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <queue> #include <stack> #include <map> #include <bitset> #include <set> #include <vector> using namespace std; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long long LL; typedef pair <int, int> PLL; int main() { int t, icase = 1; scanf("%d", &t); while (t--) { int n, m; scanf("%d%d", &n, &m); int ans = 0; if (n >= 3 && m >= 3) { if (n * m % 2 == 0) { ans = n * m / 2; } else { int s = m / 2 + 1; ans += (n / 2 + 1) * s; ans += (n / 2) * (s - 1); } } else { if (n == 1) { ans = m; } else if (m == 1) { ans = n; } else { if (m == 2) { swap(n, m); } if (m <= 3) { ans = 4; } else { int cnt = m / 2; ans = (cnt / 2 + cnt % 2) * 4; if (m % 2 && cnt % 2 == 0) { ans += 2; } } } } printf("Case %d: %d\n", icase++, ans); } return 0; }
相关文章推荐
- Java Web系列:Hibernate 基础
- css设置移动端checkbox和radio样式
- Xcode 免费真机调试方法
- 使用eclipse集成的maven的方法
- 邮件开发:POP3协议详解
- 2016年度规划
- Swift 创建framework并应用到项目中
- php composer
- Awesome PHP
- UE4 Socket多线程非阻塞通信【2】
- yum 安装Apache,mysql
- string,string.h和ctring的区别
- 拥抱 Android Studio 之三:溯源,Groovy 与 Gradle 基础
- Innodb 表空间传输迁移数据
- 图片和Base64编码转换
- oracle max()函数和min()函数
- Java heap dump触发和分析(转)
- perl 面向对象
- delete和delete[]的区别
- The file couldn't be opened because you don't have permission to view it.解决办法