Advertisement
anas_harby

Untitled

Mar 24th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int A[5], vis[5], vis2[5], per[5];
  6. bool possible = false;
  7.  
  8. void solve2(int ans)
  9. {
  10.         bool f = false;
  11.         for(int i=0; i<5; i++)
  12.             if(!vis[i])
  13.                 f = true;
  14.         if(!f)
  15.         {
  16.             if(ans==23)
  17.                 possible = true;
  18.             return;
  19.         }
  20.  
  21.  
  22.     if(!possible)
  23.     {
  24.         for(int i=0; i<5; i++)
  25.         {
  26.             if(!vis[i])
  27.             {
  28.                 vis[i] = true;
  29.                 solve2(ans+per[i]);
  30.                 solve2(ans*per[i]);
  31.                 solve2(ans-per[i]);
  32.                 vis[i] = false;
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. void solve(int k)
  39. {
  40.     if(!possible)
  41.     {
  42.         if(k==5)
  43.             solve2(0);
  44.  
  45.         for(int i=0; i<5; i++)
  46.         {
  47.             if(!vis2[i])
  48.             {
  49.                 vis2[i] = true;
  50.                 per[k] = A[i];
  51.                 solve(k+1);
  52.                 vis2[i] = false;
  53.             }
  54.         }
  55.     }
  56.  
  57. }
  58.  
  59.  
  60.  
  61. int main()
  62. {
  63.  
  64.     for(int i=0; i<26; i++)
  65.     {
  66.         bool f = false;
  67.         for(int j=0; j<5; j++)
  68.         {
  69.             cin >> A[j];
  70.             if(A[j]!=0)
  71.                 f = true;
  72.     }
  73.     if(!f)
  74.         return 0;
  75.     solve(0);
  76.     if(possible)
  77.         cout << "Possible" << endl;
  78.     else
  79.         cout << "Impossible" << endl;
  80.     possible = false;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement