递归一遍遍历所有情况就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;
}

相关文章:

  • 2021-09-21
  • 2021-11-25
  • 2022-01-17
  • 2021-09-03
  • 2021-07-13
  • 2021-12-21
  • 2021-05-28
  • 2022-02-07
猜你喜欢
  • 2021-05-28
  • 2022-01-10
  • 2022-12-23
  • 2021-10-13
  • 2021-12-07
  • 2022-01-29
  • 2022-12-23
相关资源
相似解决方案