Advertisement
YauhenMardan

Untitled

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