20201218大一集训题j题国际象棋chess

24 篇文章 0 订阅
24 篇文章 0 订阅
该博客讨论了一个关于在给定的棋盘上放置骑士的问题,使得它们不会互相攻击。作者首先尝试通过找规律来解决,但发现需要考虑特殊情况。当棋盘的行数或列数为1或2时,存在特定的放置策略。对于其他情况,可以使用数学公式估算最大数量。最终,作者提供了正确的解决方案,并反思了问题解决过程中的思考和策略。
摘要由CSDN通过智能技术生成

题目:
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
3

8 8

3 7

4 10

Sample Output
Case 1: 32

Case 2: 11

Case 3: 20
翻译:
给你一个n x m的棋盘,你可以在上面放置骑士棋子。
你必须找到棋盘上可以放置的最大棋子数,这样没有两个骑士互相攻击。
规则如下;
在这里插入图片描述

ps:骑士。白点代表可以攻击的地方。
输入:
t代表输入的数组,有t个。
n,m代表输入棋盘的行列数。
输出:
巴拉巴拉
。。。。。。。。。。。
我的思路:
看到给的数据就想到找规律,发现输出的都是n*m/2的数,不是整数就加1
源代码:

#include<stdio.h>
#include<math.h>
int main()
{
	int n;
	scanf("%d",&n);
	int copy=n;
	while(n--){
		int a,b;
		scanf("%d%d",&a,&b);
		int t=ceil(a*b/2.0);
		printf("Case %d: %d\n",copy-n,t);
	}
	return 0;
}

结果错了,发现没有讨论特殊情况。
正确思路:
当n或m为1时,可以放m或n个;
当n货m等于2时,又可以找到一种规律:
第一行: O O X X O O X X O O X X [ ] [ ]
第二行: O O X X O O X X O O X X [ ] [ ];
也就是可以找到如上周期型的规律,周期为4;
换句话说,当列数为4的周期时可以,就为列数;
当不等于时就要分情况:
如果取余小于2,就n/4x4+2*(n/4x2);
如果大于等于2,就n/4x4+4;
就这样;
然后其他的情况都是以我的思路去算。
正确代码:

#include<cstdio>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
	int n;
	scanf("%d",&n);
	int copy=n;
	while(n--){
		int a,b,sum,_;
		scanf("%d%d",&a,&b);
		sum=a*b;
		if(a==1||b==1) _=sum;
		else if(a==2||b==2){
			if(a==2) swap(a,b);
			if(a%4<2) _=a/4*4+(a%4)*2;
			else if(b%4>=2) _=a/4*4+4; 
		} 
		else{
			_=ceil(a*b/2.0);
		}
		printf("Case %d: %d\n",copy-n,_);
	}
	return 0;
}

反思:
swap函数的理解与运用。

嗯,->o<(做题自闭后的我)

拓展:
找规律,加分情况的题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值