Pagini recente » Cod sursa (job #2899474) | Cod sursa (job #804165) | Cod sursa (job #1383303) | Cod sursa (job #3199976) | Cod sursa (job #70072)
Cod sursa(job #70072)
#include <fstream>
using namespace std;
int n, nn, k = 0;
int l[1048576][3], st[1048576];
void citire() {
ifstream in("biti.in");
in >> n;
}
void gen_adiac(){
for (int x = 0; x < nn; x++ ){
l[x][0] = -1;
l[x][1] = -1;
}
for (int x = 0; x < nn; x++ ) {
l[x][0] = (x << 1) & (nn - 1);
l[x][1] = ((x << 1) & (nn - 1)) | 1;
}
}
void afisare_liste() {
ofstream out("biti.out");
out<< n - 1 + nn << endl;
for (int i = n - 1; i>=0; i--)
out<< ((st[0] >> i) & 1);
for(int i = 1; i < nn; i++)
out << (st[i] & 1);
out << endl;
out.close();
exit(0);
}
void ham() {
if (!l[l[st[k-1]][0]][2] && l[st[k-1]][0] >=0) {
int aux = l[st[k-1]][0];
l[l[st[k-1]][0]][2] = 1;
l[st[k-1]][0] = -1;
st[k] = aux;
k++;
if (k == nn)
afisare_liste();
ham();
k--;
l[st[k-1]][0] = aux;
l[l[st[k-1]][0]][2] = 0;
}
if (!l[l[st[k-1]][1]][2] && l[st[k-1]][1] >=0) {
int aux = l[st[k-1]][1];
l[l[st[k-1]][1]][2] = 1;
l[st[k-1]][1] = -1;
st[k] = aux;
k++;
if (k == nn)
afisare_liste();
ham();
k--;
l[st[k-1]][1] = aux;
l[l[st[k-1]][1]][2] = 0;
}
}
int main() {
citire();
nn = (1 <<n);
gen_adiac();
st[k++] = 0;
l[0][2] = 1;
ham();
return 0;
}