//This function finds all words on the board, by recursively building strings //it takes as input the string being built, the new board to be changed, //the row, the column, the score, and the count of words found //It is used in the computer solving part void Board::checkAllWords(string newWord, char newBoard[MAX_ROWS][MAX_COLUMNS], int r, int c, int &score, int &count) { char newArray[MAX_ROWS][MAX_COLUMNS]; //build the string newWord = newWord + newBoard[r][c]; //check for the q case if (newBoard[r][c] == 'q') { newWord = newWord + 'u'; } //check that the new word is a prefix bool check = dictionary.isPrefix(newWord, 0, dictionary.size() -1); if (check == false) { return; } //build a new board, to be changed for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++){ newArray[i][j] = newBoard[i][j]; } } //avoid backtracking by changing the current space to a blank character newArray[r][c] = ' '; //check that the row and column are on the board if (r < 0 || r >= numRows || c < 0 || c >= numCols) { return; } //if the word is of valid length, search the dictionary for it if (newWord.length() >= 3) { bool check1 = dictionary.binarySearch(newWord, 0, dictionary.size() - 1); if (check1 == true) { //make sure the word hasn't already been found bool check2 = binarySearchVector(newWord, 0, wordsFound.size() - 1, wordsFound); if (check2 == false) { wordsFound.push_back(newWord); sort(wordsFound.begin(), wordsFound.end()); score += wordScore(newWord); count += 1; } } } //for all 8 directions, recursively call the function and build strings for (int dx = -1; dx < 2; dx++){ for (int dy = -1; dy < 2; dy++) { if (dx == 0 && dy == 0) { continue; } if (r + dx < 0 || r + dx >= numRows || c + dy < 0 || c + dy >= numCols) { continue; } checkAllWords(newWord, newArray, r + dx, c + dy, score, count); } } return; }
int main() { printf("Project Euler - Problem 22:\n" "Sort 5000 names and calculate the sum of their name scores.\n\n"); // Begin time tracking struct timeval start; gettimeofday(&start, NULL); char names[6000][maxNameSize];// = {}; int nameIndex = 0; nameFile = fopen ("problem_022.txt", "rt"); // Read in names to array int ch; int charIndex = 0; while((ch = fgetc(nameFile))) { if (ch == EOF) break; if (ch == '"') continue; if (ch == ',') { nameIndex++; charIndex = 0; continue; } names[nameIndex][charIndex++] = ch; } fclose(nameFile); // Sort array qsort(names, nameIndex+1, sizeof(names[0]), compare); // Score names int score = 0; for(int i=0; i <= nameIndex; i++) score += (i + 1) * wordScore(names[i], maxNameSize); printf("Total name score: %d\n", score); printElapsedTime(start); return 0; }
int problem22() { std::vector<std::string> words; std::ifstream ifs("names.txt"); std::string line; while (std::getline(ifs, line, ',')) words.push_back(line); std::sort(words.begin(), words.end()); int score = 0; for (size_t i = 0; i < words.size(); ++i) score += wordScore(words[i], i+1); return score; }
int main(void) { assert(wordScore("\"COLIN\"", 938) == 49714); std::cout << problem22() << std::endl; }