void StringManipulation::TrimString(string &str,const string val)
{
str.erase(0,str.find_first_not_of(val));//去掉字串前面的空白符
int pos=str.find_last_not_of(val)+val.size();
if (pos<str.size())
{
str.erase(pos);
}
//去掉字串後面的空白符
}
#pragma once
#include "common.h"
class GraphRepresentation
{
public:
GraphRepresentation(void);
~GraphRepresentation(void);
map<int,string>Vertex;
map<pair<int,int>,string>GraphR;
void GetPaths(vector<vector<int>>&paths,int beginindex,int endindex);
};
#include "StdAfx.h"
#include "GraphRepresentation.h"
GraphRepresentation::GraphRepresentation(void)
{
}
GraphRepresentation::~GraphRepresentation(void)
{
}
void GraphRepresentation:: GetPaths(vector<vector<int>>&paths,int beginindex,int endindex)
{
stack<int>mystack;
vector<int> singlepath;
int *vertexsStatus=new int[Vertex.size()];//0,未在棧內,1已經在棧內。
map<pair<int,int>,int>arcstatus;
for (int i=0;i<Vertex.size();i++)
{
vertexsStatus[i]=0;
}
for (map<pair<int,int>,string>::iterator it=GraphR.begin();it!=GraphR.end();it++)
{
arcstatus[it->first]=0;
}
mystack.push(beginindex);
vertexsStatus[beginindex]=1;
int justpopup=-1;//儲存剛剛出棧的元素
while(!mystack.empty())
{
int elem=mystack.top();
if (elem==endindex)//出棧的第一個條件找到了目的節點
{ //以下程式碼完成棧的遍歷
while(!mystack.empty())
{
int tmp=mystack.top();
mystack.pop();
singlepath.push_back(tmp);
}
for (vector<int>::reverse_iterator rit=singlepath.rbegin();rit!=singlepath.rend();rit++)
{
mystack.push(*rit);
}
paths.push_back(singlepath);
singlepath.clear();
vertexsStatus[elem]=0;
for (int k=0;k<Vertex.size();k++)
{
if (vertexsStatus[k]==0)
{
if (arcstatus.count(make_pair(elem,k)))
{
arcstatus[make_pair(elem,k)]=0;
}
}
}
mystack.pop();
}
else
{ int i=0;
for (;i<Vertex.size();i++)
{
if (GraphR.count(make_pair(elem,i))&&vertexsStatus[i]==0&&arcstatus[make_pair(elem,i)]==0)
{
mystack.push(i);
vertexsStatus[i]=1;
arcstatus[make_pair(elem,i)]=1;
//graphR.erase(make_pair(elem,i));
break;
}
}
if (i==Vertex.size())//出棧的第二個條件,沒有可以往棧內新增的後續節點了。
{
int elemtmp=mystack.top();
vertexsStatus[elemtmp]=0;
for (int k=0;k<Vertex.size();k++)
{
if (vertexsStatus[k]==0)
{
if (arcstatus.count(make_pair(elemtmp,k)))
{
arcstatus[make_pair(elemtmp,k)]=0;
}
}
}
mystack.pop();
}
}
}
delete vertexsStatus;
}