Hi~I got stuck in this problem. Someone please help me!!!!
The problem is that the program will ask the user to input a number between 4 and 20 to decide the size of the maze. It will later ask the user to input the contents of the maze row by row and store it into a 2D bool array (true means blocked and false means clear). Then the program starts at the top left corner and tries to find a path leading to the lower right corner (can move right, left, up, down). At this time, the program should also maintain another char array which record the path found (if there's any) and print it out at the end of processing. This problem asks to use recursion to solve it.
Here's what I got now:
#include<iostream>
using namespace std;
int row, col;
int size=0;
bool maze[21][21];
char print[22][22];
const char start = 's', up = 'u', down = 'd', left = 'l', right = 'r', barrier = 'x';
char path(int coorx, int coory, int size)
{
if(maze[coorx][coory+1]=0)
{
print[coorx+1][coory+2]='r';
return path(coorx,coory+1,size);
}
else
{
if(maze[coorx+1][coory]=0)
{
print[coorx+2][coory+1]='d';
return path(coorx+1,coory,size);
}
else
{
if(maze[coorx][coory-1]=0)
{
print[coorx+1][coory]='l';
return path(coorx,coory-1,size);
}
else
{
if(maze[coorx-1][coory]=0)
{
print[coorx][coory+1]='u';
return path(coorx-1,coory,size);
}
}
}
}
}
int main()
{
while(size<4 || size>20)
{
cout<<"Please input size of maze (a number between 4 and 20 is expected) -> ";
cin >>size;
if(size<4 || size>20)
cout<<"**Error** maze size not in range!"<<endl;
}
cout<<"Please input contents of maze row by row, 1 for barrier and 0 for free passage."<<endl;
cout<<endl;
for(int i=1; i<size+1; i++)
{
for(int j=1; j<size+1; j++)
cin>>maze[i][j];
}
if(maze[1][1]==1)
cout<<"**Error** entrance to maze is blocked!"<<endl;
else
{
// find the path
for(int coorx=0;coorx<size;coorx++)
{
for(int coory=0;coory<size;coory++)
path(coorx,coory,size);
}
cout<<"The maze and the path:"<<endl;
// print the forum (adding characters '+','-', ' ')
print[0][0]=print[size+1][size+1]=print[0][size+1]=print[size+1][0]='+';
print[1][1]='s';
for(int x=1; x<size+1; x++)
{
for(int y=0; y<size+2; y++){
if(y==0 || y==size+1)
{
print[x][y]='|';
}
}
}
for(int x=0; x<size+2; x++)
{
for(int y=0; y<size+2; y++){
if(x==0 || x== size+1){
if(y!=0 && y!=size+1)
print[x][y]='-';
}
}
}
for(int row=0; row<size+2; row++)
{
for(int col=0; col<size+2; col++)
{
if(maze[row][col]==1)
print[row][col]='x';
}
}
// print out the record of the path found
for(int row=0; row<size+2; row++)
{
for(int col=0; col<size+2; col++){
cout<<print[row][col];
}
cout << endl;
}
}
return 0;
}
I don't know why I can't show those 'r', 'd', 'l', 'u'. I already assigned them to print[][], but why it won't show why I print out the print[][]??
New coding
#include<iostream>
using namespace std;
int row,col;
int size=0;
bool maze[20][20];
char print[22][22];
bool path(int coorx, int coory, int size)
{
if(coorx==size-1 && coory==size-1)
return true;
if(!maze[coorx][coory+1] && path(coorx,coory+1,size))
return true;
return 'r';
if(!maze[coorx+1][coory] && path(coorx+1,coory,size))
return true;
return 'd';
if(!maze[coorx][coory-1] && path(coorx,coory-1,size))
return true;
return 'l';
if(!maze[coorx-1][coory] && path(coorx-1,coory,size))
return true;
return 'u';
}
int main()
{
while(size<4 || size>20)
{
cout<<"Please input size of maze (a number between 4 and 20 is expected) -> ";
cin >>size;
if(size<4 || size>20)
cout<<"**Error** maze size not in range!"<<endl;
}
cout<<"Please input contents of maze row by row, 1 for barrier and 0 for free passage."<<endl;
cout<<endl;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
cin>>maze[i][j];
}
if(maze[0][0]==1)
cout<<"**Error** entrance to maze is blocked!"<<endl;
else
{
int row=0;
int col=0;
path(row,col,size);
if(!path(row,col,size))
{
cout<<"**Warning** no path from entrance to exit!"<<endl;
}
else
{
if('r')
print[row+2][col+3]='r';
if('d')
print[row+3][col+2]='d';
if('l')
print[row+2][col+1]='l';
if('u')
print[row+1][col+2]='u';
}
cout<<"The maze and the path:"<<endl;
// print the forum (adding characters '+','-', ' ')
print[0][0]=print[size+1][size+1]=print[0][size+1]=print[size+1][0]=='+';
print[1][1]='s';
for(int x=1; x<size+1; x++)
{
for(int y=0; y<size+2; y++){
if(y==0 || y==size+1)
{
print[x][y]=='|';
}
}
}
for(int x=0; x<size+2; x++)
{
for(int y=0; y<size+2; y++){
if(x==0 || x== size+1){
if(y!=0 && y!=size+1)
print[x][y]=='-';
}
}
}
for(int row=0; row<size; row++)
{
for(int col=0; col<size; col++)
{
if(maze[row][col]==1)
print[row+1][col+1]=='x';
}
}
path(0,0,size);
// pirnt out the record of the path found
for(int row=0; row<size+2; row++)
{
for(int col=0; col<size+2; col++){
cout<<print[row][col];
}
cout << endl;
}
}
return 0;
}
for maze size=4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
the sample run looks like
s r r r
d
d
d
but my program run like this
s r
nothing afterward, don't know why