Advertisement
Guest User

bon il marche pas trop trop masi au moins il core cumped plu

a guest
Jan 28th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.82 KB | None | 0 0
  1. /*
  2. Suduko Solver
  3. by someone
  4.  
  5. examples of sudoku available at http://magictour.free.fr/top1465
  6. */
  7.  
  8. #include <iostream>
  9. #include <string>
  10. //#include <array>
  11. #include <algorithm>
  12. #include <exception>
  13. #include <cctype>
  14. #include <vector>
  15. #include <memory>
  16. #include <utility>
  17.  
  18. class InitNbrSudokuError : public std::runtime_error
  19. {
  20. public :
  21.  
  22.     InitNbrSudokuError(std::string str) : std::runtime_error(str) {}
  23.  
  24.     enum TypeErr
  25.     {
  26.         err_alloc
  27.     };
  28.    
  29.     TypeErr getErrType()
  30.     {
  31.         return err_alloc;
  32.     }
  33. };
  34.  
  35. class NbrSudoku
  36. {
  37. public :
  38.    
  39.     NbrSudoku();
  40.     void init(int val);
  41.    
  42.     NbrSudoku(const NbrSudoku& val);
  43.    
  44.     int getNbr() const;
  45.     std::vector<int>& getVec() const;
  46.    
  47.     bool isValid() const;
  48.    
  49.     void addLittleNbr(int val);
  50.    
  51.     void putOffLitteNbr(int val);
  52.    
  53.     void set(int val);
  54.    
  55.     void setIfOnlyOne();
  56.    
  57. private :
  58.  
  59.     int nbr;
  60.    
  61.     std::unique_ptr<std::vector<int>> listInt;
  62. };
  63.  
  64. std::ostream& operator<<(std::ostream& outuput, NbrSudoku a);
  65. //external overloading lul
  66.  
  67. NbrSudoku::NbrSudoku()
  68. {
  69.     listInt = nullptr;
  70. }
  71.  
  72. NbrSudoku::NbrSudoku(const NbrSudoku& val)
  73. {
  74.     nbr = val.nbr;
  75.    
  76.     try
  77.     {
  78.         listInt = std::unique_ptr<std::vector<int>>(new std::vector<int> {val.getVec()});
  79.     }
  80.     catch (const InitNbrSudokuError& e)
  81.     {
  82.         listInt = std::unique_ptr<std::vector<int>>(new std::vector<int> {});
  83.     }
  84. }
  85.  
  86. void NbrSudoku::addLittleNbr(int val)
  87. {
  88.     listInt->push_back(val);
  89. }
  90.  
  91. int NbrSudoku::getNbr() const
  92. {
  93.     return nbr;
  94. }
  95.  
  96. bool NbrSudoku::isValid() const
  97. {
  98.     if(nbr == 0)
  99.     {
  100.         return false; //the vector is used
  101.     }
  102.     else
  103.     {
  104.         return true;
  105.     }
  106. }
  107.  
  108. std::vector<int>& NbrSudoku::getVec() const
  109. {
  110.     if(listInt != nullptr)
  111.     {
  112.         return *listInt;
  113.     }
  114.     else
  115.     {
  116.         throw InitNbrSudokuError("There aren't any vector to give here, sorry");
  117.     }
  118. }
  119.  
  120. void NbrSudoku::init(int val)
  121. {
  122.     nbr = val;
  123.        
  124.     if(nbr == 0) //if the case is empty
  125.     {
  126.         listInt = std::unique_ptr<std::vector<int>>(new std::vector<int> {}) ;
  127.     }
  128. }
  129.  
  130. std::ostream& operator<<(std::ostream& outuput, NbrSudoku a)
  131. {
  132.     if(a.isValid())
  133.     {
  134.         outuput << a.getNbr();
  135.     }
  136.     else
  137.     {
  138.         for(unsigned int i = 0; i < a.getVec().size() ; i++)
  139.         {
  140.             outuput << a.getVec()[i] << " ";
  141.         }
  142.     }
  143.    
  144.     return outuput;
  145. }
  146.  
  147. void NbrSudoku::putOffLitteNbr(int val)
  148. {
  149.     std::vector<int>::iterator listVirer
  150.         {std::find_if(listInt->begin(), listInt->end(),
  151.             [val](int val_) -> bool {return val == val_;})};
  152.    
  153.     if(listVirer != listInt->end())
  154.     {
  155.         listInt->erase(listVirer);
  156.     }
  157. }
  158.  
  159. void NbrSudoku::set(int val)
  160. {
  161.     if(val >= 0 && val <= 10)
  162.     {
  163.         nbr = val;
  164.     }
  165. }
  166.  
  167. class Sudoku
  168. {
  169. public :
  170.  
  171.     Sudoku(std::string sentence);
  172.    
  173.     std::string toString();
  174.    
  175.     bool isValid();
  176.    
  177.     void addLittleNbrEveywhere();
  178.     void addLittleNbrForOne(int x, int y);
  179.    
  180.     void addBigNbr(int x, int y, int val);
  181.    
  182.     std::ostream& coutLittleNbr(std::ostream& prut);
  183.     std::ostream& coutLittleNbrInGrid(std::ostream& prut);
  184.     std::ostream& coutOneLittleNbrInGrid(std::ostream& prut, int nbr);
  185.    
  186.     void solve(); //c'est si symple que ça
  187.    
  188.     bool setIfOnlyOne();
  189.     bool setIfOnlyOneLineRowCase();
  190.     bool putOffLittleNbrIfOnlyInLine();
  191.     bool putOffLittleNbrIfOnlyInCase();
  192.    
  193. private :
  194.  
  195.     std::vector<std::vector<NbrSudoku>> listNbr;
  196.     //Wide spaces are saved into 0
  197. };
  198.  
  199. Sudoku::Sudoku(std::string sentence)
  200. {
  201.     listNbr  = std::vector<std::vector<NbrSudoku>>
  202.                 {9, std::vector<NbrSudoku>
  203.                     {9, NbrSudoku {/* default constructor */}   }  };
  204.  
  205.     if(sentence.size() != 81 ||
  206.         !std::any_of(sentence.begin(), sentence.end(), [](char a) -> bool
  207.             {
  208.                 if(!isdigit(a) || a != '.')
  209.                 {
  210.                     return true;
  211.                 }
  212.                
  213.                 return false;
  214.             }) )
  215.     {
  216.         throw std::runtime_error("This isn't a valid input");
  217.     }
  218.    
  219.     for(unsigned int i = 0; i < listNbr.size() ; i++)
  220.     {
  221.         for(unsigned int j = 0; j < listNbr[i].size() ; j++)
  222.         {
  223.             char nbr = sentence[i * 9 + j];
  224.            
  225.             if(isdigit(nbr))
  226.             {
  227.                 listNbr[i][j].init(nbr - '0');
  228.             }
  229.             else if(nbr == '.')
  230.             {
  231.                 listNbr[i][j].init(0);
  232.             }
  233.             else
  234.             {
  235.                 throw std::runtime_error("Something went wrong");
  236.             }
  237.         }
  238.     }
  239. }
  240.  
  241. std::string Sudoku::toString()
  242. {
  243.     std::string output;
  244.    
  245.     for(unsigned int i = 0; i < listNbr.size() ; i++)
  246.     {
  247.         if(i % 3 == 0)
  248.         {
  249.             output += "+ - - - + - - - + - - - +\n";
  250.         }
  251.        
  252.         for(unsigned int j = 0 ; j < listNbr.size() ; j++)
  253.         {
  254.             if(j % 3 == 0)
  255.             {
  256.                 output += "| ";
  257.             }
  258.            
  259.             if(listNbr[i][j].getNbr() == 0)
  260.             {
  261.                 output += ".";
  262.             }
  263.             else
  264.             {
  265.                 output += std::to_string(listNbr[i][j].getNbr());
  266.             }
  267.            
  268.             output += " ";
  269.         }
  270.         output += "|\n";
  271.     }
  272.    
  273.     output += "+ - - - + - - - + - - - +\n";
  274.    
  275.     return output;
  276. }
  277.  
  278. bool Sudoku::isValid()
  279. {
  280.    
  281.    
  282.     //try for each line
  283.     for(unsigned int i = 0 ; i < listNbr.size() ; i++)
  284.     {
  285.         std::vector<int> listFind;
  286.        
  287.         for(unsigned int j = 0 ; j < listNbr[i].size() ; j++)
  288.         {
  289.             int val = listNbr[i][j].getNbr();
  290.            
  291.             listFind.push_back(val);
  292.            
  293.             if(any_of(listFind.begin(), listFind.end(),
  294.                 [val](int nbrTest) -> bool
  295.                 {
  296.                     return nbrTest == val;
  297.                 }))
  298.             {
  299.                 //the "pitu" come back
  300.                 std::cout << "pitu" << std::endl;
  301.                
  302.                 return false;
  303.             }
  304.         }
  305.        
  306.         if(listFind.size() != 9)
  307.         {
  308.             return false;
  309.         }
  310.     }
  311.    
  312.     //try for each row
  313.     for(unsigned int i = 0 ; i < listNbr[0].size() ; i++)
  314.     {
  315.         std::vector<int> listFind;
  316.        
  317.         for(unsigned int j = 0 ; j < listNbr.size() ; j++)
  318.         {
  319.             int val {listNbr[j][i].getNbr()};
  320.            
  321.             listFind.push_back(val);
  322.            
  323.             if(std::any_of(listFind.begin(), listFind.end(),
  324.                 [val](int nbrTest) -> bool
  325.                 {
  326.                     return nbrTest == val;
  327.                 }))
  328.             {
  329.                 std::cout << "pruti" << std::endl;
  330.                 return false; //salut ca va
  331.             }
  332.         }
  333.        
  334.         if(listFind.size() != 9)
  335.         {
  336.             return false;
  337.         }
  338.     }
  339.    
  340.     //the hard part : the cases
  341.     for(unsigned int nbrCase = 0; nbrCase < 9 ; nbrCase++)
  342.     {
  343.         std::vector<int> listFind;
  344.         for(unsigned int i = nbrCase % 3 * 3; i < (nbrCase) % 3 * 3 + 3; i++)
  345.         {
  346.             std::vector<int> listFind;
  347.            
  348.             for(unsigned int j = nbrCase / 3 * 3;
  349.                     j < (nbrCase) / 3 * 3 + 3; j++)
  350.             {
  351.                 int val {listNbr[j][i].getNbr()};
  352.                
  353.                 listFind.push_back(val);
  354.            
  355.                 if(std::any_of(listFind.begin(), listFind.end(),
  356.                     [val](int nbrTest) -> bool
  357.                     {
  358.                         return nbrTest == val;
  359.                     }))
  360.                 {
  361.                     std::cout << "pruti" << std::endl;
  362.                     return false; //salut ca va
  363.                 }
  364.             }
  365.         }
  366.     }
  367.    
  368.     return true;
  369. }
  370.  
  371. bool isInVect(int valTest, std::vector<int> v)
  372. {
  373.     return std::find(v.begin(), v.end(), valTest) != v.end();
  374. }
  375.  
  376. bool ifIsNotInVect(int valTest, std::vector<int> v)
  377. {
  378.     return !isInVect(valTest, v);
  379. }
  380.  
  381. void Sudoku::addLittleNbrForOne(int x, int y)
  382. {
  383.     std::vector<int> nbrFind;
  384.    
  385.     for(unsigned int i = 0; i < listNbr.size() ; i++)
  386.     {
  387.         if(listNbr[i][y].getNbr() != 0 &&
  388.             ifIsNotInVect(listNbr[i][y].getNbr(), nbrFind) )
  389.         {
  390.             nbrFind.push_back(listNbr[i][y].getNbr());
  391.         }
  392.     }
  393.    
  394.     //for each line
  395.     for(unsigned int j = 0; j < listNbr[x].size() ; j++)
  396.     {
  397.         if(listNbr[x][j].getNbr() != 0 &&
  398.             ifIsNotInVect(listNbr[x][j].getNbr(), nbrFind))
  399.         {
  400.             nbrFind.push_back(listNbr[x][j].getNbr());
  401.         }
  402.     }
  403.    
  404.     //for each case
  405.     int nbrCase = (x / 3) * 3 + (y / 3);
  406.    
  407.     //std::cout << nbrCase << " " << x << " " << y
  408.     //std::cout <<  " " << listNbr[x][y].getNbr() << std::endl;
  409.    
  410.     for(int i = nbrCase % 3 * 3; i < (nbrCase) % 3 * 3 + 3; i++)
  411.     {
  412.         std::vector<int> listFind;
  413.            
  414.         for(int j = nbrCase / 3 * 3; j < (nbrCase) / 3 * 3 + 3; j++)
  415.         {
  416.             if(listNbr[j][i].getNbr() != 0 &&
  417.                 ifIsNotInVect(listNbr[j][i].getNbr(), nbrFind))
  418.             {
  419.                 nbrFind.push_back(listNbr[j][i].getNbr());
  420.             }
  421.            
  422.             //std::cout << listNbr[j][i].getNbr() << " ";
  423.         }
  424.     }
  425.    
  426.     //std::cout << std::endl;
  427.    
  428.     for(int i = 1; i <= 9; i++)
  429.     {
  430.         if(std::count(nbrFind.begin(), nbrFind.end(), i) == 0)
  431.         {
  432.             listNbr[x][y].addLittleNbr(i);
  433.         }
  434.     }
  435. }
  436.  
  437. void Sudoku::addLittleNbrEveywhere()
  438. {
  439.     for(unsigned int i = 0; i < listNbr.size() ; i++)
  440.     {
  441.         for(unsigned int j = 0; j < listNbr[i].size() ; j++)
  442.         {
  443.             if(listNbr[i][j].getNbr() == 0)
  444.             {
  445.                 addLittleNbrForOne(i, j);
  446.             }
  447.         }
  448.     }
  449. }
  450.  
  451. void Sudoku::addBigNbr(int x, int y, int val)
  452. {
  453.     listNbr[x][y].set(val);
  454.    
  455.     for(unsigned int i = 0; i < listNbr.size() ; i++)
  456.     {
  457.         listNbr[i][y].putOffLitteNbr(val);
  458.     }
  459.    
  460.     for(unsigned int j = 0; j < listNbr[x].size(); j++)
  461.     {
  462.         listNbr[x][j].putOffLitteNbr(val);
  463.     }
  464.    
  465.     int nbrCase = (x / 3) * 3 + (y / 3);
  466.    
  467.     for(int i = nbrCase % 3 * 3; i < (nbrCase) % 3 * 3 + 3; i++)
  468.     {
  469.         for(int j = (nbrCase) / 3 * 3; j < (nbrCase) / 3 * 3 + 3; j++)
  470.         {
  471.             listNbr[j][i].putOffLitteNbr(val);
  472.         }
  473.     }
  474. }
  475.  
  476. std::ostream& Sudoku::coutLittleNbr(std::ostream& prut)
  477. {
  478.     for(unsigned int i = 0 ; i < listNbr.size() ; i++)
  479.     {
  480.         for(unsigned int j = 0 ; j < listNbr[i].size() ; j++)
  481.         {
  482.             prut << listNbr[i][j] << std::endl;
  483.         }
  484.         prut << std::endl;
  485.     }
  486.     return prut;
  487. }
  488.  
  489. void Sudoku::solve()
  490. {
  491.     bool haveAChange {true};
  492.    
  493.     while(haveAChange)
  494.     {
  495.         haveAChange = false;
  496.        
  497.         if(setIfOnlyOne() || setIfOnlyOneLineRowCase() ||
  498.             putOffLittleNbrIfOnlyInLine() || putOffLittleNbrIfOnlyInCase())
  499.         {
  500.             haveAChange = true;
  501.         }
  502.     }
  503. }
  504.  
  505. bool Sudoku::setIfOnlyOne()
  506. {
  507.     bool haveAChange {false};
  508.    
  509.     for(unsigned int i = 0; i < listNbr.size(); i++)
  510.     {
  511.         for(unsigned int j = 0 ; j < listNbr[i].size(); j++)
  512.         {
  513.             if(listNbr[i][j].isValid() && listNbr[i][j].getVec().size() == 1)
  514.             {
  515.                 addBigNbr(i, j, listNbr[i][j].getVec().front());
  516.                 haveAChange = true;
  517.             }
  518.         }
  519.     }
  520.    
  521.     return haveAChange;
  522. }
  523.  
  524. bool Sudoku::setIfOnlyOneLineRowCase()
  525. {
  526.     bool haveAChange {false};
  527.    
  528.     for(int nbrTest = 1; nbrTest <= 9; nbrTest++)
  529.     {
  530.         for(unsigned int i = 0; i < listNbr.size(); i++)
  531.         {
  532.             int find = 0;
  533.            
  534.             for(unsigned int j = 0; j < listNbr[i].size(); j++)
  535.             {
  536.                 if(isInVect(nbrTest, listNbr[i][j].getVec()) )
  537.                 {
  538.                     find++;
  539.                 }
  540.             }
  541.            
  542.             if(find == 1)
  543.             {
  544.                 for(unsigned int j = 0; j < listNbr[i].size(); j++)
  545.                 {
  546.                     if(isInVect(nbrTest, listNbr[i][j].getVec()) )
  547.                     {
  548.                         addBigNbr(i, j, nbrTest);
  549.                         haveAChange = true;
  550.                     }
  551.                 }
  552.             }
  553.         }
  554.        
  555.         for(unsigned int j = 0; j < listNbr[0].size(); j++)
  556.         {
  557.             int find = 0;
  558.            
  559.             for(unsigned int i = 0; i < listNbr.size(); i++)
  560.             {
  561.                 if(isInVect(nbrTest, listNbr[i][j].getVec()) )
  562.                 {
  563.                     find++;
  564.                 }
  565.             }
  566.            
  567.             if(find == 1)
  568.             {
  569.                 for(unsigned int i = 0; i < listNbr.size(); i++)
  570.                 {
  571.                     if(isInVect(nbrTest, listNbr[i][j].getVec()) )
  572.                     {
  573.                         addBigNbr(i, j, nbrTest);
  574.                         haveAChange = true;
  575.                     }
  576.                 }
  577.             }
  578.         }
  579.        
  580.         for(int nbrCase = 0; nbrCase < 9; nbrCase++)
  581.         {
  582.             int find = 0;
  583.            
  584.             for(int i = nbrCase % 3 * 3; i < (nbrCase) % 3 * 3 + 3; i++)
  585.             {
  586.                 for(int j = (nbrCase) / 3 * 3; j < (nbrCase) / 3 * 3 + 3; j++)
  587.                 {
  588.                     if(isInVect(nbrTest, listNbr[j][i].getVec()))
  589.                     {
  590.                         find++;
  591.                     }
  592.                 }
  593.             }
  594.            
  595.             if(find == 1)
  596.             {
  597.                 for(int i = nbrCase % 3 * 3; i < (nbrCase) % 3 * 3 + 3; i++)
  598.                 {  
  599.                     for(int j = (nbrCase) / 3 * 3;
  600.                         j < (nbrCase) / 3 * 3 + 3; j++)
  601.                     {
  602.                         if(isInVect(nbrTest, listNbr[j][i].getVec()))
  603.                         {
  604.                             addBigNbr(j, i, nbrTest);
  605.                             haveAChange = true;
  606.                         }
  607.                     }
  608.                 }
  609.             }
  610.         }
  611.     }
  612.    
  613.     return haveAChange;
  614. }
  615.  
  616. std::ostream& Sudoku::coutLittleNbrInGrid(std::ostream& prut)
  617. {
  618.     for(int i = 1; i <= 9; i++)
  619.     {
  620.         coutOneLittleNbrInGrid(prut, i);
  621.         prut << std::endl << std::endl;
  622.     }
  623.    
  624.     return prut;
  625. }
  626.  
  627. std::ostream& Sudoku::coutOneLittleNbrInGrid(std::ostream& output, int nbr)
  628. {
  629.     for(unsigned int i = 0; i < listNbr.size() ; i++)
  630.     {
  631.         if(i % 3 == 0)
  632.         {
  633.             output << "+ - - - + - - - + - - - +" << std::endl;
  634.         }
  635.        
  636.         for(unsigned int j = 0 ; j < listNbr.size() ; j++)
  637.         {
  638.             if(j % 3 == 0)
  639.             {
  640.                 if(j != 0)
  641.                 {
  642.                     output << " ";
  643.                 }
  644.                
  645.                 output << "|";
  646.             }
  647.            
  648.             if(listNbr[i][j].isValid() && listNbr[i][j].getNbr() == nbr)
  649.             {
  650.                 output << "*" << listNbr[i][j].getNbr();
  651.             }
  652.             else if(listNbr[i][j].isValid())
  653.             {
  654.                 output << " _";
  655.             }
  656.             else if(isInVect(nbr, listNbr[i][j].getVec()) && !listNbr[i][j].isValid())
  657.             {
  658.                 output << " " << nbr;
  659.             }
  660.             else
  661.             {
  662.                 output << " .";
  663.             }
  664.         }
  665.         output << " |" << std::endl;
  666.     }
  667.    
  668.     output << "+ - - - + - - - + - - - +" << std::endl;
  669.    
  670.     return output;
  671. }
  672.  
  673. /*
  674. Generic function to find an element in vector and also its position.
  675. It returns a pair of bool & int i.e.
  676.  
  677. bool : Represents if element is present in vector or not.
  678. int : Represents the index of element in vector if its found else -1
  679.  
  680. */
  681.  
  682. template < typename T>
  683. std::pair<bool, int > findInVector(const std::vector<T>  & vecOfElements, const T  & element)
  684. {
  685.     std::pair<bool, int > result;
  686.  
  687.     // Find given element in vector
  688.     auto it = std::find(vecOfElements.begin(), vecOfElements.end(), element);
  689.  
  690.     if (it != vecOfElements.end())
  691.     {
  692.         result.second = distance(vecOfElements.begin(), it);
  693.         result.first = true;
  694.     }
  695.     else
  696.     {
  697.         result.first = false;
  698.         result.second = -1;
  699.     }
  700.  
  701.     return result;
  702. }
  703.  
  704. bool Sudoku::putOffLittleNbrIfOnlyInLine()
  705. {
  706.     bool haveAChange = false;
  707.    
  708.     for(int nbrTest = 1; nbrTest <= 9; nbrTest++) //THIS IS FOR THE LINES
  709.     {
  710.         for(int i = 0; i < listNbr.size(); i++)
  711.         {
  712.             std::vector<bool> isHere {false, false, false};
  713.            
  714.             for(int j = 0; j < listNbr[i].size(); j++)
  715.             {
  716.                 if(isInVect(nbrTest, listNbr[i][j].getVec())
  717.                             && !listNbr[i][j].isValid())
  718.                 {
  719.                     isHere[j / 3] = true;
  720.                     //std::cout << i << " " << j << std::endl;
  721.                 }
  722.             }
  723.            
  724.             if(std::count(isHere.begin(), isHere.end(), true) == 1)
  725.             {
  726.                 std::pair<bool, int> result = findInVector<bool>(isHere, true);
  727.            
  728.                 //then find and put off the useless little numbers
  729.            
  730.                 int caseFind = (i / 3) * 3 + result.second;
  731.                
  732.                 /*
  733.                 std::cout << "finded ! ";
  734.                
  735.                 for(int i = 0; i < isHere.size(); i++)
  736.                 {
  737.                     std::cout << std::boolalpha << isHere[i] << " ";
  738.                 }
  739.                 std::cout << nbrTest << " " << caseFind;
  740.                 std::cout << std::endl;
  741.  
  742.                 coutLittleNbrInGrid(std::cout); */
  743.                
  744.                 for(int x = caseFind / 3 * 3; x < caseFind / 3 * 3 + 3; x++)
  745.                 {
  746.                     for(int y = caseFind % 3 * 3; y < caseFind % 3 * 3 + 3; y++)
  747.                     {
  748.                         if(x != i && isInVect(nbrTest, listNbr[x][y].getVec()))
  749.                         {
  750.                             listNbr[x][y].putOffLitteNbr(nbrTest);
  751.                             haveAChange = true;
  752.                         }
  753.                     }
  754.                 }
  755.                 //yes it's ugly but easier
  756.             }
  757.            
  758.             //so if std::count(isHere.begin(), isHere.end(), nbrTest) == 1 is true
  759.            
  760.            
  761.         }
  762.     }
  763.  
  764.     for(int nbrTest = 1; nbrTest <= 9; nbrTest++) //THIS IS FOR THE ROWS
  765.     {
  766.         for(int j = 0; j < listNbr.size(); j++)
  767.         {
  768.             std::vector<bool> isHere {false, false, false};
  769.  
  770.             int iFind;
  771.            
  772.             for(int i = 0; i < listNbr[j].size(); i++)
  773.             {
  774.                 if(isInVect(nbrTest, listNbr[i][j].getVec())
  775.                         && !listNbr[i][j].isValid())
  776.                 {
  777.                     isHere[i / 3] = true;
  778.  
  779.                     iFind = i;
  780.                     //std::cout << i << " " << j << std::endl;
  781.                 }
  782.             }
  783.            
  784.             if(std::count(isHere.begin(), isHere.end(), true) == 1)
  785.             {
  786.                 std::pair<bool, int> result = findInVector<bool>(isHere, true);
  787.            
  788.                 //then find and put off the useless little numbers
  789.            
  790.                 int caseFind = (iFind / 3) * 3 + (j / 3);
  791.                
  792.                 /*
  793.                 std::cout << "finded ! ";
  794.                
  795.                 for(int i = 0; i < isHere.size(); i++)
  796.                 {
  797.                     std::cout << std::boolalpha << isHere[i] << " ";
  798.                 }
  799.                 std::cout << nbrTest << " " << caseFind << " " << iFind << " " << j;
  800.                 std::cout << std::endl;
  801.  
  802.                 coutLittleNbrInGrid(std::cout); */
  803.                
  804.                 for(int x = caseFind / 3 * 3; x < caseFind / 3 * 3 + 3; x++)
  805.                 {
  806.                     for(int y = caseFind % 3 * 3; y < caseFind % 3 * 3 + 3; y++)
  807.                     {
  808.                         if(y != j && isInVect(nbrTest, listNbr[x][y].getVec()))
  809.                         {
  810.                             listNbr[x][y].putOffLitteNbr(nbrTest);
  811.                             haveAChange = true;
  812.                         }
  813.                     }
  814.                 }
  815.                 //yes it's ugly but easier
  816.             }
  817.         }
  818.     }
  819.    
  820.     return haveAChange;
  821. }
  822.  
  823. std::ostream& operator<<(std::ostream& output, Sudoku s)
  824. {
  825.     output << s.toString();
  826.     return output;
  827. }
  828.  
  829. bool Sudoku::putOffLittleNbrIfOnlyInCase()
  830. {
  831.     bool haveAChange = false;
  832.    
  833.     for(int nbrTest = 1; nbrTest <= 9; nbrTest++)
  834.     {
  835.         for(int nbrCase = 0; nbrCase < 9; nbrCase++)
  836.         {
  837.             std::vector<bool> isHere {false, false, false};
  838.  
  839.             int iFind;
  840.             int jFind;
  841.  
  842.             for(int x = nbrCase / 3 * 3; x < nbrCase / 3 * 3 + 3; x++)
  843.             {
  844.                 for(int y = nbrCase % 3 * 3; y < nbrCase % 3 * 3 + 3; y++)
  845.                 {
  846.                     //std::cout << x << " " << y << " " << nbrCase << std::endl;
  847.                    
  848.                     if(isInVect(nbrTest, listNbr[x][y].getVec())
  849.                             && !listNbr[x][y].isValid())
  850.                     {
  851.                         isHere[y % 3] = true;
  852.                        
  853.                         iFind = x;
  854.                         jFind = y;
  855.                     }
  856.                 }
  857.             }
  858.  
  859.             if(std::count(isHere.begin(), isHere.end(), true) == 1)
  860.             { //Then delete the bad things
  861.                
  862.                 std::cout << "finded ! ";
  863.                
  864.                 for(int i = 0; i < isHere.size(); i++)
  865.                 {
  866.                     std::cout << std::boolalpha << isHere[i] << " ";
  867.                 }
  868.                 std::cout << nbrTest << " " << nbrCase << " " << iFind
  869.                             << " " << jFind << std::endl;
  870.  
  871.                 coutLittleNbrInGrid(std::cout);
  872.                
  873.                 for(int i = 0; i < listNbr.size(); i++)
  874.                 {
  875.                     int caseFind = (i / 3) * 3 + (jFind / 3);
  876.                    
  877.                     //std::cout << caseFind << " " << i << std::endl;
  878.                     //std::cout << listNbr[i][jFind] << std::endl;
  879.                    
  880.                     if(caseFind != nbrCase && listNbr[i][jFind].isValid()
  881.                         && isInVect(nbrTest, listNbr[i][jFind].getVec()))
  882.                     {
  883.                         listNbr[i][jFind].putOffLitteNbr(nbrTest);
  884.                     }
  885.                 }
  886.                 //std::cout << "it's nit here" << std::endl;
  887.             }
  888.         }
  889.     }
  890.    
  891.     //std::cout << "i'm (may be) a bit safe" << std::endl;
  892.    
  893.     return haveAChange;
  894. }
  895.  
  896. int main()
  897. {
  898.     try
  899.     {
  900.         Sudoku a("4...3.......6..8..........1....5..9..8....6...7.2........1.27..5.3....4.9........");
  901.         std::cout << a << std::endl;
  902.         std::cout << a.isValid() << std::endl << std::endl;
  903.        
  904.         //a.coutLittleNbrInGrid(std::cout);
  905.        
  906.         a.addLittleNbrEveywhere();
  907.        
  908.         std::cout << std::endl << std::endl;
  909.        
  910.         //a.coutLittleNbr(std::cout);
  911.         //std::cout << std::endl;
  912.         // ui c la flemme de surcharger l'operateur << une deusième fois
  913.        
  914.         a.coutLittleNbrInGrid(std::cout);
  915.  
  916.         std::cout << a << std::endl;
  917.        
  918.         a.solve();
  919.        
  920.         std::cout << a << "THE PROCESS HAS ENDED" << std::endl;
  921.        
  922.         a.coutLittleNbrInGrid(std::cout);
  923.     }
  924.     catch (const std::exception& e)
  925.     {
  926.         std::cout << "error : " << e.what() << std::endl;
  927.     }
  928.  
  929.     return 0;
  930. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement