Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <vector>
- #include <map>
- const std::string letters="ABCDEFGHIJKLMNOP";
- class Sudoku{
- private:
- const int ROWS_NUM=16;
- const int COLS_NUM=16;
- const int NUM=16;
- std::map<std::string,std::string> values;
- std::map<std::string,std::vector<std::string>> units;
- public:
- Sudoku(){
- }
- void solve(){
- initUnits(); //init units
- }
- void initUnits(){
- //init units
- int x,y;
- std::string str;
- std::string strk;
- std::vector<std::string> vect;
- //for each cell
- for(int i=0;i<ROWS_NUM;i++){
- for(int j=0;j<COLS_NUM;j++){
- str="";
- str+=letters[i];
- str+=letters[j];
- vect.clear();
- //rows
- for(int k=0;k<ROWS_NUM;k++){
- if(k!=i){
- strk="";
- strk+=letters[k];
- strk+=letters[j];
- vect.push_back(strk);
- }
- }
- //columns
- for(int k=0;k<COLS_NUM;k++){
- if(k!=j){
- strk="";
- strk+=letters[i];
- strk+=letters[k];
- vect.push_back(strk);
- }
- }
- y=i/4;
- x=j/4;
- //square
- for(int ii=0; ii<NUM/4; ii++){
- for(int jj=0; jj<NUM/4; jj++){
- if(ii!=i && jj!=j){
- strk="";
- strk+=letters[y+ii];
- strk+=letters[x+jj];
- vect.push_back(strk);
- }
- }
- }
- units.insert(std::pair<std::string, std::vector<std::string>>(str, vect));
- }
- }
- // TEST UNITS
- // std::string st="CC";
- // for(int i=0;i<units[st].size();i++){
- // std::cout<<units[st][i]<<" ";
- // }
- // std::cout<<std::endl<<units[st].size()<<std::endl;
- }
- bool assign(int k, int val) {
- for (int i = 1; i <= 9; i++) {
- if (i != val) {
- if (!eliminate(k, i))
- return false;
- }
- }
- return true;
- }
- void read(std::string fileName){
- std::ifstream fin(fileName);
- char c;
- std::string str;
- std::string str1;
- for(int i=0;i<ROWS_NUM;i++){
- for(int j=0;j<COLS_NUM;j++){
- fin>>c;
- str=letters[i]+letters[j];
- if(c=='-'){
- values.insert(std::pair<std::string, std::string>(str,letters));
- } else{
- values.insert(std::pair<std::string, std::string>(str,std::string(1,c)));
- }
- }
- }
- fin.close();
- }
- void write(std::string fileName){
- std::ofstream fout(fileName);
- std::string str;
- for(int i=0;i<ROWS_NUM;i++){
- for(int j=0;j<COLS_NUM;j++){
- str=letters[i]+letters[j];
- fout<<values[str].size()<<" ";
- }
- fout<<"\n";
- }
- fout.close();
- }
- };
- int main(int argc, const char * argv[]) {
- Sudoku sudoku;
- sudoku.read("input.txt");
- sudoku.solve();
- sudoku.write("output.txt");
- // std::cout<<"GGG\n";
- // char c1;
- // while(true){
- // std::cin>>c1;
- // std::cout<<sudoku.ctoi(c1)<<std::endl;
- // std::cout<<sudoku.itoc(sudoku.ctoi(c1))<<std::endl;
- // }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement