Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <iomanip>
- #include <stdio.h>
- #include <ctime>
- using namespace std;
- int grid[9][9]={{0,0,0 ,0,0,0 ,0,0,0}, //sudoku ploča
- {0,0,0 ,0,0,0 ,0,0,0},
- {0,0,0 ,0,0,0 ,0,0,0},
- {0,0,0 ,0,0,0 ,0,0,0},
- {0,0,0 ,0,0,0 ,0,0,0},
- {0,0,0 ,0,0,0 ,0,0,0},
- {0,0,0 ,0,0,0 ,0,0,0},
- {0,0,0 ,0,0,0 ,0,0,0},
- {0,0,0 ,0,0,0 ,0,0,0}};
- int nd[81][9]; //nedostupni brojevi
- int isprintan1=0,isprintan2=0;
- int grids[2][9][9];
- bool rek(int ss){
- if(isprintan1==0){
- if(ss==81){ //ovo je bitno,zbog ispisivanja prvog riješenja
- isprintan1=1;
- for(int x=0;x<9;x++){
- for(int y=0;y<9;y++){
- grids[0][x][y]=grid[x][y];
- }
- }
- return true;
- }
- for(int x=0;x<9;x++){
- if(grid[x][ss%9]!=0){
- nd[ss][grid[x][ss%9]-1]=grid[x][ss%9]; //nedostupni brojevi u redu
- }
- if(grid[ss/9][x]!=0){
- nd[ss][grid[ss/9][x]-1]=grid[ss/9][x]; //nedostupni brojevi u stupcu
- }
- } //određivalje bloka u kojem se nalazi pokazivač
- for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
- for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
- if(grid[x][y]!=0){
- nd[ss][grid[x][y]-1]=grid[x][y];
- }
- }
- }
- for(int x=1;x<10;x++){
- if(nd[ss][x-1]!=x){
- if(grid[ss/9][ss%9]==0){
- grid[ss/9][ss%9]=x; //ako je polje prazno
- rek(ss+1);
- grid[ss/9][ss%9]=0;
- }else{
- rek(ss+1);
- }
- }else{
- if(grid[ss/9][ss%9]!=0){
- rek(ss+1); //ako je polje postavljeno
- break;
- }
- }
- }
- for(int x=0;x<9;x++){
- nd[ss][x]=0;
- }
- return 0;
- }
- }
- bool rekm(int ss){
- if(isprintan2==0){
- if(ss==81){ //ovo je bitno,zbog ispisivanja prvog riješenja
- isprintan2=1;
- for(int x=0;x<9;x++){
- for(int y=0;y<9;y++){
- grids[1][x][y]=grid[x][y];
- }
- }
- return true;
- }
- for(int x=0;x<9;x++){
- if(grid[x][ss%9]!=0){
- nd[ss][grid[x][ss%9]-1]=grid[x][ss%9]; //nedostupni brojevi u redu
- }
- if(grid[ss/9][x]!=0){
- nd[ss][grid[ss/9][x]-1]=grid[ss/9][x]; //nedostupni brojevi u stupcu
- }
- } //određivalje bloka u kojem se nalazi pokazivač
- for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
- for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
- if(grid[x][y]!=0){
- nd[ss][grid[x][y]-1]=grid[x][y];
- }
- }
- }
- for(int x=9;x>0;x--){
- if(nd[ss][x-1]!=x){
- if(grid[ss/9][ss%9]==0){
- grid[ss/9][ss%9]=x; //ako je polje prazno
- rekm(ss+1);
- grid[ss/9][ss%9]=0;
- }else{
- rekm(ss+1);
- }
- }else{
- if(grid[ss/9][ss%9]!=0){
- rekm(ss+1); //ako je polje postavljeno
- break;
- }
- }
- }
- for(int x=0;x<9;x++){
- nd[ss][x]=0;
- }
- return 0;
- }
- }
- int rj=0;
- bool rijes(int ss){
- if(ss==81){
- return false;
- }
- for(int x=0;x<9;x++){
- nd[ss][x]=0;
- }
- for(int x=0;x<9;x++){
- if(grid[x][ss%9]!=0){
- if(nd[ss][grid[x][ss%9]-1]==grid[x][ss%9]){
- rj+=1;
- }else{
- nd[ss][grid[x][ss%9]-1]=grid[x][ss%9];
- }
- }
- }
- for(int x=0;x<9;x++){
- nd[ss][x]=0;
- }
- for(int x=0;x<9;x++){
- if(grid[ss/9][x]!=0){
- if(nd[ss][grid[ss/9][x]-1]==grid[ss/9][x]){
- rj+=1;
- }else{
- nd[ss][grid[ss/9][x]-1]=grid[ss/9][x];
- }
- }
- }
- for(int x=0;x<9;x++){
- nd[ss][x]=0;
- }
- for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
- for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
- if(grid[x][y]!=0){
- if(nd[ss][grid[x][y]-1]==grid[x][y]){
- rj+=1;
- }else{
- nd[ss][grid[x][y]-1]=grid[x][y];
- }
- }
- }
- }
- for(int x=0;x<9;x++){
- nd[ss][x]=0;
- }
- rijes(ss+1);
- return 0;
- }
- bool sigurni(){
- for(int ss=0;ss<81;ss++){
- for(int x=0;x<9;x++){
- if(grid[x][ss%9]!=0){
- nd[ss][grid[x][ss%9]-1]=grid[x][ss%9];
- }
- }
- for(int x=0;x<9;x++){
- if(grid[ss/9][x]!=0){
- nd[ss][grid[ss/9][x]-1]=grid[ss/9][x];
- }
- }
- for(int x=((ss/9)/3)*3;x<=((ss/9)/3)*3+2;x++){
- for(int y=((ss%9)/3)*3;y<=((ss%9)/3)*3+2;y++){
- if(grid[x][y]!=0){
- nd[ss][grid[x][y]-1]=grid[x][y];
- }
- }
- }
- int b=0,c;
- if(grid[ss/9][ss%9]==0){
- for(int x=0;x<9;x++){
- if(nd[ss][x]==0){
- b+=1;
- c=x;
- }
- nd[ss][x]=0;
- }
- if(b==1){
- grid[ss/9][ss%9]=c+1;
- ss=-1;
- }
- }
- }
- return 0;
- }
- int main(){
- char a;
- for(int x=0;x<9;x++){
- for(int y=0;y<9;y++){
- scanf("%c",&a);
- if(a=='.'){
- grid[x][y]=0;
- }else{
- if(a>='0' && a<='0'+10){
- grid[x][y]=a-'0';
- }else{
- y-=1;
- }
- }
- }
- }
- // double start=clock();
- sigurni();
- rijes(0);
- if(rj==0){
- rek(0);
- rekm(0);
- int jednakost=81;
- for(int x=0;x<9;x++){
- for(int y=0;y<9;y++){
- if(grids[0][x][y]==grids[1][x][y]){
- jednakost-=1;
- }
- }
- }
- if(isprintan1!=0){
- if(jednakost==0){
- for(int x=0;x<9;x++){
- for(int y=0;y<9;y++){
- printf("%d",grids[0][x][y]);
- }
- printf("\n");
- }
- }else{
- for(int z=0;z<2;z++){
- for(int x=0;x<9;x++){
- for(int y=0;y<9;y++){
- printf("%d",grids[z][x][y]);
- }
- printf("\n");
- }
- printf("\n");
- }
- }
- }
- }else{
- if(isprintan1==0){
- for(int x=0;x<9;x++){
- for(int y=0;y<9;y++){
- printf("%d",grid[x][y]);
- }
- printf("\n");
- }
- printf("IMPOSSIBLE\n");
- }
- }
- // start=(clock()-start)/1000;
- // printf("%fsecunds\n",start);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement