I am making a recursive algorithm as part of an extra credit project for class to find the shortest path through a 2D int array maze of 0's and 1's. 0 represents a wall and 1 represents a hallway where you can pass through. I think I have everything perfect but it just won't compile. It says I am trying to convert something from int to array or whatever. Here is my code, please help.
#include <iostream>
using namespace std;
#define 20 SIZEX;
#define 5 SIZEY;
int value; //to compare paths to take
int starti = 1;
int startj = 0;
int newi;
int newj;
int counter = 0; //keeps track of how many steps taken
void pathfinder(int a[][SIZEX], int currenti, int currentj);
int arr[SIZEY][SIZEX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,
0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int main()
{
pathfinder(arr, starti, startj);
system("PAUSE");
return 0;
}
void pathfinder(int a[][SIZEX], int currenti, int currentj)
{
//as soon as it walks somewhere, the value of that spot increments
int temp;
temp = a[currenti][currentj];
temp++;
a[currenti][currentj] = temp;
if (counter == 0) //keeps track of starting point
{
starti = currenti;
startj = currentj;
}
if (currenti-1 >= 0 && a[currenti-1][currentj] != 0) //checks up
{
value = a[currenti-1][currentj];
}
else if (currenti+1 < 5 && a[currenti+1][currentj] != 0) //checks down
{
value = a[currenti+1][currentj];
}
else if (currentj-1 >= 0 && a[currenti][currentj-1] != 0) //checks left
{
value = a[currenti][currentj-1];
}
else if (currentj+1 < 20 && a[currenti][currentj+1] != 0) //checks right
{
value = a[currenti][currentj+1];
}
//value has a value, now check it against all possible values for the least travelled path
if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value > a[currenti-1][currentj])
{
value = a[currenti-1][currentj];
}
if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value > a[currenti+1][currentj])
{
value = a[currenti+1][currentj];
}
if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value > a[currenti][currentj-1])
{
value = a[currenti][currentj-1];
}
if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value > a[currenti][currentj+1])
{
value = a[currenti][currentj+1];
}
//value now holds the smallest possible value among the four possible paths
if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value == a[currenti-1][currentj]) //move up
{
newi = currenti-1;
newj = currentj;
counter++;
}
else if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value == a[currenti+1][currentj]) //move down
{
newi = currenti+1;
newj = currentj;
counter++;
}
else if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value == a[currenti][currentj-1]) //move left
{
newi = currenti;
newj = currentj-1;
counter++;
}
else if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value == a[currenti][currentj+1]) //move right
{
newi = currenti;
newj = currentj+1;
counter++;
}
//upon reaching the exit, it will print out a new 2d maze, and the path with the smallest value of non-zero integers is the shortest path
if ((currenti == 0 || currentj == 0 || currenti == 4 || currentj == 19) && (currenti != starti || currentj !=startj))
{
for (int i = 0; i < 5; i++)
{
for (int j = 0;j < 20;j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return;
}
pathfinder(arr, newi, newj);
}
1>------ Build started: Project: Project5, Configuration: Debug Win32 ------ 1> Source.cpp 1>c:\\users\\justin\\documents\\visual studio 2012\\projects\\project5\\project5\\source.cpp(22): error C2664: 'pathfinder' : cannot convert parameter 1 from 'int' to 'int [][20]' 1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 1>c:\\users\\justin\\documents\\visual studio 2012\\projects\\project5\\project5\\source.cpp(114): error C2664: 'pathfinder' : cannot convert parameter 1 from 'int' to 'int [][20]' 1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
These are incorrect:
#define 20 SIZEX;
#define 5 SIZEY;
If you really want macros, the correct way to define them is:
#define SIZEX 20
#define SIZEY 5
But, as this is C++, you should make use of const int
here:
const int SIZEX = 20;
const int SIZEY = 5;
That should fix your compile problem.
On a separate note: If you're going to pass arr
as your initial argument to pathfinder()
to start the recursion in main
, you might change your recursive call to pass the argument pathfinder()
was passed. ie:
pathfinder(a, newi, newj);
Otherwise, even if main
were to pass a different array to pathfinder
, it would still do most of its work on arr
, rendering the parameter useless.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.