简体   繁体   English

使用C ++从.txt解决迷宫

[英]Solving a Maze from .txt using C++

So, I am working on a code that should solve a maze to be given in a .txt file let's say "input.txt" where 1's = blocks and 0's = open paths in a form like this: 因此,我正在编写一个代码,该代码应解决在.txt文件中给出的迷宫的问题,我们说“ input.txt”,其中1的=块和0的=以以下形式打开路径:

Maze1
7,6 //Number of rows and columns of the maze: row,column
1,1,1,1,1,1
1,0,1,1,0,1
1,0,0,0,0,1
1,0,1,1,0,0
1,0,1,1,0,1
1,1,0,0,1,1
1,1,1,1,1,1
1 // number of entrances
1,2 // coordinate of an entrance relative to the origin: row, column

So I have got some algorithm in my mind, but let's say the "1st" entrance is invalid... 所以我脑子里有了一些算法,但是让我们说“第一个”入口是无效的...

so the output should be like this : 所以输出应该是这样的:

Maze1
Entrance: 1,2 Invalid

And this should be printed in another .txt file let's say "Output.txt".... but it actually gives me no syntax errors, yet it doesn't write anything to the Output.txt... 这应该打印在另一个.txt文件中,例如“ Output.txt”。...但是它实际上没有语法错误,但是它没有向Output.txt写入任何内容...

Anyways here's my "Incomplete-Code" ... so please help me : 无论如何,这是我的“不完整代码” ...所以请帮助我:

#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


struct Stack
{
    int data[30]; //X should be constant
    int top;
}; //Stack

void Push(int a,Stack &S)
{
    S.top++;

    if (S.top>=30) //Remember X should be constant
    {
        cout<<"The stack is full!"<<endl;
        S.top--;
        return;
    }

    else
    {
        S.data[S.top]=a;
    }
} //Push

int Pop(Stack &S)
{
    if (S.top==-1)
    {
        cout<<"The stack is empty!"<<endl;
        return 0;
    }

    else
    {
        int Temp=S.data[S.top];
        S.data[S.top]=NULL;
        S.top--;
        return Temp;
    }
} //Pop





int main ()
{
    #define false 1;
    #define true 0;
    string line;
    Stack s1 = { NULL, -1 }, s2 = { NULL, -1 };
    int x, y;   //Dimensions of Matrix.
    int z;      //Element Location in Matrix.
    int a;      //Number of Entrance Points.
    int b, c;   //Coordinates of Entrance Points.
    char ch;   //Comma indication.

    ifstream input ("Input.txt");   //Using relative addresses.
    ofstream output ("Output.txt");

    input>>line;
    cout<<"Solution for "<<line<<" : \n \n \n";
    input>>x>>ch>>y;                //Reading the Dimensions of the Matrix.
    cout<<line<<" coordinates are "<<x<<ch<<y<<endl<<endl<<endl;

    int **Maze = new int *[x];      //Creating Dynamic Matrix.
    for (int i=0; i<x; i++)
    {
        Maze[i]= new int [y];
    }


    for (int i=0; i<x; i++)         //Filling the Maze from the .txt
    {
        for (int j=0; j<y; j++)
        {
            input>>z;
            Maze[i][j]=z;
        }
    }


    input>>a;                       //Reading the number of entrances.
    for(int i=0; i<a; i++)  
    {
        input>>b>>ch>>c;            //Reading the entrance coordinates.
        if (Maze[b][c]==1 || b>7 || c>6)   //Checking for the validity of entrance point.
        {
            cout<<"Entrance: "<<b<<ch<<c<<"    is Invalid! \n";
            output<<"Entrance: "<<b<<ch<<c<<"    is Invalid! \n";// WRITE ENTRANCE IS INVALID IN OUTPUT FILE
        }

    }



    output.close();
    input.close();


    for(int i=0; i<x; i++)          //Deleting Maze
    {
        delete[] Maze[i];
    }
    delete[] Maze;



  return 0;
}

So where's the mistake ? 那么哪里出了错?

Arrays in C++ are 0-indexed - that is, Maze[1][2] is the cell in the second row, third column. C ++中的数组的索引为0-即Maze[1][2]是第二行第三列中的单元格。 Either number your entrance cell like that in the input file, or subtract 1 from each co-ord in the code. 像输入文件中那样为您的入口单元编号,或者从代码中的每个坐标中减去1。

Also, when parsing the maze itself, you don't seem to be taking account of the commas. 另外,在解析迷宫本身时,您似乎并没有考虑逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM