Line maze solver

how this robot can save the road? i mean how it can recognise the road at the second run?

At its simplist all you need is tree data structure where you have a node for each intersection. Each node will have 3 pointer to the node for the next intersection if you go left, right, or straight. That will only work if the maze has no loops though.

From that point, there's many ways you can find the optimal path. You can have links back to the parent nodes in your data structure, or you can apply Dijkstra's shortest path algorithm to it, just as a couple of example.