Noam_15

"Increase_the_array" הבית עם האיקס - כל הפתרונות עם פונקציית

Aug 30th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void the_big_to_the_first(int &n1, int &n2){
  5.     if(n1<n2){
  6.         int tmp = n1;
  7.         n1 = n2;
  8.         n2 = tmp;
  9.     }
  10. }
  11.  
  12.  
  13. class HomeArray {
  14. public:
  15.     int arr[9];
  16.  
  17.     void reset(){
  18.         int i;
  19.         for(i=0; i<9; i++)
  20.             arr[i]=0;
  21.     }
  22.  
  23.     void show(){
  24.         for (int i=0; i<9; i++) cout<<arr[i]<<" ";
  25.         cout<<"\n";
  26.     }
  27.  
  28.     bool is_it_legal(){
  29.         for (int i=0; i<8; i++){
  30.             if(arr[i]==arr[i+1]) return false;
  31.             switch (arr[i]){
  32.             case 0: if(arr[i+1]!=1 && arr[i+1]!=2){ return false;} break;
  33.             case 3: if(arr[i+1]==0){ return false;} break;
  34.             case 4: if(arr[i+1]==0){ return false;} break;
  35.             }
  36.             // עד כאן ווידאנו שיש קווים בצורה של הבית בלבד
  37.  
  38.  
  39.             // מכאן מתחילה בדיקה שאין שני מקלות זהים
  40.             int an1 = arr[i], an2 = arr[i+1];
  41.             the_big_to_the_first(an1, an2);
  42.             // עד כאן המספר הגדול מבניהם נמצא במשתנה 1 והקטן במשתנה 2
  43.  
  44.             int ab1, ab2;
  45.             for(int j=i+1; j<8; j++){
  46.                 ab1 = arr[j], ab2 = arr[j+1];
  47.                 the_big_to_the_first(ab1, ab2);
  48.                 if(ab1==an1 && ab2==an2) return false;
  49.             }
  50.             // עד כאן הבדיקה
  51.  
  52.         }
  53.         return true;
  54.     }
  55. };
  56.  
  57. int Increase_the_array(int *Array, int max, int ind){ // (הפונקציה הזו מתאימה לכל גודל של מערך! (חייבת לקבל מערך שמאותחל כולו לאפסים
  58.     if(ind<0) return  -1;
  59.     if (Array[ind] == max) {
  60.         Array[ind] = 0;
  61.         return Increase_the_array(Array, max, ind-1);
  62.     }
  63.     Array[ind]++;
  64.     return 1;
  65. }
  66.  
  67.  
  68. void main(){
  69.     cout<<"\n   0\n  * *\n *   *\n1*****2\n*     *\n*     *\n3*****4\n\n\n";
  70.     cout<<"\n   0\n  * *\n *   *\n1*****2\n**   **\n* * * *\n*  *  *\n* * * *\n**   **\n3*****4\n";
  71.  
  72.     HomeArray HM[200], htmp;
  73.     int ind=0;
  74.     htmp.reset();
  75.     do{
  76.         if(htmp.is_it_legal() == true){
  77.             HM[ind] = htmp;
  78.             ind++;
  79.         }
  80.     }while(Increase_the_array(htmp.arr, 4, 8)==1);
  81.  
  82.     cout<<"\nThis program checks for all possible solutions to the game.\nFound: "<<ind<<"\n\n";
  83.     for(int i=0; i<ind; i++){
  84.         if(i<9)cout<<" ";
  85.         cout<<i+1<<"@      ";
  86.         HM[i].show();
  87.     }
  88.     cout<<"\n         -END-\n\n";
  89. }
Advertisement
Add Comment
Please, Sign In to add comment