Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <iomanip>
  6. #include <stdio.h>
  7. #include <ctime>
  8. using namespace std;
  9.  
  10.  
  11. int grid[9][9]={{0,0,0 ,0,0,0 ,0,0,0},          //sudoku ploča
  12.                 {0,0,0 ,0,0,0 ,0,0,0},
  13.                 {0,0,0 ,0,0,0 ,0,0,0},
  14.  
  15.                 {0,0,0 ,0,0,0 ,0,0,0},
  16.                 {0,0,0 ,0,0,0 ,0,0,0},
  17.                 {0,0,0 ,0,0,0 ,0,0,0},
  18.  
  19.                 {0,0,0 ,0,0,0 ,0,0,0},
  20.                 {0,0,0 ,0,0,0 ,0,0,0},
  21.                 {0,0,0 ,0,0,0 ,0,0,0}};
  22. int nd[81][9];     //nedostupni brojevi
  23. int isprintan1=0,isprintan2=0;
  24. int grids[2][9][9];
  25. bool rek(int ss){
  26.     if(isprintan1==0){
  27.         if(ss==81){                 //ovo je bitno,zbog ispisivanja prvog riješenja
  28.             isprintan1=1;
  29.             for(int x=0;x<9;x++){
  30.                 for(int y=0;y<9;y++){
  31.                     grids[0][x][y]=grid[x][y];
  32.                 }
  33.             }
  34.             return true;
  35.         }
  36.         for(int x=0;x<9;x++){
  37.             if(grid[x][ss%9]!=0){
  38.                 nd[ss][grid[x][ss%9]-1]=grid[x][ss%9];      //nedostupni brojevi u redu
  39.             }
  40.             if(grid[ss/9][x]!=0){
  41.                 nd[ss][grid[ss/9][x]-1]=grid[ss/9][x];          //nedostupni brojevi u stupcu
  42.             }
  43.         }                                                       //određivalje bloka u kojem se nalazi pokazivač
  44.         for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
  45.             for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
  46.                 if(grid[x][y]!=0){
  47.                     nd[ss][grid[x][y]-1]=grid[x][y];
  48.                 }
  49.             }
  50.         }
  51.         for(int x=1;x<10;x++){
  52.             if(nd[ss][x-1]!=x){
  53.                 if(grid[ss/9][ss%9]==0){
  54.                     grid[ss/9][ss%9]=x;                         //ako je polje prazno
  55.                     rek(ss+1);
  56.                     grid[ss/9][ss%9]=0;
  57.                 }else{
  58.                     rek(ss+1);
  59.                 }  
  60.             }else{
  61.                 if(grid[ss/9][ss%9]!=0){
  62.                     rek(ss+1);                                  //ako je polje postavljeno
  63.                     break;
  64.                 }
  65.             }
  66.         }
  67.         for(int x=0;x<9;x++){
  68.             nd[ss][x]=0;
  69.         }
  70.         return 0;
  71.     }
  72. }
  73. bool rekm(int ss){
  74.     if(isprintan2==0){
  75.         if(ss==81){                 //ovo je bitno,zbog ispisivanja prvog riješenja
  76.             isprintan2=1;
  77.             for(int x=0;x<9;x++){
  78.                 for(int y=0;y<9;y++){
  79.                     grids[1][x][y]=grid[x][y];
  80.                 }
  81.             }
  82.             return true;
  83.         }
  84.         for(int x=0;x<9;x++){
  85.             if(grid[x][ss%9]!=0){
  86.                 nd[ss][grid[x][ss%9]-1]=grid[x][ss%9];      //nedostupni brojevi u redu
  87.             }
  88.             if(grid[ss/9][x]!=0){
  89.                 nd[ss][grid[ss/9][x]-1]=grid[ss/9][x];          //nedostupni brojevi u stupcu
  90.             }
  91.         }                                                       //određivalje bloka u kojem se nalazi pokazivač
  92.         for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
  93.             for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
  94.                 if(grid[x][y]!=0){
  95.                     nd[ss][grid[x][y]-1]=grid[x][y];
  96.                 }
  97.             }
  98.         }
  99.         for(int x=9;x>0;x--){
  100.             if(nd[ss][x-1]!=x){
  101.                 if(grid[ss/9][ss%9]==0){
  102.                     grid[ss/9][ss%9]=x;                         //ako je polje prazno
  103.                     rekm(ss+1);
  104.                     grid[ss/9][ss%9]=0;
  105.                 }else{
  106.                     rekm(ss+1);
  107.                 }  
  108.             }else{
  109.                 if(grid[ss/9][ss%9]!=0){
  110.                     rekm(ss+1);                                 //ako je polje postavljeno
  111.                     break;
  112.                 }
  113.             }
  114.         }
  115.         for(int x=0;x<9;x++){
  116.             nd[ss][x]=0;
  117.         }
  118.         return 0;
  119.     }
  120. }
  121. int rj=0;
  122. bool rijes(int ss){
  123.     if(ss==81){
  124.         return false;
  125.     }
  126.     for(int x=0;x<9;x++){
  127.         nd[ss][x]=0;
  128.     }
  129.     for(int x=0;x<9;x++){
  130.         if(grid[x][ss%9]!=0){
  131.             if(nd[ss][grid[x][ss%9]-1]==grid[x][ss%9]){
  132.                 rj+=1;
  133.             }else{
  134.                 nd[ss][grid[x][ss%9]-1]=grid[x][ss%9]; 
  135.             }
  136.         }
  137.     }
  138.     for(int x=0;x<9;x++){
  139.         nd[ss][x]=0;
  140.     }
  141.     for(int x=0;x<9;x++){
  142.         if(grid[ss/9][x]!=0){
  143.             if(nd[ss][grid[ss/9][x]-1]==grid[ss/9][x]){
  144.                 rj+=1;
  145.             }else{
  146.                 nd[ss][grid[ss/9][x]-1]=grid[ss/9][x];
  147.             }
  148.         }
  149.     }
  150.     for(int x=0;x<9;x++){
  151.         nd[ss][x]=0;
  152.     }
  153.     for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
  154.         for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
  155.             if(grid[x][y]!=0){
  156.                 if(nd[ss][grid[x][y]-1]==grid[x][y]){
  157.                     rj+=1;
  158.                 }else{
  159.                     nd[ss][grid[x][y]-1]=grid[x][y];
  160.                 }
  161.             }
  162.         }
  163.     }
  164.     for(int x=0;x<9;x++){
  165.         nd[ss][x]=0;
  166.     }
  167.     rijes(ss+1);
  168.     return 0;
  169. }
  170. bool sigurni(){
  171.     for(int ss=0;ss<81;ss++){
  172.         for(int x=0;x<9;x++){
  173.             if(grid[x][ss%9]!=0){
  174.                 nd[ss][grid[x][ss%9]-1]=grid[x][ss%9]; 
  175.             }
  176.         }
  177.         for(int x=0;x<9;x++){
  178.             if(grid[ss/9][x]!=0){
  179.                 nd[ss][grid[ss/9][x]-1]=grid[ss/9][x];
  180.             }
  181.         }
  182.         for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
  183.             for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
  184.                 if(grid[x][y]!=0){
  185.                     nd[ss][grid[x][y]-1]=grid[x][y];
  186.                 }
  187.             }
  188.         }
  189.         int b=0,c;
  190.         if(grid[ss/9][ss%9]==0){
  191.             for(int x=0;x<9;x++){
  192.                 if(nd[ss][x]==0){
  193.                     b+=1;
  194.                     c=x;
  195.                 }
  196.                 nd[ss][x]=0;
  197.             }
  198.             if(b==1){
  199.                 grid[ss/9][ss%9]=c+1;
  200.                 ss=-1;
  201.             }
  202.         }
  203.     }
  204.     return 0;
  205. }
  206. int main(){
  207.     char a;
  208.     for(int x=0;x<9;x++){
  209.         for(int y=0;y<9;y++){
  210.             scanf("%c",&a);
  211.             if(a=='.'){
  212.                 grid[x][y]=0;
  213.             }else{
  214.                 if(a>='0' && a<='0'+10){
  215.                     grid[x][y]=a-'0';
  216.                 }else{
  217.                     y-=1;
  218.                 }
  219.             }      
  220.         }
  221.     }
  222. //  double start=clock();
  223.     sigurni();
  224.     rijes(0);
  225.     if(rj==0){
  226.         rek(0);
  227.         rekm(0);
  228.         int jednakost=81;
  229.         for(int x=0;x<9;x++){
  230.                for(int y=0;y<9;y++){
  231.                     if(grids[0][x][y]==grids[1][x][y]){
  232.                         jednakost-=1;
  233.                     }
  234.                 }
  235.         }
  236.         if(isprintan1!=0){
  237.             if(jednakost==0){
  238.                 for(int x=0;x<9;x++){
  239.                     for(int y=0;y<9;y++){
  240.                         printf("%d",grids[0][x][y]);
  241.                     }
  242.                     printf("\n");
  243.                 }
  244.             }else{
  245.                 for(int z=0;z<2;z++){
  246.                     for(int x=0;x<9;x++){
  247.                         for(int y=0;y<9;y++){
  248.                             printf("%d",grids[z][x][y]);
  249.                         }
  250.                         printf("\n");
  251.                     }
  252.                         printf("\n");
  253.                 }
  254.             }
  255.         }
  256.     }else{
  257.  
  258.         if(isprintan1==0){
  259.             for(int x=0;x<9;x++){
  260.                 for(int y=0;y<9;y++){
  261.                     printf("%d",grid[x][y]);
  262.                 }
  263.                 printf("\n");
  264.             }
  265.             printf("IMPOSSIBLE\n");
  266.         }  
  267.     }
  268. //  start=(clock()-start)/1000;
  269. //  printf("%fsecunds\n",start);
  270.     system("pause");
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement