Mathematica 116 114 byte
Dengan beberapa byte tersimpan berkat Misha Lavrov.
Last@FindPath[Graph[Rule@@@Cases[Tuples[Tuples[{0,1},{l=Length@#}],{2}],x_/;Count[Plus@@x,1]==1]],##,{1,2^l},Alll]&
Input (8 dimensi)
[{1,0,0,1,0,0,0,1},{1,1,0,0,0,0,1,1}]//AbsoluteTiming
Output (panjang = 254, setelah 1,82 detik)
{1.82393, {{1, 0, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0}, {0, 0,0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1, 1, 1}, {0, 0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 0,1, 1, 1,0}, {0, 0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 1}, {0, 0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 0,1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 0, 1}, {0, 0, 0, 0, 1, 1, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0}, {0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 1,0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 1, 1}, {0, 0, 0, 1, 0, 1, 1, 1}, {0, 0, 0, 1, 0, 1, 0, 1}, {0, 0, 0, 1, 1, 1, 0, 1}, {0, 0, 0, 1, 1, 0, 0, 1}, {0, 0, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 0, 1, 0}, {0, 0, 0, 1, 1, 0, 1, 1}, {0, 0, 0, 1,1, 1, 1, 1}, {0, 0, 0, 1, 1, 1, 1, 0}, {0, 0, 0, 1, 0, 1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 1, 1, 0, 0, 0, 0}, {1, 1, 1, 1,0, 0, 0, 1}, {1, 1, 1, 1, 1, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 0}, {1, 1, 1, 1,1, 0, 1, 0}, {1, 1, 1, 1, 1, 0, 0, 0}, {1, 0, 1, 1, 1, 0, 0, 0}, {1, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 1,1, 0, 1, 1}, {1, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 1, 0, 0, 0, 1, 1}, {1, 1, 0, 0, 0, 0, 1, 1}}}
Tuples[{0,1},{l=Length@#}],{2}]
& menghasilkan angka 0 ... 8 sebagai daftar biner.
Bagian luar Tuples...{2}
menghasilkan semua pasangan nomor biner yang dipesan.
Plus@@x
jumlah masing-masing pasangan, menghasilkan kembar tiga dari 0, 1.
Cases....Count[Plus@@x, 1]==1
mengembalikan semua jumlah yang mengandung satu 1. Ini muncul ketika dua angka biner asli dihubungkan oleh satu sisi.
Rules
menghubungkan simpul grafik, setiap simpul menjadi angka biner.
Graph
membuat grafik yang sesuai dengan simpul dan tepi tersebut.
FindPath
menemukan hingga 2 ^ n jalur yang menghubungkan titik a ke titik b, angka yang diberikan.
Last
mengambil jalan terpanjang dari ini.
Untuk tiga dimensi, grafik dapat direpresentasikan dalam bidang seperti yang ditunjukkan di sini:
Untuk input, {0,0,0}, {1,1,1}
berikut ini adalah output:
{{{0, 0, 0}, {0, 0, 1}, {0, 1, 1}, {0, 1, 0}, {1, 1, 0}, {1, 0,
0}, {1, 0, 1}, {1, 1, 1}}}
Jalur ini dapat ditemukan dalam grafik di atas.
Itu juga dapat dipahami sebagai jalur berikut dalam 3-ruang, di mana setiap titik berhubungan dengan suatu titik {x,y,z}
. {0,0,0} mewakili asal dan {1,1,1} mewakili titik "berlawanan" dalam satuan kubus.
Jadi jalur solusi sesuai dengan traverse tepi di sepanjang unit kubus. Dalam hal ini, jalannya adalah Hamiltonian: ia mengunjungi setiap simpul satu kali (yaitu tanpa penyeberangan dan tidak ada simpul dihilangkan).