【原创】递归函数实现迷宫问题
//迷宫问题
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#define mazefile "maze.txt"
using namespace std;
class Escape
{
public:
int left;
int forward;
int right;
friend ostream& operator<<(ostream& out,Escape& e);
bool operator!();
};
ostream& operator<<(ostream& out,Escape& e) //重载<<
{
if(e.left!=0) out <<e.left <<"号路口 ";
if(e.forward!=0) out <<e.forward <<"号路口 ";
if(e.right!=0) out <<e.right <<"号路口 ";
return out;
}
bool Escape::operator!() //重载!
{
if(this->left==0 && this->forward==0 && this->right==0)
return false;
return true;
}
vector<Escape> ev;
vector<int> vi;
int _EXIT;
int ston(const string& s);
bool findex(int escape);
int main(int argc,char* argv[])
{
string line;
ifstream in(mazefile);
Escape esc;
int i=0;
while(getline(in,line))
{
int l=0;
string s;
l=line.find(":");
s=line.substr(0,l);
line=line.substr(l+1); //获取冒号后的字符串
l=line.find(",");
esc.left=ston(line.substr(0,l)); //取得第一个逗号前的数字
line=line.substr(l+1);
l=line.find(",");
esc.forward=ston(line.substr(0,l));//取得第二个逗号前的数字
line=line.substr(l+1);
esc.right=ston(line); //取得第三个逗号前的数字
ev.push_back(esc);
if(!ev[i])
cout <<i <<"号路口通向:" <<ev[i] <<endl;
else
cout <<i <<"号路口是死路!" <<endl;
i++;
}
cout <<"请输入您要到达的路口:";
cin >>_EXIT;
if(findex(1))
cout <<"1号路口->";
for(int i=vi.size()-1;i>=0;i--)
cout <<vi[i] <<"号路口->";
cout <<"EXIT" <<endl;
system("pause");
return 0;
}
bool findex(int escape) //递归查找
{
if(escape>0)
{
if(escape==_EXIT) return true;
if(findex(ev[escape].left))
{
vi.push_back(ev[escape].left);
return true;
}
if(findex(ev[escape].forward))
{
vi.push_back(ev[escape].forward);
return true;
}
if(findex(ev[escape].right))
{
vi.push_back(ev[escape].right);
return true;
}
}
return false;
}
int ston(const string& s) //将字符串转换为整数并返回
{
const char* c=s.c_str();
int num=0;
while(*c)
num=num*10+(int)((*c++)-'0');
return num;
}
maze.txt的内容如下
0:0,0,0
1:2,0,0
2:4,0,0
3:0,0,0
4:3,5,0
5:6,0,0
6:7,0,0
7:8,9,0
8:0,0,18
9:10,0,0
10:11,13,0
11:12,0,14
12:0,0,0
13:16,0,0
14:15,20,0
15:17,0,0
16:21,0,0
17:0,19,0
18:22,0,0
19:0,0,0
20:0,23,0
21:0,0,23
22:0,0,0
23:0,0,24
24:26,25,27
25:0,27,0
26:28,0,0
27:0,0,27
28:0,0,0