1010 - Knights in Chessboard
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.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.
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
|
38 83 74 10 |
Case 1: 32Case 2: 11Case 3: 20 |
题目类型:找规律
算法分析:维持row <= col,然后当row == 1时,直接输出col。当row == 2时,那么可以一次性把一个田字格全部放上马,然后间隔一个田字格,然后再放马。当row >= 3时,则直接判断棋盘上黑白格最大的那一个的个数即可(因为同一个颜色的格子中的马不会相互攻击)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 66666; int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int t, flag = 1; scanf ("%d", &t); while (t--) { printf ("Case %d: ", flag++); int row, col; scanf ("%d%d", &row, &col); if (row > col) swap (row, col); if (row == 1) { printf ("%d\n", col); } else if (row == 2) { int sum = 4 * (col / 4); int temp = col % 4; if (temp < 3) sum += temp * 2; else sum += 4; printf ("%d\n", sum); } else { int tot = row * col; if (tot % 2) printf ("%d\n", tot / 2 + 1); else printf ("%d\n", tot / 2); } } return 0; } |