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";
- enum Type{GIVEN,UNDEF};
- class Options{
- private:
- std::string options;
- Type type;
- public:
- Options(){
- }
- void set(){
- type=UNDEF;
- options=letters;
- }
- void set(char ch){
- type=GIVEN;
- options=ch;
- }
- int getLen(){
- return (int)options.length();
- }
- bool isGiven(){
- return type==GIVEN;
- }
- };
- class Sudoku{
- private:
- const int ROWS_NUM=16;
- const int COLS_NUM=16;
- const int NUM=16;
- std::vector<std::vector<Options>> grid;
- std::map<std::string,std::vector<std::string>> units;
- public:
- Sudoku(){
- grid.resize(ROWS_NUM);
- for(int i=0;i<ROWS_NUM;i++){
- grid[i].resize(COLS_NUM);
- }
- }
- 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;
- }
- void read(std::string fileName){
- std::ifstream fin(fileName);
- char c;
- for(int i=0;i<ROWS_NUM;i++){
- for(int j=0;j<COLS_NUM;j++){
- fin>>c;
- if(c=='-'){
- grid[i][j].set();
- } else{
- grid[i][j].set(c);
- }
- }
- }
- fin.close();
- }
- void write(std::string fileName){
- std::ofstream fout(fileName);
- for(int i=0;i<ROWS_NUM;i++){
- for(int j=0;j<COLS_NUM;j++){
- fout<<grid[i][j].getLen();
- }
- 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