void AlienDictionary::processGraph() { for (int i=0; i<words.size(); i++) { processHorizontal(i); } processVertical(0, 0, words.size()-1); }
void AlienDictionary::processVertical(int front, int rowBegin, int rowEnd) { if (rowBegin >= rowEnd) return; if (words[rowBegin].size() <= front) { processVertical(front, rowBegin+1, rowEnd); return; } int rstart = rowBegin; for (int rctr=rowBegin+1; rctr<=rowEnd; rctr++) { if (words[rctr][front] != words[rstart][front]) { processVertical(front+1, rstart, rctr-1); addDependency(words[rstart][front], words[rctr][front]); rstart = rctr; } } processVertical(front+1, rstart, rowEnd); }
int main (int argc, char * argv[]) { printf ("program to solve the word-find puzzle problem\n"); if (argc != 2) { printf ("Run-time error: include file name on command-line\n"); return 1; } /* open file for input */ printf ("input file for data: %s\n", argv[1]); FILE * inFile = fopen (argv[1], "r"); if (inFile == NULL) { printf ("Run-time error: file not found\n"); return 2; } /* load input table from file */ char inGrid[gridSize][2*gridSize+1]; /* each row is a null-terminated string */ int i, j; /* indices for table */ for (i = 0; i < gridSize; i++) { /* table contains both letters and separating spaces */ fgets (inGrid[i], 2*gridSize+1, inFile); } /* initialize output grid to spaces */ char outGrid[gridSize][2*gridSize]; /* each row is a null-terminated string */ for (i = 0; i < gridSize; i++) { for (j = 0; j < 2*gridSize-1; j++) { outGrid[i][j] = ' '; /* rows are spaces, except */ } outGrid [i][2*gridSize-1] = 0; /* last character in row is null */ } /* process successive words in file */ char line [gridSize]; /* words cannot be larger than grid size */ /* read entire line until EOF */ while (fgets(line, gridSize, inFile) != NULL) { /* since fgets includes '\n' in line, remove white space at end */ int k = strlen(line)-1; while (!isalpha(line[k])) { line[k] = 0; /* shorten string with null over whitespace */ k--; } printf ("processing string: %s\n", line); if (!processHorizontal (inGrid, outGrid, line)) processVertical (inGrid, outGrid, line); } /* print resulting grid */ for (i = 0; i < gridSize; i++) { printf ("%s\n", outGrid[i]); } return 0; }