stack overflow error in maze game

hi guys. this is the code of maze game. of course it is not complete yet. when ever i run it, it shows stack overflow error. what is wrong here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "stdafx.h"
#include <iostream>
using namespace std;

int milad(int b[10][10], int n, int m, int x1, int y1, int x2, int y2,int c[10][10])
{

	if (x1 == x2 && y1 == y2)
	{
		return true;
	}

	
	else if (b[x1++][y1] != 1 && x1++ < n && c[x1++][y1] == 0)
	{
		c[x1][y1]++;
		return milad(b, n, m, x1++, y1, x2, y2, c);
	}


	else if (b[x1--][y1] != 1 && x1-- >= 0 && c[x1--][y1] == 0)
	{
		c[x1][y1]++;
		return milad(b, n, m, x1--, y1, x2, y2, c);
	}


	else if (b[x1][y1++] != 1 && y1++ < m && c[x1][y1++] == 0)
	{
		c[x1][y1]++;
		return milad(b, n, m, x1, y1++, x2, y2, c);
	}


	else if (b[x1][y1--] != 1 && y1-- >= 0 && c[x1][y1--] == 0)
	{
		c[x1][y1]++;
		return milad(b, n, m, x1, y1--, x2, y2, c);
	}
	
	return false;

}





int main()
{
	char a[10][10];
	int b[10][10];
	int c[10][10] = {0};
	int n, m, i, j;

	//n * m
	cin >> n >> m;

	for (j = 0; j < m; j++)
	{
		for (i = 0; i < n; i++)
			cin >> a[i][j];
	}

	//changing the shape to numbers
	for (j = 0; j < m; j++)
	{
		for (i = 0; i < n; i++)
		{
			if (a[i][j] == '*')
				b[i][j] = 1;
			else b[i][j] = 0;
		}
	}

	int x1, y1, x2, y2;
	//place of entering
	cin >> x1 >> y1;

	//place of exiting
	cin >> x2 >> y2;

	i = 11, j = 11;

	if (milad(b, n, m, x1, y1, x2, y2,c))
		cout << "yes";
	else cout << "no";

	cout << endl;
	system("pause");
	return 0;
}

Last edited on
> else if (b[x1++][y1] != 1 && x1++ < n && c[x1++][y1] == 0)
you are using postincrement there, that would modify the variable.
use x1+1 instead (same for y1)
I don't know the game Maze and I didn't had a deeper view into your code, but you should test index overflow each time before using the indices to access an array. The same after getting the indices from stdin.

What's the goal of this game? Starting from a given position then reaching a specified second one whithout crossing walls? If I do understand your code, then the array b represents your board whith walls marked as digit 1, while c marks your visiting status?

Is it correct to do up to four steps each time you call milad()?
Even with the bugs just reported fixed, your process may run into infinit recursive function calls when trying to leave a blind alley. == Stack overflow.
Add
cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << endl;
At the beginning of milad and see what happens. I suspect that you're overflowing the indexes.
Topic archived. No new replies allowed.