Seeing Next_permutation seems to be able to ╮(╯▽╰)╭.
The learning point of this question:
1. Map mapping
2. Guaranteed minimum dictionary order by sequencing enumeration
3.STRTOK,SSCANF,STRCHR and other functions have been reviewed again, although the program is not actually used
4. Pruning, or backtracking
#include <bits/stdc++.h>using namespacestd;intg[8][8],deg[8];BOOLvg[8][8];//Interpreting Connectivityintpos[8];BOOLvis[8];intK;intbest[8];intCNT;intid[ -];Charrev_id[8];voidDfsintDintwidth) { if(d = =CNT) { if(Width <k) {k=width; memcpy (Best,pos,sizeof(POS)); } return; } for(inti =0; i < CNT; i++)if(!vis[i]) {//Put I in D positionPOS[D] =i; //Prune calculate the maximum distance between I and the node of the previously determined position intm =0; for(intj = d1; J >=0; j--)if(Vg[i][pos[j]]) {m= Max (m,d-j); if(M >= k)Continue;//The data is water, this is a return or can } //Prune2 calculates the number of adjacent points for u and indeterminate positions, for simple graphs intCT =0; for(intj =0; J < Deg[i]; J + +)if(!vis[g[i][j]]) ct++; if(CT >= k)Continue; Vis[i]=1; DFS (d+1, Max (width,m)); Vis[i]=0; }}intMain () {//freopen ("In.txt", "R", stdin); Charbuf[ -]; Const intINF =0x3fffffff; while(Fgets (BUF, -, stdin) && *buf!='#') {CNT=0; memset (VG,0,sizeof(VG)); memset (deg,0,sizeof(deg)); memset (ID,-1,sizeof(ID)); memset (Vis,0,sizeof(VIS)); BOOLap[ -] ; Memset (AP,0,sizeof(AP)); for(Char*cur = BUF; *cur; cur++) { intU = *cur-'A'; if(0<= u && u < -) Ap[u] =1; } for(inti =0; I < -; i++){ if(Ap[i]) {rev_id[cnt]= i+'A'; Id[i]= cnt++; } } for(Char*cur = BUF; *cur; cur++) { intU = id[*cur-'A']; for(cur+=2; *cur! ='\ n'&& *cur! =';'; cur++) { intv = id[*cur-'A']; VG[U][V]= Vg[v][u] =1; } } for(inti =0; I < CNT; i++) for(intj =0; J < CNT; J + +){ if(Vg[i][j]) g[i][deg[i]++] =J; } k=INF; DFS (0,0); for(inti =0; I < CNT; i++) {printf ("%c", Rev_id[best[i]]); } printf ("%d\n", K); } return 0;}View Code
UVA brandwidth[Pruning]