-1

很抱歉用一个简单的问题打扰大家,但我对 C++ 很陌生,我不知道该怎么做。因此,我得到了一个 C++ 框架,它读取 2 个包含 wordsearch 和要找到的单词列表的 txt 文件,并将结果显示在另一个 txt 文件中。我正在尝试完成框架,但我无法弄清楚程序将如何检查所有 8 个方向,或者系统如何将字典文件中的单词与 wordsearch 进行比较,并将任何匹配的单词放入 results.txt 文件中。我附上了下面的框架,任何关于如何开始的信息或建议将不胜感激。

dictionary.txt 
ABSEIL
BIKE
PIXEL
RESIGHTS
YARDWORKS

wordsearch.txt 
S T H G I S E R B
G K L L B X D I E
K P R H I M K L D
T G Y O L E X I P
B A P T W H T E J
T Q U D X D W S F
V M V S H L R B A
H Q L B C K S A Y
R D G B F J P Q Y


Wordsearch.h 
#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <Windows.h>

using namespace std;

class WordSearch
{
 public:
WordSearch();
~WordSearch();

bool ReadPuzzle();
bool ReadDictionary();
bool SolvePuzzleWithDictionary();

void WriteResults(string fileName);

private:
LARGE_INTEGER start, end, frequency;
const int NUMBER_OF_RUNS;
const string PUZZLE_NAME;
const string DICTIONARY_NAME;
};


Wordsearch.cpp 
 #include "WordSearch.h"
 #include <algorithm>
 #include <iterator>
 #include <iostream>
 #include <iomanip>

using namespace std;

WordSearch::WordSearch() : NUMBER_OF_RUNS(500), PUZZLE_NAME("wordsearch_grid.txt"), DICTIONARY_NAME("dictionary.txt")
{
}

WordSearch::~WordSearch()
{
}

bool WordSearch::ReadPuzzle()
{
cout << endl << "ReadPuzzle() has NOT been implemented" << endl;
return true;
}

bool WordSearch::ReadDictionary()
{
cout << endl << "ReadDictionary() has NOT been implemented" << endl;
return true;
}

bool WordSearch::SolvePuzzleWithDictionary() {
cout << endl << "SolvePuzzleWithDictionary() has NOT been implemented" << endl;
double timeTakenInSeconds;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start);

for (int n = 0; n < NUMBER_OF_RUNS; ++n) {
    // Add solving code here!
}

QueryPerformanceCounter(&end);
timeTakenInSeconds = (end.QuadPart - start.QuadPart) / (double)(frequency.QuadPart*NUMBER_OF_RUNS);

cout << fixed << setprecision(10) << "SolvePuzzleWithDictionary() - " << timeTakenInSeconds << " seconds" << endl;
return false;
}

void WordSearch::WriteResults(string fileName) {
cout << "WriteResults() has NOT been implemented" << endl;
}
4

1 回答 1

1

您可能希望有一个索引增量数组用于在所有方向上移动(假设您将字段存储在矩阵中),因此您可以编写通用代码来尝试所有方向:

const int dirIncr[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}/*...*/}

因此,您循环遍历所有可能的起始单元格并从所有可能的方向进行每次销售搜索(并且在每个步骤中,您必须检查当前坐标是否仍然适合您的数组)。

至于实际的搜索前缀树在这里可能会有所帮助。

于 2015-03-18T18:02:12.380 回答