很抱歉用一个简单的问题打扰每个人,但我对C ++很新,我不知道该怎么做。所以我给了一个C ++框架,它读入包含wordsearch和要找到的单词列表的2个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;
}
答案 0 :(得分:1)
您可能希望有一个索引增量数组,以便在所有方向上移动(前提是您将字段存储在矩阵中),这样您就可以编写通用代码来尝试所有方向:
const int dirIncr[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}/*...*/}
因此,您可以遍历所有可能的起始单元格以及所有可能方向上的每次卖出搜索(并且每一步都必须检查当前坐标是否仍适合您的阵列)。
至于实际搜索Prefix trees可能会有所帮助。