Pre-build and reuse travel maps. For example, build scity [] [] with a valid word distance that can be reused.
Just a quick job search exercise can be simplified.
#define SLEN 10 char* dict[SLEN]={ "bat", "hat", "bad", "had", "mad", "tad", "het", "hep", "hady", "bap"}; int minD=0xfffff; int edst(char *a, char *b) { char *ip=a,*op=b; int d=0; while((*ip)&&(*op)) if(*ip++!=*op++) { if(d) return 0; d++; } if((*op)||(*ip)) d++; return d; } int strlen(char *a) { char *ip=a; int i=0; while(*ip++) i++; return i; } int valid(char *dict[], int a, int b) { if((a==b)||(strlen(dict[a])!=strlen(dict[b]))||(edst(dict[a],dict[b])!=1)) return 0; return 1; } void sroute(int scity[SLEN][SLEN], char* dict[], int a[], int end, int pos) { int i,j,d=0; if(a[pos]==end) { for(i=pos;i<(SLEN-1);i++) { printf("%s ",dict[a[i]]); d+=scity[a[i]][a[i+1]]; } printf(" %s=%d\n",dict[a[SLEN-1]],d); if(d<minD) minD=d; return; } for(i=pos-2;i>=0;i--) { int b[SLEN]; for(j=0;j<SLEN;j++) b[j]=a[j]; b[pos-1]=a[i]; b[i]=a[pos-1]; if(scity[b[pos-1]][b[pos]]==1) sroute(scity,dict,b,end,pos-1); } if(scity[a[pos-1]][a[pos]]==1) sroute(scity,dict,a,end,pos-1); } void initS(int scity[SLEN][SLEN], char* dict[], int a, int b) { int i,j; int c[SLEN]; for(i=0;i<SLEN;i++) for(j=0;j<SLEN;j++) scity[i][j]=valid(dict,i,j); for(i=0;i<SLEN;i++) c[i]=i; c[SLEN-1]=b; c[b]=SLEN-1; sroute(scity, dict, c, a, SLEN-1); printf("min=%d\n",minD); }