Advertisement
YauhenMardan

Untitled

May 30th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <map>
  5.  
  6. const std::string letters="ABCDEFGHIJKLMNOP";
  7.  
  8. class Sudoku{
  9. private:
  10. const int ROWS_NUM=16;
  11. const int COLS_NUM=16;
  12. const int NUM=16;
  13.  
  14. std::map<std::string,std::string> values;
  15. std::map<std::string,std::vector<std::string>> units;
  16.  
  17. public:
  18. Sudoku(){
  19. }
  20. void solve(){
  21. initUnits(); //init units
  22.  
  23. }
  24. void initUnits(){
  25. //init units
  26. int x,y;
  27. std::string str;
  28. std::string strk;
  29. std::vector<std::string> vect;
  30. //for each cell
  31. for(int i=0;i<ROWS_NUM;i++){
  32. for(int j=0;j<COLS_NUM;j++){
  33. str="";
  34. str+=letters[i];
  35. str+=letters[j];
  36. vect.clear();
  37. //rows
  38. for(int k=0;k<ROWS_NUM;k++){
  39. if(k!=i){
  40. strk="";
  41. strk+=letters[k];
  42. strk+=letters[j];
  43. vect.push_back(strk);
  44. }
  45. }
  46. //columns
  47. for(int k=0;k<COLS_NUM;k++){
  48. if(k!=j){
  49. strk="";
  50. strk+=letters[i];
  51. strk+=letters[k];
  52. vect.push_back(strk);
  53. }
  54. }
  55. y=i/4;
  56. x=j/4;
  57. //square
  58. for(int ii=0; ii<NUM/4; ii++){
  59. for(int jj=0; jj<NUM/4; jj++){
  60. if(ii!=i && jj!=j){
  61. strk="";
  62. strk+=letters[y+ii];
  63. strk+=letters[x+jj];
  64. vect.push_back(strk);
  65. }
  66. }
  67. }
  68. units.insert(std::pair<std::string, std::vector<std::string>>(str, vect));
  69. }
  70. }
  71. // TEST UNITS
  72. // std::string st="CC";
  73. // for(int i=0;i<units[st].size();i++){
  74. // std::cout<<units[st][i]<<" ";
  75. // }
  76. // std::cout<<std::endl<<units[st].size()<<std::endl;
  77. }
  78.  
  79. bool assign(int k, int val) {
  80. for (int i = 1; i <= 9; i++) {
  81. if (i != val) {
  82. if (!eliminate(k, i))
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88.  
  89. void read(std::string fileName){
  90. std::ifstream fin(fileName);
  91. char c;
  92. std::string str;
  93. std::string str1;
  94. for(int i=0;i<ROWS_NUM;i++){
  95. for(int j=0;j<COLS_NUM;j++){
  96. fin>>c;
  97. str=letters[i]+letters[j];
  98. if(c=='-'){
  99. values.insert(std::pair<std::string, std::string>(str,letters));
  100. } else{
  101. values.insert(std::pair<std::string, std::string>(str,std::string(1,c)));
  102. }
  103. }
  104. }
  105. fin.close();
  106. }
  107. void write(std::string fileName){
  108. std::ofstream fout(fileName);
  109. std::string str;
  110. for(int i=0;i<ROWS_NUM;i++){
  111. for(int j=0;j<COLS_NUM;j++){
  112. str=letters[i]+letters[j];
  113. fout<<values[str].size()<<" ";
  114. }
  115. fout<<"\n";
  116. }
  117. fout.close();
  118. }
  119. };
  120.  
  121. int main(int argc, const char * argv[]) {
  122. Sudoku sudoku;
  123. sudoku.read("input.txt");
  124. sudoku.solve();
  125. sudoku.write("output.txt");
  126.  
  127. // std::cout<<"GGG\n";
  128. // char c1;
  129. // while(true){
  130. // std::cin>>c1;
  131. // std::cout<<sudoku.ctoi(c1)<<std::endl;
  132. // std::cout<<sudoku.itoc(sudoku.ctoi(c1))<<std::endl;
  133. // }
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement