예제 #1
0
파일: hamilto.c 프로젝트: sacOO7/sl2
int main (){
	int graph[20][20];
	printf ("Enter the number of vertices");
	scanf ("%d",&n);
	hamilton(graph);

}
예제 #2
0
/* test jestli graf je hamiltonovsky */
bool hamilton(int n, int graph[][n], int path[n], int position, int used[n])
{
    
    // pokud uz je cesta dlouha jako je pocet vrcholu a existuje prechod z posledniho bodu do prvniho tak konec, mame kruznici -- jinak jsme nasli cesty delky n, ale neni to kruznice.
    if (position == n) {
        
        if (graph[path[0]][path[position-1]] == 1) {
            //print_hamilton_cycle(n, path);    
            return true;
        }
        else {
            return false;   
        }
    }  

    for(int i = 0; i < n; i++) {
        // pokud existuje prechod z posledniho vrcholu cesty do vrcholu i a jeste jsem vrchol i nepouzil, tak ho pridam do cesty a pustim dalsi iteraci rekurze
        if(graph[path[position-1]][i] == 1 && used[i] == 0) {
            path[position] = i;
            used[i] = 1;
            if(hamilton(n, graph, path, position+1, used)) {
                return true;
            }
            // backtracking
            else {
                path[position] = -1;
                used[i] = 0;
            }
        }
    }
    
    return false; 
}
예제 #3
0
bool hamilton_test(int n, int graph[][n]) {
    // cesta
    int path[n];
    
    // pole s pouzitymi vrcholy (0 = nepouzity, 1 = pouzity)
    int used[n];
    int position = 0;
    for (int i = 0; i < n; i++) {
        used[i] = 0;
    }

    //zacnem ve vrcholu 0:

    position = 1;
    path[0] = 0;
    used[0] = 1;

    if(hamilton(n, graph, path, position, used)) {
        return true;
    }
    else {
        return false;
    }

}
//hdu 3414
int main() {
	while (scanf("%d", &n) && n) {
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++) {
				int x;
				scanf("%d", &x);
				g[i][j] = x;
			}
		if (n == 1) {
			printf("1\n");
			continue;
		}
		bool flag = false;
		for (int i = 0; i < n && !flag; i++) {
			if (hamilton(i)) {
				flag = true;
			}
		}
		if (!flag) puts("-1");
	}
	return 0;
}
예제 #5
0
int main()
{
    int x,y;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;
        n*=2;
        init();
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&x,&y);
            map[x][y]=0;
            map[y][x]=0;
        }
        hamilton();
        for(int i=0; i<n; i++)
        {
            if(i!=n-1)
            printf("%d ",ans[i]);
            else printf("%d\n",ans[i]);
        }
    }
}