Reputation: 39
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:
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...
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 ?
Upvotes: 0
Views: 3181
Reputation: 46677
Arrays in C++ are 0-indexed - that is, Maze[1][2]
is the cell in the second row, third column. Either number your entrance cell like that in the input file, or subtract 1 from each co-ord in the code.
Also, when parsing the maze itself, you don't seem to be taking account of the commas.
Upvotes: 1