Skip to content

Commit

Permalink
Create PetersonGraph.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhinavSharma24 authored Oct 1, 2020
1 parent 44deaa8 commit 820180a
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions PetersonGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <bits/stdc++.h>
using namespace std;

char S[100005];

bool adj[10][10];

char result[100005];

bool findthepath(char* S, int v)
{
result[0] = v + '0';
for (int i = 1; S[i]; i++) {

if (adj[v][S[i] - 'A'] || adj[S[i] -
'A'][v]) {
v = S[i] - 'A';
}

else if (adj[v][S[i] - 'A' + 5] ||
adj[S[i] - 'A' + 5][v]) {
v = S[i] - 'A' + 5;
}

else
return false;

result[i] = v + '0';
}

return true;
}

int main()
{
adj[0][1] = adj[1][2] = adj[2][3] =
adj[3][4] = adj[4][0] = adj[0][5] =
adj[1][6] = adj[2][7] = adj[3][8] =
adj[4][9] = adj[5][7] = adj[7][9] =
adj[9][6] = adj[6][8] = adj[8][5] = true;

char S[] = "ABB";

if (findthepath(S, S[0] - 'A') ||
findthepath(S, S[0] - 'A' + 5)) {
cout << result;
}

else {
cout << "-1";
}
return 0;
}

0 comments on commit 820180a

Please sign in to comment.