hdu2181-哈密顿绕行世界

来源:互联网 发布:刘嘉玲网络云盘 编辑:程序博客网 时间:2024/04/30 00:18
#include<stdio.h>#include<algorithm>#include<math.h>#include<string.h>#include<queue>#define maxn 21#define INF 0xfffffffusing namespace std;bool map[maxn][maxn];bool used[maxn];int res[maxn];int num;int cas;void init(){    int a, b, c, i;    for (i = 1; i <= 20; i++) {        scanf("%d%d%d", &a, &b, &c);        map[i][a] = true;        map[i][b] = true;        map[i][c] = true;    }}void dfs(int dep, int count){    int i, j;    res[count] = dep;    if (count == 19) {        if (map[dep][cas]) {            printf("%d:  ", ++num);            for (i = 0; i < 20; ++i)                printf("%d ", res[i]);            printf("%d\n", res[0]);        }        return;    }    for (j = 1; j <= 20; j++) {        if (map[dep][j] && !used[j]) {            used[j] = true;            dfs(j, count + 1);            used[j] = false;        }    }}int main(){    memset(map, 0, sizeof(map));    init();    while (scanf("%d",&cas),cas)    {        num = 0;        memset(used, false, sizeof(used));        memset(res, 0, sizeof(res));        used[cas] = true;        dfs(cas, 0);    }    return 0;}
0 0