迷宫(Maze)问题 递归解
Maze.h
Maze.cpp
main.cpp
迷宫数据文件data.txt
1 #ifndef MAZE_H
2 #define MAZE_H
3
4 #include <iostream.h>
5 #include <fstream.h>
6 #include <stdlib.h>
7 //using namespace std;
8
9 struct Intersection
10 {
11 int left;
12 int forward;
13 int right;
14 };
15
16 class Maze
17 {
18 public:
19 Maze(char *filename);
20 ~Maze();
21
22 int traverseAll(int currentPos);
23
24 private:
25 int size;
26 int EXIT;
27 Intersection *intsec;
28 };
29
30
31
32
33 #endif
34
35
2 #define MAZE_H
3
4 #include <iostream.h>
5 #include <fstream.h>
6 #include <stdlib.h>
7 //using namespace std;
8
9 struct Intersection
10 {
11 int left;
12 int forward;
13 int right;
14 };
15
16 class Maze
17 {
18 public:
19 Maze(char *filename);
20 ~Maze();
21
22 int traverseAll(int currentPos);
23
24 private:
25 int size;
26 int EXIT;
27 Intersection *intsec;
28 };
29
30
31
32
33 #endif
34
35
Maze.cpp
1 #include "Maze.h"
2
3 Maze::Maze(char *filename)
4 {
5 ifstream fin;
6 fin.open(filename, ios::in | ios::nocreate );
7 if(!fin)
8 {
9 cerr << "The maze data file " << filename << " open failed!" << endl;
10 exit(-1);
11 }
12
13 fin >> size;
14 if(size == 6)
15 cout << "yes" << endl;
16 intsec = new Intersection[size + 1];// from 1 ~ size
17 for(int i = 1; i <= size; i++)
18 {
19 fin >> intsec[i].left >> intsec[i].forward >> intsec[i].right;
20 cout << intsec[i].left << " " << intsec[i].forward << " " << intsec[i].right << endl;
21 }
22 fin >> EXIT;
23 cout << EXIT << endl;
24 fin.close();
25 }
26
27 Maze::~Maze()
28 {
29 if(intsec != NULL)
30 delete [] intsec;
31 }
32
33 int Maze::traverseAll(int currentPos)
34 {
35 cout << currentPos;
36 if(currentPos <= 0 || currentPos > size)
37 //if(currentPos > 0)
38 {
39 cerr << " Invailed beginning position!\n" << endl;
40 return -1;
41 }
42
43 if(currentPos == EXIT)
44 {
45 cout << currentPos <<" ";
46 return 1;
47 }
48 else if(traverseAll(intsec[currentPos].left))
49 {
50 cout << currentPos << " ";
51 return 1;
52 }
53 else if(traverseAll(intsec[currentPos].forward))
54 {
55 cout << currentPos << " ";
56 return 1;
57 }
58 else if(traverseAll(intsec[currentPos].right))
59 {
60 cout << currentPos << " ";
61 return 1;
62 }
63
64 //cout << " no path to exit" << endl;
65
66 return 0; //failed
67 }
68
69
2
3 Maze::Maze(char *filename)
4 {
5 ifstream fin;
6 fin.open(filename, ios::in | ios::nocreate );
7 if(!fin)
8 {
9 cerr << "The maze data file " << filename << " open failed!" << endl;
10 exit(-1);
11 }
12
13 fin >> size;
14 if(size == 6)
15 cout << "yes" << endl;
16 intsec = new Intersection[size + 1];// from 1 ~ size
17 for(int i = 1; i <= size; i++)
18 {
19 fin >> intsec[i].left >> intsec[i].forward >> intsec[i].right;
20 cout << intsec[i].left << " " << intsec[i].forward << " " << intsec[i].right << endl;
21 }
22 fin >> EXIT;
23 cout << EXIT << endl;
24 fin.close();
25 }
26
27 Maze::~Maze()
28 {
29 if(intsec != NULL)
30 delete [] intsec;
31 }
32
33 int Maze::traverseAll(int currentPos)
34 {
35 cout << currentPos;
36 if(currentPos <= 0 || currentPos > size)
37 //if(currentPos > 0)
38 {
39 cerr << " Invailed beginning position!\n" << endl;
40 return -1;
41 }
42
43 if(currentPos == EXIT)
44 {
45 cout << currentPos <<" ";
46 return 1;
47 }
48 else if(traverseAll(intsec[currentPos].left))
49 {
50 cout << currentPos << " ";
51 return 1;
52 }
53 else if(traverseAll(intsec[currentPos].forward))
54 {
55 cout << currentPos << " ";
56 return 1;
57 }
58 else if(traverseAll(intsec[currentPos].right))
59 {
60 cout << currentPos << " ";
61 return 1;
62 }
63
64 //cout << " no path to exit" << endl;
65
66 return 0; //failed
67 }
68
69
main.cpp
1 #include <iostream.h>
2 #include "Maze.h"
3 //using namespace std;
4
5 int main()
6 {
7 char filename[] = "data.txt";
8 Maze myMaze(filename);
9
10 int i = 1;
11 myMaze.traverseAll(i);
12
13 return 0;
14 }
15
16
17
2 #include "Maze.h"
3 //using namespace std;
4
5 int main()
6 {
7 char filename[] = "data.txt";
8 Maze myMaze(filename);
9
10 int i = 1;
11 myMaze.traverseAll(i);
12
13 return 0;
14 }
15
16
17
迷宫数据文件data.txt
6
0 2 0
3 5 6
0 0 4
0 0 0
0 0 0
7 0 0
7
0 2 0
3 5 6
0 0 4
0 0 0
0 0 0
7 0 0
7