#include <bits/stdc++.h>
using namespace std;
int n, k, cnt, S[100000], L;
int dfs(int cur)
{
if (cnt++ == n)
{
for (int i = 0; i < cur; i++)
{
cout << char('A' + S[i]);
if ((i + 1) % 64 == 0 && i != cur - 1) cout << endl;
else if ((i + 1) % 4 == 0 && i != cur - 1) cout << " ";
}
cout << endl;
cout << cur << endl;
return 0;
}
else for (int i = 0; i < L; i++)
{
S[cur] = i;
int ok = 1;
for (int j = 1; j * 2 <= cur + 1; j++)
{
bool equal = 1;
for (int k = 0; k < j; k++)
if (S[cur - k] != S[cur - k - j])
{equal = 0; break;}
if (equal) {ok = 0; break;}
}
if (ok) if (!dfs(cur + 1)) return 0;
}
return 1;
}
int main(int argc, char const *argv[])
{
while (cin >> n >> L)
{
if (n == 0 && L == 0) break;
cnt = 0;
memset(S, 0, sizeof(S));
dfs(0);
}
return 0;
}
uva 129 回溯
最新推荐文章于 2018-08-09 16:15:07 发布