紫书 例题 10-8 UVa 1262 (暴力枚举)

递归一遍遍历所有情况就ok了

#include<cstdio>
#include<cstring>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 10;
char map[2][10][10], str[10];
int k, num;

bool dfs(int pos)
{
	if(pos == 5) return ++num == k;
	
	bool vis[2][26];
	memset(vis, 0, sizeof(vis));
	REP(i, 0, 2)
		REP(j, 0, 6)
			vis[i][map[i][j][pos] - 'A'] = true;
	
	REP(i, 0, 26)
		if(vis[0][i] && vis[1][i])
		{
			str[pos] = 'A' + i;
			if(dfs(pos + 1)) return true;
		}
	return false;
}

int main()
{
	int T;
	scanf("%d", &T);
	
	while(T--)
	{
		scanf("%d", &k);
		REP(i, 0, 2)
			REP(j, 0, 6) 
				scanf("%s", map[i][j]);

		num = 0;
		if(dfs(0)) puts(str);
		else puts("NO");
	} 
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34416123/article/details/81061843