Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- char testData[][2][6]= {
- {"dc","end"},
- {"HN","start"},
- {"start","kj"},
- {"dc","start"},
- {"dc","HN"},
- {"LN","dc"},
- {"HN","end"},
- {"kj","sa"},
- {"kj","HN"},
- {"kj","dc"}};
- char data[][2][6]= {
- {"end","MY"},
- {"MY","xc"},
- {"ho","NF"},
- {"start","ho"},
- {"NF","xc"},
- {"NF","yf"},
- {"end","yf"},
- {"xc","TP"},
- {"MY","qo"},
- {"yf","TP"},
- {"dc","NF"},
- {"dc","xc"},
- {"start","dc"},
- {"yf","MY"},
- {"MY","ho"},
- {"EM","uh"},
- {"xc","yf"},
- {"ho","dc"},
- {"uh","NF"},
- {"yf","ho"},
- {"end","uh"},
- {"start","NF"}
- };
- typedef struct Node {
- int id;
- int visited = 0;
- bool bigRoom = false;
- int nodes[10];
- int index = 0;
- };
- typedef struct Graph{
- Node nodes[20];
- char nodeDict[20][6];
- int index = 0;
- };
- Graph graph;
- int getNode(char* a){
- for(int i = 0; i < graph.index; i++){
- if(strcmp(graph.nodeDict[i], a) == 0){
- return i;
- }
- }
- }
- void addToGraph(char* a){
- for(int i = 0; i < graph.index; i++){
- if(strcmp(graph.nodeDict[i], a) == 0){
- return;
- }
- }
- graph.nodes[graph.index].id = graph.index;
- if(isUpperCase(a[0]))graph.nodes[graph.index].bigRoom = true;
- strcpy(graph.nodeDict[graph.index], a);
- graph.index++;
- }
- void addToNode(Node &na, Node &nb){
- na.nodes[na.index]=nb.id;
- na.index++;
- nb.nodes[nb.index]=na.id;
- nb.index++;
- }
- void addConnection(char* a, char* b){
- addToGraph(a);
- addToGraph(b);
- addToNode(graph.nodes[getNode(a)],graph.nodes[getNode(b)]);
- }
- void searchpath(Node &node, long& count, bool visitedTwice)
- {
- node.visited++;
- if (node.id == 1)
- {
- count++;
- node.visited = 0;
- return;
- }
- for (int index = 0; index < node.index; index++)
- {
- Node n = graph.nodes[node.nodes[index]];
- if (n.id == 0)
- continue;
- bool visited = visitedTwice;
- if (n.visited > 0 && n.bigRoom == false && visitedTwice)
- continue;
- else if (n.visited > 0 && n.bigRoom == false )
- visited = true;
- searchpath(graph.nodes[node.nodes[index]], count,visited);
- }
- node.visited--;
- }
- void setup() {
- Serial.begin(9600);
- while(!Serial);
- addToGraph("start");
- addToGraph("end");
- for (int i=0; i<sizeof data/sizeof data[0]; i++) {
- char* s = data[i][0];
- char* e = data[i][1];
- addConnection(s,e);
- }
- long count = 0;
- long time = millis();
- searchpath(graph.nodes[0], count, true);
- Serial.print("Nummber of routes part 1: ");
- Serial.println(count);
- Serial.print("Millisec: ");
- Serial.println(millis() - time);
- for(int i = 0; i < graph.index; i++){
- graph.nodes[i].visited = 0;
- }
- count = 0;
- time = millis();
- searchpath(graph.nodes[0], count, false);
- Serial.print("Nummber of routes part 2: ");
- Serial.println(count);
- Serial.print("Millisec: ");
- Serial.println(millis() - time);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- }
Add Comment
Please, Sign In to add comment