题目连接:哈密顿绕行世界问题
解题思路:非常裸的深搜,记录好访问过的点就可以了。
#include<stdio.h>
#include<string.h>
int map[25][3], v[25], ans[25], m, tot;
void print(){
int i;
printf("%d: ", tot++);
for(i = 0; i < 21; i++){
printf(" %d", ans[i]);
}
printf("\n");
}
void DFS(int depth){
int i, pre;
pre = ans[depth - 1];
if(depth > 20){
return;
}
for(i = 0; i < 3; i++){
if(!v[map[pre][i]]){
ans[depth] = map[pre][i];
v[map[pre][i]] = 1;
if(depth == 20 && ans[20] == ans[0]){
print();
}
else{
DFS(depth + 1);
}
v[map[pre][i]] = 0;
}
}
}
int main(){
int i, j, k;
for(i = 1; i <= 20; i++){
for(j = 0; j < 3; j++){
scanf("%d", &map[i][j]);
}
}
while(scanf("%d", &m) && m){
tot = 1;
memset(v, 0, sizeof(v));
ans[0] = m;
DFS(1);
}
return 0;
}