C++ 示例项目 - 需要算法帮助

标签 c++ algorithm

我的一位 friend 给我发了一份编程作业,这样我就可以温习一些 C++ 技能。以下是程序描述和我提出的算法。有人可以提供一些反馈/替代解决方案吗:

问题:

该程序创建单词搜索谜题 - 单词打印在矩形网格中的随机位置。单词可以是水平的或垂直的,并且可以是正向(从左到右或从上到下)或反向(从右到左或从下到上)。未使用的网格方 block 填充有随机字母。该程序应采用一组单词列表作为输入并生成两个文件作为输出。第一个列表为每个谜题列出了谜题中的单词列表,然后是谜题本身。第二个应该显示每个谜题中单词的位置,没有随机填充字母

我们的输入文件包含以下内容: 一个数字 n > 0(代表拼图中的单词数),后面跟着那么多单词。例如:

3
佛罗多
吉姆利
阿拉贡

N 不会大于 10

我们需要使用大小为 12 x 12 的多维数组来创建拼图

要求:
1. 两个输出文件 - 一个包含拼图单词和拼图,一个仅包含答案,没有填充字符
2.横字数必须与竖字数一样多
3. 1/3的单词需要颠倒
4. 拼图中至少需要有两个交叉点


建议的算法:
1. 创建两个多维数组 - 一个用于谜题,一个用于解决方案
2. 创建一个包含字母表中各个字母的一维数组
3. 用随机字母填充拼图数组(使用伪随机 # 生成器和步骤 # 2 中的数组)
4.开始读取输入文件
5.读入n
6. 当计数器小于n时,以单词读取,也有竖字和横字的计数器
7. 对于每个单词,找到字符串的长度
8. 找到一个随机数组位置来插入单词。
9. 如果随机位置索引 + 字符串长度 <= 12 或随机位置索引 - 字符串长度 >= 0(以确保单词能够正向或反向匹配),则插入单词
10.同时将单词插入解决方案数组
12. 重用数组插入输入文件中的所有单词(类似方式)

我仍然不确定如何确保至少存在两个交叉点。

我还担心我提出的算法过于复杂。

非常感谢您的反馈!


好的,在我决定返回并重新审视算法之前,这就是我进入编码过程的情况:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;

//Error Codes
const int INPUT_FAIL = 1;
const int PUZZLES_OUTPUT_FAIL = 2;
const int SOLUTIONS_OUTPUT_FAIL = 3;

//Function Declarations/Prototypes
void OpenFiles(ifstream& input, ofstream& puzzles, ofstream& solutions);
//PRE:  The filestream objects exist and their address locations are passed in
//POST: The filestreams are opened. If they cannot be opened, an error message is printed to screen
//      and the program is terminated.

void FillArray(char puzzle[][12], char alphabet[]);
//PRE:  The address of the array is passed in
//POST: The array is filled with a random set of 

void CreatePuzzle(char puzzle[][12], ifstream& input, ofstream& puzzles, ofstream& solutions);
//PRE:  The address of the puzzle array,the address of the ifstream object and the addresses of the
//      ofstream objects are passed in.
//POST: The data in the input file is read and the words are input into the puzzle AND the puzzle
//      and solutions are printed to file.

void PrintPuzzle(char puzzle[][12], ofstream& output);
//PRE:  The address of the puzzle array and the ofstream object is passed in
//POST: The puzzle is output to the file

int main()
{
    //Seed the pseudo random generator
    srand(time(NULL));



    //Declare the filestream objects
    ifstream input;
    ofstream puzzles, solutions;

    //Declare the 2D array
    char puzzle[12][12];
    char solution[12][12];

    //Declare an alphabet array
    char alphabet[27] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    /*char alphabet[27] = {'A','B','C','D','E','F','G','H','I','J','K','L',
    'M','N','O','P','Q','R','S','T','U','V','W',
    'X','Y','Z'};*/

    //Attempt to open files
    OpenFiles(input, puzzles, solutions);

    //Fill puzzle array with random letters of the alphabet
    FillArray(puzzle, alphabet);

    //Print puzzle
    PrintPuzzle(puzzle, puzzles);

    //Read in data to create puzzle
    input >> numwords;

    return 0;
}



//Function definitions
void OpenFiles(ifstream& input, ofstream& puzzles, ofstream& solutions)
{

    //Attempt to open files
    input.open("input.txt");
    puzzles.open("puzzles2.txt");
    solutions.open("solutions2.txt");

    //Ensure they opened correctly
    if (input.fail())
    {
        cout << "Input file failed to open!" << endl;
        exit(INPUT_FAIL);
    }

    if (puzzles.fail())
    {
        cout << "Output file - puzzles.txt failed to open!" << endl;
        exit(PUZZLES_OUTPUT_FAIL);
    }

    if (solutions.fail())
    {
        cout << "Output file - solutions.txt failed to open" << endl;
        exit(SOLUTIONS_OUTPUT_FAIL);
    }

}


void FillArray(char puzzle[][12], char alphabet[])
{
    int tmp;
    for(int i = 0; i < 12; i++)
    {
        for(int j = 0; j < 12; j++)
        {
            tmp = rand()%26;
            puzzle[i][j] = alphabet[tmp];
        }
    }
}


void PrintPuzzle(char puzzle[][12], ofstream& output)
{
    for(int i = 0; i < 12; i++)
    {
        for(int j = 0; j < 12; j++)
        {
            output <<   puzzle[i][j] << " ";
        }
        output << endl;
    }
}



void CreatePuzzle(char puzzle[][12], ifstream& input, ofstream& puzzles, ofstream& solutions)
{
    string pword; //#the puzzle word being read
    int numwords; //# of words in a given puzzle
    char tmparray[13];
    int wordlength = 0;
    int startloc;

    //Read the number of words to be used in the puzzle
    input >> numwords;

    int vwords = numwords/2; //#of vertical words
    int rwords = numwords/3; //# of reversed words
    int hwords = (numwords - (numwords/2)); //# of horizontal words

    for(int i = 0; i < numwords; i++)
    {
        //Read the word into our tmparray
        input >> pword;
        tmparray[] = pword;
        wordlength = pword.length();

        //Find a random array location to begin inserting the words
        startloc = rand()%12;
    int tmpcount = 0; //a temporary counter to ensure that 
        for(tmpcount; tmpcount <= 1; tmpcount ++)startloc + wordlength < 12)
        {
            for(int j = 0; j <= wordlength; j++)
            {
                puzzle[startloc][startloc]

最佳答案

先在纸上尝试一下
然后让它工作(在代码中)
然后使其快速/高效/优雅

编辑 - 抱歉,我不是在讽刺,这是在OP发布代码之前,不清楚他们是否尝试过这个问题。

关于C++ 示例项目 - 需要算法帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2249502/

相关文章:

c++ - 为什么这个构造函数在传递给 std::thread 时被调用 3 次?

c++ - multimap 与带集合的 map

c++ - 从 sourceCpp 移动到带有 Rcpp 的包

c++ - 有哪些小型、快速和轻量级的开源应用程序(µTorrent 式)?

c++ - 二进制文件中的模式搜索

java - 更合适的说法是分摊 O(1) vs O(n) 插入未排序的动态数组?

c++ - "|="这是什么意思,这叫什么? (c++)

c - 使用固定数量的函数,如何计算给定误报概率的布隆过滤器的大小?

algorithm - 使用 Q 查询遍历图形中的最后一个节点

string - 就地游程长度编码算法