fork(60) download
  1. #include <iostream>
  2. #include <climits>
  3. using namespace std;
  4.  
  5.  
  6. int min_squares(int m, int n) {
  7. if (m == n)
  8. return 1;
  9.  
  10. static int cache[100][100]; // Automatically initialized to 0 since static
  11.  
  12. if (m < n)
  13. swap(m, n);
  14. if (cache[m][n])
  15. return cache[m][n];
  16.  
  17. int x = INT_MAX;
  18.  
  19. for (int i = 1; i+i <= n; ++i)
  20. x = min(x, min_squares(m, i) + min_squares(m, n-i));
  21.  
  22. for (int i = 1; i+i <= m; ++i)
  23. x = min(x, min_squares(i, n) + min_squares(m-i, n));
  24.  
  25. return cache[m][n] = x;
  26. }
  27.  
  28.  
  29. int main() {
  30. int m = 6;
  31. int n = 5;
  32.  
  33. cout << min_squares(m, n) << endl;
  34. }
  35.  
Success #stdin #stdout 0s 3376KB
stdin
Standard input is empty
stdout
5