璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:usaco22feb_blocks_b
problems
名称Blocks B
题目编号USACO22FEB_B3
题目链接luogu.com.cn/…
来源USACO
算法分类DFS, DFS_枚举
难易程度容易

Blocks B

想法

使用DFS去枚举每个位置的放置情况,属于指数枚举类型。

代码实现

#include<iostream>
#include<cstring>
using namespace std;
 
bool maps[4][26], vis[4], flag;
char line[10];
int len;
 
void DFS(int x)
{
	if(x>len)
	{
		flag = true;
		return ;
	}
	for (int i = 0; i < 4; ++i) {
		if(vis[i])continue;
		if(!maps[i][line[x] - 'A'])continue;
		vis[i] = true;
		DFS(x+1);
		vis[i] = false;
		if(flag) return ;
	}
 
	return ;
}
 
int main()
{
	int T;
	cin >> T;
	for (int i = 0; i < 4; ++i) {
		char temp;
		for (int j = 0; j < 6; ++j) {
			cin >> temp;
			maps[i][ temp-'A' ] = true;
		}
	}
	while(T--)
	{
		memset(vis, false, sizeof(vis));
		flag = false;
		cin >> line;
		len = strlen(line)-1;
		DFS(0);
		if(flag)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
 
	return 0;
}
/app/www/public/data/pages/icpc/problems/usaco22feb_blocks_b.txt · 最后更改: 2023/02/08 07:42 由 温婕莺