hdu 2181 深搜
题意:十二面体有20个顶点,每个顶点和相邻3个顶点相连,问从一个顶点出发,每个顶点只走一遍,回到原点的走法。如果有多条,按字典序输出。
分析:将每个顶点的相邻点排序,然后简单深搜。。。
int a[21][3]; int m, cnt; int p[21], b[21]; void d(int i, int u){//第i步为顶点u if(i==19){ FOR(j, 0, 3) if(a[u][j] == m){ printf("%d: ", ++cnt); FOE(k, 0, 20) printf(" %d", p[k]); printf("\n"); } return; } FOR(j, 0, 3){ int v = a[u][j]; if(b[v]) continue; b[v] = 1; p[i+1] = v; d(i+1, v); b[v] = 0; } } int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif FOE(i, 1, 20) { FOR(j, 0, 3) scanf("%d", &a[i][j]); sort(a[i], a[i]+3); } while(scanf("%d", &m), m){ memset(b, 0, sizeof b); cnt = 0; b[m] = 1; p[0] = m; p[20] = m; d(0, m); } return 0; }