跳到正文

题解

题解:AT_abc345_d [ABC345D] Tiling

kevinZ99约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟
获赞
1

版本

  1. v1当前

题意

给你一个 h×wh \times w 的矩阵。

在给你 nn 个瓷砖,第 ii 个瓷砖的大小为 ai×bia_i \times b_i。

然你选择一些瓷砖来填满,h×wh \times w 的地板。

瓷砖可以翻转。

思路

这么小的数据范围明显 dfs 暴力即可。

dfs 搜索:按照顺序尝试,类似于添数独的方式,按照从左上到右下的方式填,即可。

代码

因为是用赛时的代码改的所以比较乱

C++
/*
  Author:Kevin Z K Y
  t_limit:
  m_limit:
*/
//mod 998244353 \ 1e9+7 \ 1e9+9 \ 19260817 \ 104857601
#include <bits/stdc++.h>
#define up(a,b,c) for(int (a)=(b);(a)<=(c);(a)++)
#define dn(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
using namespace std;
using pii = pair<int,int> ;
namespace my{
	#define fst first
	#define sed second
	const int N=10;
	string ot;
	pii a[N];
	bool vis[15][15];
	bool used[N];
	int n,h,w;
	void dfs(int have){
		if(have==h*w){
			cout<<"Yes\n";
			exit(0);
		}
		up(choose,1,n){
			if(used[choose])continue;
			bool in=false;
			up(x,1,h){
				up(y,1,w){
					if(vis[x][y])continue;
					if(x+a[choose].fst-1<=h&&y+a[choose].sed-1<=w){
						bool can=false;
						up(i,x,x+a[choose].fst-1){
							up(j,y,y+a[choose].sed-1)
								if(vis[i][j]){
									can=true;
									break;
								}
							if(can)break;
						}
						if(!can){
							in=true;
							up(i,x,x+a[choose].fst-1)up(j,y,y+a[choose].sed-1)
								vis[i][j]=true;
							used[choose]=true;
							dfs(have+a[choose].fst*a[choose].sed);
							used[choose]=false;
							up(i,x,x+a[choose].fst-1)up(j,y,y+a[choose].sed-1)
								vis[i][j]=false;
						}
					}
					if(x+a[choose].sed-1<=h&&y+a[choose].fst-1<=w){
						bool can=false;
						up(i,x,x+a[choose].sed-1){
							up(j,y,y+a[choose].fst-1)
								if(vis[i][j]){
									can=true;
									break;
								}
							if(can)break;
						}
						if(!can){
							in=true;
							up(i,x,x+a[choose].sed-1)up(j,y,y+a[choose].fst-1)
								vis[i][j]=true;
							used[choose]=true;
							dfs(have+a[choose].fst*a[choose].sed);
							used[choose]=false;
							up(i,x,x+a[choose].sed-1)up(j,y,y+a[choose].fst-1)
								vis[i][j]=false;
						}
					}
					if(in)break;
				}
				if(in)break;
			}
		}
	}
	bool flag=false;
	void judge(int x,int s){
		if(flag)return ;
		if(s==h*w){
			flag=true;
			return ;
		}
		if(x>n)return ;
		judge(x+1,s);
		judge(x+1,s+a[x].fst*a[x].sed);
		return ;
	}
	void solve(){
		srand(time(0));
		cin>>n>>h>>w;
		int sum=0;
		up(i,1,n)cin>>a[i].fst>>a[i].sed,sum+=a[i].fst*a[i].sed;
		if(sum<h*w){
			cout<<"No\n";
			return ;
		}
		judge(1,0);
		if(!flag){
			cout<<"No\n";
			return ;
		}
		dfs(0);
		cout<<"No\n";
	}
	#undef fst
	#undef sed
}
int main(){
//  freopen("","r",stdin);
//  freopen("","w",stdout);
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	int _=1;
	while(_--)my::solve();
	return 0;
}
原文