lightoj 1010 找规律坑比题

该博客讨论了如何在m x n的棋盘上放置国际象棋骑士,使得没有两个骑士互相攻击的问题。博主分享了解题思路,包括寻找规律、特定情况的处理,如一排的情况和n或m为2的特殊情况,并提到了使用特定算法避免超时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

规律好找
具体的找法是一开始瞎画
后来发现了被攻击的点被占得次数越多越好
然后左上角那个点不论何时占住都很好
然后就有了这样的策略
从左上角的点找到不能走的点,从这个不能走的点延伸出所有要走的点
再把不能走的划掉
直到全图都有标记了为止
….
然后应该就是隔一个画一次

好了坑点来了

需要注意这么几种特殊情况
一种是一排的
这种全部都是满的
第二种是n或者m为2的
他可以一个田字格一个空田字格。。
这样最小化也是1/2
这就很坑很烦
很蓝受

虽然数据不大
但是cin的话会TLE

坑!

#include<iostream>
#include<cstdio>
#include<memory.h>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
    int T;
    cin>>T;
    int u=0;
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==1||m==1)
        {
            printf("Case %d: %d\n",++u,n*m);
            continue;
        }
        if(n<=2&&m<=2)
        {
            printf("Case %d: %d\n",++u,n*m);
            continue;
        }
        if(n==2)
        {
            int qq=m/2;
            if(qq%2==0)
            {
                printf("Case %d: %d\n",++u,2*qq+m*n-4*qq);
                continue;
            }
            else
            {
                printf("Case %d: %d\n",++u,4*((qq/2)+1));
                continue;
            }
        }
        if(m==2)
        {
            int qq=n/2;
            if(qq%2==0)
            {
                printf("Case %d: %d\n",++u,2*qq+m*n-4*qq);
                continue;
            }
            else
            {
                printf("Case %d: %d\n",++u,4*((qq/2)+1));
                continue;
            }
        }
        if(n%2==0||m%2==0)printf("Case %d: %d\n",++u,n*m/2);
        else printf("Case %d: %d\n",++u,(n*m/2)+1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值