20分求助
查看原帖
20分求助
900910
lsrsrl楼主2024/9/29 19:30
#define maxN 150
#include <bits/stdc++.h>
using namespace std;

int n;
int x, y;
char s[10];
char c1, c2;
char a[maxN];
bool isinput[maxN], Graphic[maxN][maxN], visit[maxN][maxN];
int now, tot;
void DFS(int x) {
	if (now == n) {
		for (int i = 1; i <= tot; i++)
			cout << a[i];
		exit(0);
		return ;
	}
	for (int i = 'A'; i <= 'z'; i++)
		if (Graphic[x][i] && !visit[x][i]) {
			now++;
			tot++;
			visit[x][i] = visit[i][x] = true;
			a[tot] = i;
		    DFS(i);
			now--;
			tot--;
			visit[x][i] = visit[i][x] = false;;
		}
}
int main() {
	//freopen("letter.in", "r", stdin);
	//freopen("letter.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		scanf ("%s", s);
		c1 = s[0], c2 = s[1];
		isinput[c1] = isinput[c2] = true;
		Graphic[c1][c2] = Graphic[c2][c1] = true;
	}
	for (int i = 'A'; i <= 'z'; i++)
		if (isinput[i]) {a[++tot] = i;DFS(i); break;}
	cout << "No Solution";
	return 0;
}
2024/9/29 19:30
加载中...