Problem with this maze game

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
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

char **path;
bool **exist;
int ent_a, ent_b, a, b;

bool route(int x, int y)
{
  if (path[x][y] == 'F'){
    exist[x][y] = true;
    exist[ent_a][ent_b] = true;
    return true;
  }

  if ((x < 0) || (x > a) || (y < 0) || (y > b))
    return false;
  
  exist[x][y] = true;

  if (!((x-1 < 0) || (x-1 > a) || (y < 0) || (y > b)))
    if ( !exist[x-1][y] && path[x-1][y] !='W')
      if (route(x-1,y))
        return true;
  
  if (!((x+1 < 0) || (x+1 > a) || (y < 0) || (y > b)))
    if ( !exist[x+1][y] && path[x+1][y] !='W')
      if (route(x+1,y))
        return true;
  
  if (!((x < 0) || (x > a) || (y-1 < 0) || (y-1 > b)))
    if ( !exist[x][y-1] && path[x][y-1] !='W')
      if (route(x,y-1))
        return true;
  
  if (!((x < 0) || (x > a) || (y+1 < 0) || (y+1 > b)))
    if ( !exist[x][y+1] && path[x][y+1] !='W')
      if (route(x,y+1))
        return true; 

  exist[x][y] = false;
  return false;
}

int main()
{
  ofstream fout;
  fout.open("path.txt");

  string infile;
  cin >> infile;
  ifstream fin;
  fin.open(infile.c_str());

  cin >> a >> b;
 
  path = new char*[a];
  exist = new bool*[a];
  for(int i = 0; i < a; i++)
  {
    path[i] = new char[b];
    exist[i] = new bool[b];
  }

  for(int i = 0;i < a; i++)
  {
    for(int j = 0;j < b; j++)
    {
      fin >> path[i][j]; 
      exist[i][j] = false;
    } 
  }
  
  for(int i = 0;i < a; i++)
    for(int j = 0;j < b; j++)
      if (path[i][j] == 'S')
        {
 	  ent_a = i;
          ent_b = j;
 	}

  if (route(ent_a,ent_b))
    for(int i = 0;i < a; i++)
      for(int j = 0;j < b; j++)
        if (exist[i][j])
          fout << i << ", " << j << endl;
  else fout << "No ways";
  
  fin.close();
  fout.close();
  
  for(int i = 0;i < a; i++){
    delete [] exist[i];
    delete [] path[i];
  }
  delete [] exist;
  delete [] path;
          
  return 0;
}


I am writing a maze game with content of input file maze_1.txt
(S stands for entrance, F for exit, W for wall, . for road):
SWWWW
.....
W.W.W
....F
The execution of the program
(input the name of the input file, the numbers of rows and columns of the maze):
$./maze
maze.txt
4
5
After the execution, the content of output file output.txt should be
(output the one of the path from entrance to exit):
0, 0
1, 0
1, 1
1, 2
1, 3
2, 3
3, 3
3, 4

1
2
3
4
5
[administrator@localhost Desktop]$ ./maze
maze_1.txt
4
5
Segmentation fault (core dumped)


However, I got the error "Segmentation fault (core dumped)" after execution,
and I can't figure out what's wrong with my program.
Can anyone tell me what's wrong?
Topic archived. No new replies allowed.