BFS for RGB puzzle.

I solve RGB puzzle using BFS, it is working correctly but i need to print the route which it used to find out the solution. I am a little bad with pointers, the parent pointer of the node i have is not traversing at all. It is always giving the last node as output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
void BreadthFS(Node obj){
           queue <Node> puzzle;
           obj.parent=NULL;
           Node temp=obj;
           puzzle.push(obj);
           Random func;
           func.generategoal();
           
           while(!puzzle.empty()){
                                                                
                                  
                                  //MoveUP
                                  temp=obj;
                                  temp.findblank();
                                  if(temp.row>0){
                                                 temp.moveup();
                                                 func.printobj(temp);
                                                 temp.route='U';
                                                 cout<<endl<<"Moved Up"<<endl;
                                                 if(func.comparegoal(temp)==true){
                                                                                 cout<<"Goal State Reached"<<endl;
                                                                                 while(temp.parent!=NULL){
                                                                                                          cout<<temp.route<<endl;
                                                                                                          temp.parent=temp.parent->parent;
                                                                                                          func.printobj(temp);
                                                                                                          system("pause");
                                                                                                          }
                                                                                 break;
                                                                                 }
                                                 temp.parent=&obj;
                                                 puzzle.push(temp);
                                                 }
                                  
                                  //MoveDown
                                  temp=obj;
                                  temp.findblank();
                                  if(temp.row<4){
                                                 temp.movedown();
                                                 func.printobj(temp);
                                                 temp.route='D';
                                                 cout<<endl<<"Moved Down"<<endl;
                                                 if(func.comparegoal(temp)==true){
                                                                                 cout<<"Goal State Reached"<<endl;
                                                                                 while(temp.parent!=NULL){
                                                                                                          cout<<temp.route<<endl;
                                                                                                          temp.parent=temp.parent->parent;
                                                                                                          func.printobj(temp);
                                                                                                          system("pause");
                                                                                                          }
                                                                                 break;
                                                                                 }
                                                 temp.parent=&obj;
                                                 puzzle.push(temp);
                                                 }
                                  
                                  //MoveLeft
                                  temp=obj;
                                  temp.findblank();
                                  if(temp.col>0){
                                                 temp.moveleft();
                                                 func.printobj(temp);
                                                 temp.route='L';
                                                 cout<<endl<<"Moved Left"<<endl;
                                                 if(func.comparegoal(temp)==true){
                                                                                 cout<<"Goal State Reached"<<endl;
                                                                                 while(temp.parent!=NULL){
                                                                                                          cout<<temp.parent->route<<endl;
                                                                                                          temp.parent=temp.parent->parent;
                                                                                                          func.printobj(temp);
                                                                                                          system("pause");
                                                                                                          }
                                                                                 break;
                                                                                 }
                                                 temp.parent=&obj;
                                                 puzzle.push(temp);
                                                 }
                                  
                                  //MoveRight
                                  temp=obj;
                                  temp.findblank();
                                  if(temp.col<4){
                                                 temp.moveright();
                                                 func.printobj(temp);
                                                 temp.route='R';
                                                 cout<<endl<<"Moved Right"<<endl;
                                                 if(func.comparegoal(temp)==true){
                                                                                 cout<<"Goal State Reached"<<endl;
                                                                                 while(temp.parent!=NULL){
                                                                                                          cout<<temp.route<<endl;
                                                                                                          temp.parent=temp.parent->parent;
                                                                                                          func.printobj(temp);
                                                                                                          system("pause");
                                                                                                          }
                                                                                 break;
                                                                                 }
                                                 temp.parent=&obj;
                                                 puzzle.push(temp);
                                                 }                            
                                  puzzle.pop();
                                  obj=puzzle.front();
                                  }
           }
Please indent your code properly. It is unreadable in its current form.
Thank you!
@theturk1234

Leave the indentation, i just want help with this
1
2
3
4
5
6
while(temp.parent!=NULL){
                                                                                                          cout<<temp.route<<endl;
                                                                                                          temp.parent=temp.parent->parent;
                                                                                                          func.printobj(temp);
                                                                                                          system("pause");
                                                                                                          }


What am i doing wrong here, why isn't it traversing back?
spacer15:
On line 3 you wrote:
obj.parent=NULL;
And as far as I can tell, you don't assign it any other value. Naturally the while loop while(temp.parent!=NULL){ is going to break out after the first while iteration because you assign obj to temp.

On line 68 you wrote:
temp.parent=temp.parent->parent;
You only use the -> operator when you are using it on a pointer. temp is not a pointer. In this case, you should use
temp.parent=temp.parent; but that statement is redundant. You need to change the temp variable before you try to determine if its parent is not NULL. Does that answer your question?
Last edited on
@theturk1234

Well I didn't apply pointers anymore, I just made a string with each node object and appended it. It completed the solution. Well thanks for the help though. Topic closed. Bless you sir.
Topic archived. No new replies allowed.