Skip to content

Commit a98619f

Browse files
authored
Create Tower of Hanoi minimum cost DP.cpp
1 parent abc4748 commit a98619f

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

Tower of Hanoi minimum cost DP.cpp

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// https://codeforces.com/contest/392/problem/B
2+
#include<bits/stdc++.h>
3+
///...................................*****.................................................///
4+
/// Author : Raihan Khan Raka ( [email protected] ) ///
5+
/// Department of Computer Science ///
6+
/// & Engineering ///
7+
/// Comilla University , Bangladesh. ///
8+
///...................................*****.................................................///
9+
10+
/*....................................Values................................................*/
11+
#define p5 100007
12+
#define p6 1000007
13+
#define PI acos(-1)
14+
#define M 1000000007
15+
#define inf 1LL << 62
16+
#define white 0
17+
#define gray 1
18+
#define black 2
19+
/*....................................Functions.............................................*/
20+
#define sqr(x) x*x
21+
#define sc scanf
22+
#define pf printf
23+
#define pfn printf("\n")
24+
#define scin(x) sc("%d",&(x))
25+
#define scin2(xx,zz) scanf("%d %d",&xx,&zz)
26+
#define scln(x) sc("%lld",&(x))
27+
#define scln2(xx,zz) scanf("%lld %lld",&xx,&zz)
28+
#define min3(a,b,c) min(a,b<c?b:c)
29+
#define max3(a,b,c) max(a,b>c?b:c)
30+
#define all(v) v.begin(), v.end()
31+
#define ok cout << "ok" << endl
32+
#define mem(x,y) memset(x,y,sizeof(x))
33+
#define clr(a) a.clear()
34+
#define READ(f) freopen(f, "r", stdin)
35+
#define WRITE(f) freopen(f, "w", stdout)
36+
37+
/*...................................Data_Types............................................*/
38+
#define lli long long int
39+
#define ull unsigned long long int
40+
#define pii pair < int, int>
41+
#define pll pair < ll, ll>
42+
#define veci vector<int>
43+
#define vecl vector<long long int>
44+
#define vecp vector< pair<int,int> >
45+
#define mapstrint map< string , int >
46+
#define mapstrstr map< string , string >
47+
#define mapint map< int, int >
48+
#define uset unordered_set
49+
#define umap unordered_map
50+
#define pq priority_queue
51+
52+
#define pb push_back
53+
#define mp make_pair
54+
#define ff first
55+
#define ss second
56+
57+
/*.....................................Loops...............................................*/
58+
#define rep( i , a , b ) for( i=a ; i<b ; i++)
59+
#define rev( i , a , b ) for( i=a ; i>=b ; i--)
60+
#define repx( i ,a,b, x) for( i=a ; i<b ; i+=x)
61+
62+
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
63+
64+
//int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
65+
66+
///------------------------------- Mudular functions----------------------------------------
67+
/*
68+
inline lli power(lli x, lli y){ lli temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; }
69+
inline lli add(lli a, lli b) {a += b; return a >= M ? a - M : a;}
70+
inline lli sub(lli a, lli b) {a -= b; return a < 0 ? a + M : a;}
71+
inline lli mul(lli a, lli b) {return (a * b) % M;}
72+
lli gcd(lli x,lli y)
73+
{
74+
if(x==0) return y;
75+
return gcd(y%x,x);
76+
}
77+
lli bigmod(lli n, lli k )
78+
{
79+
lli ans=1;
80+
while(k)
81+
{
82+
if(k&1)
83+
ans=(ans*n)%M;
84+
k=k>>1;
85+
n=(n*n)%M;
86+
}
87+
88+
return ans;
89+
}
90+
*/
91+
///----------------------------------Graph moves----------------------------------------
92+
/*
93+
int dx4[5] = {1, -1, 0, 0};
94+
int dy4[5] = {0, 0, 1, -1};
95+
int dx8[9] = {0, 0, 1, -1, -1, 1, -1, 1};
96+
int dy8[9] = {-1, 1, 0, 0, 1, 1, -1, -1};
97+
int knightx[9] = {-2, -2, -1, -1, 1, 1, 2, 2};
98+
int knighty[9] = {-1, 1, -2, 2, -2, 2, -1, 1};
99+
bool valid( int r , int c , int x , int y ){ if( x >= 1 && x <= r && y >= 1 && y <= c ) return 1 ; return 0 ; }
100+
*/
101+
102+
using namespace std;
103+
int cost[4][4];
104+
lli dp[43][4][4][4];
105+
106+
lli towerhanoi(int n,int A,int B,int C)
107+
{
108+
if(n==0) return 0;
109+
110+
lli &ret=dp[n][A][B][C];
111+
if(ret) return ret;
112+
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
115+
116+
return ret = min(way1,way2);
117+
118+
}
119+
120+
121+
int main()
122+
{
123+
int n,i,j;
124+
125+
rep(i , 1 , 4)
126+
rep(j , 1 , 4)
127+
scin(cost[i][j]);
128+
129+
scin(n);
130+
131+
lli minimoves= towerhanoi(n,1,3,2); // from,to,aux
132+
133+
cout << minimoves << endl;
134+
135+
136+
#ifdef HOME
137+
cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
138+
#endif
139+
return 0;
140+
}
141+

0 commit comments

Comments
 (0)