Skip to content

Commit fd1d1f4

Browse files
authored
Update Tower of Hanoi minimum cost DP.cpp
1 parent a98619f commit fd1d1f4

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Tower of Hanoi minimum cost DP.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ lli towerhanoi(int n,int A,int B,int C)
110110
lli &ret=dp[n][A][B][C];
111111
if(ret) return ret;
112112

113-
lli way1 = towerhanoi(n-1,A,C,B) + cost[A][B] + towerhanoi(n-1,C,B,A); // move n-1 disks A-C then nth disk A-B then return n-1 disks C-A
114-
lli way2 = towerhanoi(n-1,A,B,C) + cost[A][C] + towerhanoi(n-1,B,A,C) + cost[C][B] + towerhanoi(n-1,A,B,C); //move n-1 disks A-B then nth disk A-C then return n-1 disks B-A then nth disk C-B then n-1 disks A-B
113+
lli way1 = towerhanoi(n-1,A,C,B) + cost[A][B] + towerhanoi(n-1,C,B,A);
114+
// move n-1 disks A-C then nth disk A-B then return n-1 disks C-A
115+
lli way2 = towerhanoi(n-1,A,B,C) + cost[A][C] + towerhanoi(n-1,B,A,C) + cost[C][B] + towerhanoi(n-1,A,B,C);
116+
//move n-1 disks A-B then nth disk A-C then return n-1 disks B-A then nth disk C-B then n-1 disks A-B
115117

116118
return ret = min(way1,way2);
117119

0 commit comments

Comments
 (0)