hdoj5630 Rikka with Chess

题目描述:

Problem Description
Yuta gives Rikka a chess board of size n×m
.As we all know, on a chess board, every cell is either black or white and every two cells that share a side have different colors.Rikka can choose any rectangle formed by board squares and perform an inversion, every white cell becomes black, and vice versa.Rikka wants to turn all cells into the same color, please tell Rikka the minimal number of inversions she need to achieve her goal.
Input
The first line contains a number T(T≤10)
——The number of the testcases.Each testcase contains two numbers n,m(n≤109,m≤109)
Output
For each testcase, print a single number which represents the answer.
Sample Input
3
1 2
2 2
3 3
Sample Output
1
2
2

此题是说有一个n*m的棋盘,棋盘黑白相间(相邻格子的颜色不相同),每次可以使一行或一列的格子的颜色翻转(黑变白,白变黑),问至少多少次可以使棋盘上的格子全为一种颜色。

此题可以通过画图找到规律。

参考代码:

#include <iostream>
using namespace std;
int min_chess(int &n,int &m) {
    int min;
    min = n / 2 + m / 2;
    return min;
}
int main() {
    int t;
    int n,m;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        int min = min_chess(n,m);
        cout << min << endl;
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容