gpt4 book ai didi

C++ Wordsearch 拼图网格二维数组

转载 作者:行者123 更新时间:2023-11-28 01:34:17 25 4
gpt4 key购买 nike

我在尝试读取包含单词搜索字母的文本文件(如下)时遇到问题。我想以数组形式读取文本文件,然后能够将我的 dictionary.txt 中的单词与 wordsearch_grid.txt 匹配。有什么想法吗?

wordsearch_grid:
9
E M M A R G O R P
C L U A U N L L D
O T A O F I L O I
M E U N J G E O K
P W H K G G H P Q
I C O M P U T E R
L L V R Z B A O X
E H O M L E Q G U
T N I R P D C O E

dictionary:
COMPILE
COMPUTER
DEBUGGING
HELLO
KITCHEN
GRAPHICS
LOOP
SPAN
PROGRAMME
WORLD

我目前的代码如下:

#include "WordSearch.h"
#include "fstream"
#include <iostream>
#include <string>
#include "vector"


using namespace std;

vector<string> list;
vector<string> grid;
string line;
string n;

WordSearch::WordSearch(const char * const filename)
{


}

WordSearch::~WordSearch()
{

}

void WordSearch::ReadSimplePuzzle() {

ifstream inFile;
inFile.open("wordsearch_grid.txt");

if (inFile.fail())
{
cerr << "Error Wordsearch Grid File" << endl;
exit(1);
}
else
{

while (getline (inFile, n))
{
cout << n << endl;
}
//grid[4][3];
inFile.close();
//cout << grid << endl;
cout << "\n" << endl;
}

}

void WordSearch::ReadSimpleDictionary()
{
ifstream inFile;
inFile.open("dictionary.txt");


if (inFile.fail())
{
cerr << "Error Dictionary File" << endl;
exit(1);

}
else
{
int count = 0;
while (getline(inFile, line))
{
list.push_back(line);
cout << line << endl;
}
inFile.close();
}


}

void WordSearch::SolvePuzzleSimple()

{


}

到目前为止,它可以读取文件并显示它们,但我希望能够操纵网格,以便我可以匹配说“COMPILE”的第一个和最后一个字母,以匹配网格中的 2 个字母,并且输出到 output.txt “在 [1][2] 处找到了 COMPILE

最佳答案

这是内联逻辑,您可以根据自己的选择将其封装在类中:

#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
using namespace std;

ifstream inFile("wordsearch_grid.txt");
ifstream dict("dictionary.txt");
ofstream out("output.txt");

int main(){
string word;
char c;
char grid[9][9] = {};
int row = 0;
int column = 0;
vector<string> wordsFound;
clock_t start;
double duration;
vector<string> words;
vector<vector<int> > locations;
//store words from dictionary into vector
while (getline(dict, word))
{
words.push_back(word);
}
start = clock();
//store grid in a c-array
while (inFile.get(c))
{
if (c != ' ' && c != '\n')
{
grid[row][column] = c;
if (column == 8)
{
column = 0;
row++;
}else
{
column++;
}
}
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cout << grid[i][j] << " ";
}
cout << endl;
}
duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
cout << "Time it took to populate grid (seconds) : " << duration << endl;
start = clock();
//for each character in grid
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
//cout << grid[i][j] << " ";
//for each word
for (int k = 0; k < words.size(); k++)
{
//check if grid letter equals the first letter of word
if (grid[i][j] == words[k][0])
{
//check horizontal vertical and diagonal
for (int l = 1; l <= words[k].size(); l++)
{
if (
//break if no word was found
grid[i-l][j] != words[k][l] &&
grid[i+l][j] != words[k][l] &&
grid[i][j+l] != words[k][l] &&
grid[i][j-l] != words[k][l] &&
grid[i+l][j+l] != words[k][l] &&
grid[i-l][j-l] != words[k][l] &&
grid[i+l][j-l] != words[k][l] &&
grid[i-l][j+l] != words[k][l] )
{
break;
}
else if (l == words[k].size()-1)
{
//else write word found to file
//out << words[k] << " was found at [" <<
//j+1 << "][" << i+1 << "]" << endl;
//add word location to locations
vector<int> location;
location.push_back(j+1);
location.push_back(i+1);
locations.push_back(location);
//add word to wordsFound
wordsFound.push_back(words[k]);
}
}
}
}
}
//cout << endl;
}
duration = (clock() - start ) / (double) CLOCKS_PER_SEC;
cout << "Time it took to finish wordsearch puzzle (seconds) : " << duration << endl;

out << "number of words found: " << wordsFound.size() << endl;

for (int i = 0; i < wordsFound.size(); i++){
out << wordsFound[i] << " was found at [" << locations[i][0] << "][" << locations[i][1] << "]" << endl;
}
out << "number of words not found: " << words.size() - wordsFound.size() << endl;

for (int i = 0; i < words.size(); i++) {
for (int j = 0; j < wordsFound.size(); j++) {
//loop to check if word in dictionary wasn't found and append to output.txt
if (words[i] == wordsFound[j]){
break;
}
else if (j == wordsFound.size()-1){
out << words[i] << " was not found!" << endl;
}
}
}
return 0;
}

关于C++ Wordsearch 拼图网格二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50013087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com