Advertisement
Guest User

ça fait un peu totalement de la merde maintenant c chiant

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