跳到正文

题解

B3755 [信息与未来 2019] 方格覆盖

#SUSPENDED#约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月24日
篇幅
约 1 分钟
题目
B3755

版本

  1. v1当前

思路

贪心选择每个小矩形的放置位,如果可以横着放,尽量横着,如果横着不行,尝试竖着放。

代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int k;
int a[51][51];
int cnt;
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=k;i++)a[i][i]=-1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(a[i][j]!=0)continue;
			if(a[i][j+1]==0&&j!=n){
				cnt++;
				a[i][j]=cnt;
				a[i][j+1]=cnt;
			}else if(a[i+1][j]==0&&i!=n){
				cnt++;
				a[i][j]=cnt;
				a[i+1][j]=cnt;
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(a[i][j]==-1)cout<<0<<' ';
			else cout<<a[i][j]<<' ';
		}
		cout<<endl;
	}
	return 0;
}
原文