Sie sind auf Seite 1von 2

#include<iostream>

#include<list>
#include<queue>
#include<unordered_map>
#include<climits>
#include<vector>
#include<algorithm>
using namespace std;

template<typename T>

class Graph
{
unordered_map<T,list<T> > adjList;

public:

void addEdge(T u,T v,bool bidir=true)


{
adjList[u].push_back(v);

if(bidir==true)
adjList[v].push_back(u);
}

void bfs(T src,T dest)


{
queue<T> q;
unordered_map<T,int> dist;
unordered_map<T,T> parent;

for(auto i:adjList)
dist[i.first]=INT_MAX;

q.push(src);
dist[src]=0;
parent[src]=src;

while(!q.empty())
{
T node=q.front();
q.pop();

for(auto i:adjList[node])
{
if(dist[i]==INT_MAX)
{
q.push(i);
dist[i]=dist[node]+1;
parent[i]=node;
}
}
}

T temp=dest;
vector<int> path;

while(temp!=src)
{
path.push_back(temp);
temp=parent[temp];
}
path.push_back(src);

reverse(path.begin(),path.end());

cout<<"The Path is: "<<endl;

vector<int>::iterator it;
for(it=path.begin();it!=path.end();it++)
cout<<*it<<"-> ";
}
};

int main()
{
Graph<int> g;

int board[50]={0};

board[2]=13;
board[5]=2;
board[9]=18;
board[18]=11;
board[17]=-13;
board[20]=-14;
board[24]=-8;
board[25]=-10;
board[32]=-2;
board[34]=-22;

for(int i=0;i<=36;i++)
{
for(int dice=1;dice<=6;dice++)
{
int v=i+dice+board[i+dice];
g.addEdge(i,v,false);
}
}

g.bfs(1,36);

return 0;
}

Das könnte Ihnen auch gefallen