我的C ++课程作业是对哈利波特的Tri Wizard Tournament迷宫挑战的解释。作业说明如下:http://www.cs.rhodes.edu/~kirlinp/courses/cs2/s17/proj/proj5/
目前我正在努力解决我的求解功能。移动范围超出范围,例如:大于行数/列数或负数的数字。我也得到无限的输出,就像动作一直持续直到被终止,因为被识别为无限循环。
如果您愿意查看我的代码/作业,我将非常感激。我觉得我的大部分代码都是正确的,但我有些问题,我不知道如何解决自己。
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<string> read_function(string file);
string solve(vector<string> maze, int &row, int &col, int &numcalls);
void find_start(vector<string> maze, int &row, int &col);
void print_state();
vector<string> maze;
string s;
int main() {
cout << "Enter a file name: " << endl;
string file;
cin >> file;
vector<string> test = read_function(file);
print_state();
int row = 0;
int col = 0;
int numcalls = 0;
find_start(maze, row, col);
cout << "start is: " << row << "," << col << endl;
string solution = solve(maze, row, col, numcalls);
cout << solution << endl;
cout << "Number of steps: " << numcalls << endl;
print_state();
return 0;
}
vector<string> read_function(string file) {
ifstream infile; // Input file stream
infile.open(file);
while (getline(infile, s)) //
{
//infile >> s;
maze.push_back(s);
}
infile.close();
return maze;
}
void print_state(){
for (int i = 0; i < maze.size(); i++){
cout << maze[i] << endl;
}
}
string solve(vector<string> maze, int &row, int &col, int &numcalls){
numcalls += 1;
cout << row << "," << col << endl;
maze[row][col] = 'o';
if (maze[row][col] == 'C') {
return " C ";
}
if (row>=0 && maze[row-1][col] != '#' && maze[row-1][col] != 'o'&& maze[row-1][col] != ' '){ // MOVE NORTH Y/N
row --;
string answer = solve(maze, row, col, numcalls);
if (answer != "X"){
return "N"+answer;
}
}
if (maze[row+1][col] != '#' && maze[row+1][col] != 'o' && maze[row+1]
[col] != ' '){ // MOVE SOUTH Y/N
row ++;
string answer = solve(maze, row, col, numcalls);
if (answer != "X"){
return "S"+answer;
}
}
if (maze[row][col+1] != '#' && maze[row][col+1] != 'o' && maze[row]
[col+1] != ' '){ // MOVE EAST Y/N
col ++;
string answer = solve(maze, row, col, numcalls);
if (answer != "X"){
return "E"+answer;
}
}
if (maze[row][col-1] != '#' && maze[row][col-1] != 'o' && maze[row][col-
1] != ' '){ // MOVE WEST Y/N
col --;
string answer = solve(maze, row, col, numcalls);
if (answer != "X"){
return "W"+answer;
}
}
else{
maze[row][col] = '.';
}
return "X";
}**
void find_start(vector<string> maze, int &row, int &col){
for (int i = 0; i < maze.size(); i++) {
for (int j = 0; j < maze[i].size(); j++){
if (maze[i][j] == 'H'){
row = i;
col = j;
}
}
}
}
答案 0 :(得分:1)
缺少多个绑定检查,以及路径的返回字符串值将始终返回&#39; X&#39;。
您的fixed()方法的固定来源:
string solve(vector<string>& maze, int row, int col, int &numcalls) {
numcalls += 1;
cout << row << "," << col << endl;
string answer;
bool any_direction = false;
if (maze[row][col] == 'C') {
return " C ";
}
maze[row][col] = 'o';
if (row > 0 && maze[row-1][col] != '#' && maze[row-1][col] != 'o'&& maze[row-1][col] != ' '){ // MOVE NORTH Y/N
//row--;
answer = solve(maze, row-1, col, numcalls);
if (answer != "X"){
any_direction = true;
return "N"+answer;
}
}
if (row < (maze.size()-1) && maze[row+1][col] != '#' && maze[row+1][col] != 'o' && maze[row+1][col] != ' '){ // MOVE SOUTH Y/N
//row++;
answer = solve(maze, row+1, col, numcalls);
if (answer != "X"){
any_direction = true;
return "S"+answer;
}
}
if (col < (maze[row].size()-1) && maze[row][col+1] != '#' && maze[row][col+1] != 'o' && maze[row][col+1] != ' '){ // MOVE EAST Y/N
//col++;
answer = solve(maze, row, col+1, numcalls);
if (answer != "X"){
any_direction = true;
return "E"+answer;
}
}
if (col > 0 && maze[row][col-1] != '#' && maze[row][col-1] != 'o' && maze[row][col-1] != ' '){ // MOVE WEST Y/N
//col--;
answer = solve(maze, row, col-1, numcalls);
if (answer != "X"){
any_direction = true;
return "W"+answer;
}
}
if(!any_direction) {
maze[row][col] = '.';
return "X";
}
return answer;
}
此解决方案不适合您的任务,但希望它能帮助您完成它并更好地理解递归。享受您的C ++学习!