#include #include #include #include #include #include #include #include #include #include #define INFINITE 0 #define MULTIPLE 0 #if INFINITE #define DBG2 #endif #if INFINIT || MULTIPLE #define DBG3 #endif #define MAX_CH 200 #define CHAR_SET ('z'-'a'+1) char kbd_init[CHAR_SET+1] = ""; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 int x_cor[CHAR_SET] = {85,123,161,199,237,275,313,351,389,427,95,133,171,209,247,285,324,362,400,122,160,198,236,275,313,351}; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 int y_cor[CHAR_SET] = {0,0,0,0,0,0,0,0,0,0,39,39,39,39,39,39,39,39,39,79,79,79,79,79,79,79}; #define get_x(a) x_cor[a] #define get_y(a) y_cor[a] // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 int priority[CHAR_SET] = {0,1,10,11,2,12,19,20,3,13,21,4 ,14,22,5, 15,23,6, 16,24,7, 17,25,8, 18,9}; //int priority[CHAR_SET] = {12,13,3,2,11,20,21,22,4,14,1 ,19,10,0 ,5, 23,15,6, 24,16,7, 25,17,8, 18,9}; int dist[CHAR_SET][CHAR_SET]; //int pairs[CHAR_SET][CHAR_SET]; int inch_cnt = 0; int inch_dif = 0; char inch[MAX_CH+1] = ""; void read_file() { FILE *fh; fh = fopen("keyboard.in","r"); if (!fh) { printf("Err opening input file !\n"); exit(1); } else { fscanf(fh,"%d",&inch_cnt); fscanf(fh,"%s",inch); inch[inch_cnt] = 0; fclose(fh); #ifdef DBG printf("%d\n%s\n",inch_cnt,inch); #endif } } inline int distance(int pos1, int pos2) { int x1 = get_x(pos1); int y1 = get_y(pos1); int x2 = get_x(pos2); int y2 = get_y(pos2); if (x1==x2) return abs(y1-y2); else if (y1==y2) return abs(x1-x2); int dx = x1-x2; int dy = y1-y2; return sqrt((double)(dx*dx + dy*dy))+0.5; } void misc_calcs() { //--------------------------------------------- for (int p1=0;p1= get_x(fr)) { d2 = dist[fr][cp]; sum += d2; fr = cp; } else { d1 = dist[fl][cp]; d2 = dist[fr][cp]; if (d1<=d2) { sum += d1; fl = cp; } else { sum += d2; fr = cp; } } } return sum; } void kbd_shuffle(char *chset, int *chidx) { char rnd_src[CHAR_SET+1]; int n = inch_dif; memcpy(rnd_src, kbd_init, n); for (int i=0;i=get_x(rfp)) continue; int L_start = lfp; int R_start = rfp; int res = run(L_start, R_start, inch, chidx); if (res %d (%d)\n",l_min,r_min,res_min,cnt); #endif } } //------------------------------------------- int rfp = chidx[kbd_init[0]-'a']; for (int lf=1; lf=get_x(rfp)) continue; int L_start = lfp; int R_start = rfp; int res = run(L_start, R_start, inch, chidx); if (res %d (%d)\n",l_min,r_min,res_min,cnt); #endif } } //------------------------------------------- time(&t2); seconds = difftime(t2,t1); cnt++; } while (INFINITE || seconds<=2.95); #ifdef DBG3 printf("Time: %.3f s, Cnt: %d, Min: %d\n",seconds,cnt,res_min); printf("%s\n%d %d\n", chset_min, l_min+1, r_min+1); #endif } while (MULTIPLE); FILE *fw = fopen("keyboard.out","w"); if (!fw) printf("Err opening output file !\n"); else { fprintf(fw,"%s\n%d %d\n", chset_min, l_min+1, r_min+1); fclose(fw); } return 0; }