lightoj 1010 - Knights in Chessboard 【数学思维】
2015-11-04 21:56
393 查看
1010 - Knights in Chessboard
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.
![](http://www.lightoj.com/data/problems/desc/1010/1010.png)
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.
PROBLEM SETTER: JANE ALAM JAN
题意:给定一个n*m的棋盘,现在给出马能攻击的8个位置,问最多放置多少个马使得任意两匹马之间不能相互攻击。
40000组测试数据 + 200 * 200的棋盘,匹配 状压 网络流 都不行吧。
那只能找规律了。
思路:
(1) n == 1 || m == 1时,结果显然就是max(n, m);
(2) n > 2 && m > 2时,结果为n * m % 2 == 0 ? n*m/2 : n*m/2+1;
(3) n == 2 || m == 2时,这里我就贪心的考虑了一下,没想到过了(假设n < m)
考虑 n == 2 && m == 1时,结果为2,n == 2 && 1 < m <= 4时结果为4。我们是否可以贪心的认为这就是最优 解,然后把原棋盘分成若干个n
== 2 && m == 4的棋盘 以及一个n == 2 && 1 <= m <= 3的棋盘?没错就是这样 最优情况下n == 2 && m == 4放4个,n == 2 && m == 1放2个,n == 2 && 1 < m <= 3放4个。统计下就好了
AC代码:
![]() | PDF (English) | Statistics | Forum |
Time Limit: 1 second(s) | Memory Limit: 32 MB |
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.
![](http://www.lightoj.com/data/problems/desc/1010/1010.png)
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*m的棋盘,现在给出马能攻击的8个位置,问最多放置多少个马使得任意两匹马之间不能相互攻击。
40000组测试数据 + 200 * 200的棋盘,匹配 状压 网络流 都不行吧。
那只能找规律了。
思路:
(1) n == 1 || m == 1时,结果显然就是max(n, m);
(2) n > 2 && m > 2时,结果为n * m % 2 == 0 ? n*m/2 : n*m/2+1;
(3) n == 2 || m == 2时,这里我就贪心的考虑了一下,没想到过了(假设n < m)
考虑 n == 2 && m == 1时,结果为2,n == 2 && 1 < m <= 4时结果为4。我们是否可以贪心的认为这就是最优 解,然后把原棋盘分成若干个n
== 2 && m == 4的棋盘 以及一个n == 2 && 1 <= m <= 3的棋盘?没错就是这样 最优情况下n == 2 && m == 4放4个,n == 2 && m == 1放2个,n == 2 && 1 < m <= 3放4个。统计下就好了
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f #define eps 1e-8 #define MAXN 500000+10 #define MAXM 50000000 #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long using namespace std; int main() { int t, kcase = 1; Ri(t); W(t) { int n, m; Ri(n), Ri(m); int ans; if(n == 1 || m == 1) ans = max(n, m); else if(n == 2 || m == 2) { int temp = max(n, m) % 4; ans = max(n, m); if(temp) { ans = max(n, m) / 4 * 4; if(temp == 1) ans += 2; else ans += 4; } } else ans = n*m % 2 == 0 ? n*m/2 : n*m/2+1; printf("Case %d: %d\n", kcase++, ans); } return 0; }
相关文章推荐
- 2015年MyEclipse使用教程最强盘点
- 【HNOI2014】【BZOJ3573】米特运输
- lightoj 1008 - Fibsieve`s Fantabulous Birthday 【数学 找规律】
- 一些关于git的基本使用方法
- 点9图片的处理
- SSD6 Exercise1 深入分析
- hdoj 5101 Select 【二分 + 思维】
- vim配置
- vim编辑器详解
- hdoj 5100 Chessboard 【思维】
- 偏执却管用的10条Java编程技巧
- Mysql审计
- CSDN中怎样让图片居中和首行缩进
- Add Binary
- ——黑马程序员——OC中self和super关键字的使用
- [温故而知新] 《深度探索c++对象模型》——构造函数
- leetcode之Best Time to Buy and Sell Stock
- (职员)2015-11-04 星期三 日志
- 06.Curator Barrier
- gdb调试器