我让我的一个朋友发给我这个编程任务,这样我就可以了解一些我的C ++技能。以下是程序说明和我提出的算法。有人可以提供一些反馈/替代解决方案:
问题:
此程序创建单词搜索难题 - 单词在矩形网格中的随机位置打印。单词可以是水平的或垂直的,可以是向前的(从左到右或从上到下)或反向(从右到左或从下到上)。未使用的网格方块用随机字母填充。程序应该将一组单词列表作为输入,并生成两个文件作为输出。对于每个拼图,首先列出拼图中的单词列表,然后是拼图本身。第二个应该显示单词在每个拼图中的位置,而不是随机填充字母
我们的输入文件包含以下内容: 数n> 0(表示拼图中的单词数量)后跟那么多单词。例如:
3
FRODO
GIMLI
ARAGORN
N不会大于10
我们需要使用尺寸为12 x 12的多维数组创建拼图
要求:
1.两个输出文件 - 一个包含拼图单词和拼图,一个只有解决方案,没有填充字符
2.需要有与垂直单词一样多的水平单词
3. 1/3的单词需要反转
4.谜题中至少需要有两个交叉点
建议的算法:
1.创建两个多维数组 - 一个用于拼图,另一个用于解决方案
2.创建一个包含字母表中各种字母的一维数组
3.用字母随机字母填充谜题数组(使用伪随机#生成器和步骤#2中的数组)
4.开始阅读输入文件
5.读入n
6.当计数器小于n时,读入单词,也有一个垂直单词和水平单词的计数器
7.对于每个单词,找到字符串的长度
8.找到一个随机数组位置来插入单词
9.如果随机位置索引+字符串长度< = 12或者随机位置索引 - 字符串长度> gt = = 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]
答案 0 :(得分:3)
首先在纸上试试
然后让它工作(代码中)
然后快速/高效/优雅
编辑 - 抱歉,我没有讽刺,这是在OP发布的代码之前,并不清楚他们是否尝试过这个问题。
答案 1 :(得分:2)
我的第一个建议是不要用任何东西预先填充数组 - 只需将文字粘在一起,并在完成后随机填补空白。
答案 2 :(得分:1)
一些想法/建议:
答案 3 :(得分:0)
我的第一个想法是: