图数据结构的所有可能遍历路线算法实现-①利用索引②利用节点指针

// 游历魔法王国.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;


class mapNode
{
public:
	int index;
	string strName;
	int nextCnt;
	bool isTra;
	vector<mapNode*> nextNode;//下一个节点的集合

	mapNode(int _index,string _name) //构造函数初始化
	{
		index = _index;
		strName = _name;
		isTra=false;
		nextCnt = 0;
	}

	void GetNextNode(mapNode* pNext) //获取下一个与该节点相连的点
	{
		nextNode.push_back(pNext);
		nextCnt++;
	}

	void DeepTra(int a) //深度优先遍历
	{
		a++;
		cout << strName << endl;
		this->isTra = true; //该节点已被遍历
		for (int i = 0; i < nextCnt; i++)
		{
			if (nextNode.at(i)->isTra)continue; //是否该节点已被遍历
			nextNode.at(i)->DeepTra(a);
		}
	}



};

class MagicCon
{
public:
	vector<mapNode*> city; //这里最好用指针的集合,否则会内存中会由两个该对象
	int cityCnt;
	int maxCnt = 0;
	MagicCon()
	{
		cityCnt = 0;
	}

	void RsetCity() //重置城市是否被遍历
	{
		if (cityCnt == 0)return;
		for (int i = 0; i < cityCnt; i++)
		{
			city.at(i)->isTra = false;
		}
	}

	void AddCity( mapNode &_city) //添加城市
	{
		city.push_back(&_city);
		cityCnt++;
	}

	void TraCity(int _index) //从指定索引的城市开始遍历  深度优先
	{
		if (cityCnt == 0)return;
		for (int i = 0; i < cityCnt; i++)
		{
			if (city.at(i)->index == _index)
			{
				city.at(i)->DeepTra(0);
				break;
			}
		}
		RsetCity();
	}

	void GetMaxTra()//利用索引获取最大生成树
	{
		for (int i = 0; i <(int) city.size(); i++)
		{
			RsetCity();
			vector<int> chosed;
			AllTra(chosed, i);
		}
		cout << maxCnt << endl;
	}

	void GetMaxTra1()//利用指针获取最大生成树
	{
		for (int i = 0; i <(int)city.size(); i++)
		{
			RsetCity();
			vector<mapNode *> chosed;
			AllTra(chosed, city[i]);
		}
		cout << maxCnt << endl;
	}

	void AllTra(vector<int> chosed,int index)   //利用索引寻找最大生成树
	{
		chosed.push_back(index);
		if ((int)chosed.size() > maxCnt)
			maxCnt = (int)chosed.size();

		if ((int)chosed.size() == cityCnt)
		{
			prinVec(chosed); //打印一下城市遍历的顺序
			return;
		}

		ResetChosed(chosed);//初始化已走过和没走过的城市
		for (int i = 0; i <city[index]->nextCnt; i++)
		{
			if (city[index]->nextNode.at(i)->isTra)continue; //是否该节点已被遍历
			int Tindex = city[index]->nextNode[i]->index;
			AllTra(chosed, Tindex);  //进入当前节点的下一个节点
			ResetChosed(chosed);
		}
		//prinVec(chosed);
		//if (a > maxCnt)maxCnt = a;
	}


	void AllTra(vector<mapNode *>  chosed,  mapNode * start)  //利用指针集合寻找最大生成树
	{
		chosed.push_back(start);

		// start->isTra = true; //该节点已被遍历
		if ((int)chosed.size() > maxCnt)
			maxCnt = (int)chosed.size();

		if ((int)chosed.size() == cityCnt)
		{
			prinVec(chosed); //打印一下城市遍历的顺序
			return;
		}

		ResetChosed(chosed);//初始化已走过和没走过的城市
		for (int i = 0; i <start->nextCnt; i++)
		{
			if (start->nextNode[i]->isTra)continue; //是否该节点已被遍历	
			mapNode *p = NULL;
			p = start->nextNode[i];
			AllTra(chosed, p);  //进入当前节点的下一个节点
			ResetChosed(chosed);
		}
	}

	void ResetChosed(vector<int> chosed)//重置已走过的索引集合
	{
		for (int i = 0; i < cityCnt; i++)
		{
			for (int j = 0; j <(int) chosed.size(); j++)
			{
				if (i== chosed[j])
				{
					city[i]->isTra = true;
					break;
				}
				else
				{
					city[i]->isTra = false;
				}
			}
		}
	}

	void ResetChosed(vector<mapNode * > chosed)//重置已走过的指针集合
	{
		for (int i = 0; i < cityCnt; i++)
		{
			for (int j = 0; j <(int)chosed.size(); j++)
			{
				if (city[i] == chosed[j])
				{
					city[i]->isTra = true;
					break;
				}
				else
				{
					city[i]->isTra = false;
				}
			}
		}
	}

	void CityRel(mapNode &c1, mapNode &c2)//建立城市的链接关系
	{
		c1.GetNextNode(&c2);
		c2.GetNextNode(&c1);
	}

	void prinVec(vector<int> chosed)//打印索引集合
	{
		for (int j = 0; j < (int)chosed.size(); j++)
		{
			cout << city[chosed[j]]->strName << " ";
		}
		cout << endl;
	}

	void prinVec(vector<mapNode*> chosed)//打印指针集合
	{
		for (int j = 0; j < (int)chosed.size(); j++)
		{
			cout << chosed[j]->strName << " ";
		}
		cout << endl;
	}
};

int main()
{
	MagicCon cn;
	mapNode m1(0, "m1");
	mapNode m2(1, "m2");
	mapNode m3(2, "m3");
	mapNode m4(3, "m4");
	mapNode m5(4, "m5");

	cn.CityRel(m1, m2);
	cn.CityRel(m1, m3);
	cn.CityRel(m2, m5);
	cn.CityRel(m2, m4);
	cn.CityRel(m4, m3);
	cn.AddCity(m1);
	cn.AddCity(m2);
	cn.AddCity(m3);
	cn.AddCity(m4);
	cn.AddCity(m5);

	cn.TraCity(0);
	//cn.GetMaxTra()//利用索引
	cn.GetMaxTra1();//利用指针


	system("pause");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值