本文共 1742 字,大约阅读时间需要 5 分钟。
#include <cstdlib>#include <iostream>#include <fstream>#include <vector>#include <iterator>#include <algorithm>using namespace std;const int NODE_COUNT = 6;vector<int> traceVec;int visit[NODE_COUNT + 1] = {0};typedef struct _NODE{ int forward; int left; int right;}NODE;int getTrace(const NODE *trace, int curPos){ if(curPos > -1 && visit[curPos] == 0) { if(curPos == 7) { traceVec.push_back(curPos); return 1; } else if(getTrace(trace, trace[curPos].left)) { traceVec.push_back(curPos); return 1; } else if(getTrace(trace, trace[curPos].forward)) { traceVec.push_back(curPos); return 1; } else if(getTrace(trace, trace[curPos].right)) { traceVec.push_back(curPos); return 1; } visit[curPos] = 1; //如果这个点的三个方向都走过, 则将其置为visited状态 } return 0;}int main(int argc, char *argv[]){ ifstream input("input.txt"); if(!input) { cerr << "Can not open file: " << input << endl; return -1; } NODE trace[NODE_COUNT + 1]; int index = 1; while(input >> (trace[index].left)) { input >> (trace[index].forward); input >> (trace[index].right); index++; } input.close(); getTrace(trace, 1); reverse(traceVec.begin(), traceVec.end()); if(traceVec.size() > 0) { cout << "trace is: " << endl; ostream_iterator<int> os(cout, " "); copy(traceVec.begin(), traceVec.end(), os); cout << endl; } else cout << "no trace" << endl; system("PAUSE"); return EXIT_SUCCESS;}
每个点有三个方向, 前, 左, 右...
转载地址:http://sbkqb.baihongyu.com/