unlucky_13

uva_10276 Hanoi Tower Troubles Again!

May 30th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. /*
  2.  Author                             : unlucky_13
  3.  Problem_link                       :
  4.  Category                           :
  5.  Algorithm_Used                     : Backtracking
  6.  */
  7.  
  8. #include<cstdio>
  9. #include<sstream>
  10. #include<cstdlib>
  11. #include<cctype>
  12. #include<cmath>
  13. #include<algorithm>
  14. #include<set>
  15. #include<queue>
  16. #include<stack>
  17. #include<list>
  18. #include<iostream>
  19. #include<fstream>
  20. #include<numeric>
  21. #include<string>
  22. #include<vector>
  23. #include<cstring>
  24. #include<map>
  25. #include<iterator>
  26. #define LL long long int
  27. //const long long int inf = 2147483647 ;
  28. //const int minx=;
  29. //const int maxn=;
  30.  
  31. using namespace std;
  32. int res ,peg;
  33. int v[111] ;
  34. bool is_perfect(const int& x) {
  35.     int y = (int) sqrt(x * 1.0);
  36.     return y * y == x;
  37. }
  38.  
  39. void backtrack(int n){
  40.    
  41.     bool flag = false ;
  42.     int i = 1 ;
  43.     while(!flag && i<=peg)
  44.     {
  45.         if(v[i]==-1 || is_perfect(v[i]+n)) flag = true ;
  46.         else i++ ;
  47.     }
  48.    
  49.     if(flag==false)   res = max(res,n-1) ; //no move was possible
  50.     else {
  51.         v[i]=n ;
  52.         backtrack(n+1) ;
  53.     }
  54.  
  55.  
  56. }
  57.  
  58. int main() {
  59.  
  60.     freopen("C:\\Users\\Mazhar\\Desktop\\Text_Files\\in.txt", "r", stdin);
  61.     int tc ;
  62.     scanf("%d",&tc) ;
  63.     while(tc--)
  64.     {
  65.        
  66.         res = 0 ;
  67.         scanf("%d",&peg) ;
  68.         for(int i=1;i<=peg;i++) v[i]=-1 ;
  69.         backtrack(1) ;
  70.         printf("%d\n",res) ;
  71.     }
  72.  
  73.  
  74.  
  75.     return 0;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment