HELP WITH MY BACKTRACKING OF SUDOKU

I need to use this template.. but he's stop on pos [9][1] because idk why he's put my bool ENC=true (only 'true' when n==81 and i get solution) in ambient where N=72 later of the line "guardar_paso(x,y,i)"(save_step(x,y,i))

i need help ...

see my code please

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>

using namespace std;

int tab[9][9];
bool enc=false;
int n=1;
void initab(){
	int f,c;
	for (f=1; f<=9; f++){
		for(c=1; c<=9; c++) {
			tab[f][c]=0;
		}
	}
}

void imprimir () {
	int f,c;
	cout<<"-------------"<<endl;
	for (f=1; f<=9; f++){
		cout<<"|";
		for(c=1; c<=9; c++) {
			cout<<tab[f][c];
			if(c==3||c==6||c==9)
				cout<<"|";
		}
		cout<<endl;
		if(f==3||f==6||f==9)
		cout<<"-------------"<<endl;
	}
}

bool ir_posicion_libre(int &a, int &b){
	for (a = 1; a <= 9; a++)
        for (b = 1; b <= 9; b++)
            if (tab[a][b] == 0)
                return true;
    return false;
}

void obtener_sig_alt(int &i) {
	i++;
}

bool val_col(int x, int i){
	for (int y = 1; y <= 9; y++)
    	if (tab[x][y] == i)
            return false;
    return true;
}

bool val_fila(int y, int i){
	for (int x = 1; x <= 9; x++)
    	if (tab[x][y] == i)
            return false;
    return true;
}

void tener_inicio_reg(int z, int &ini){
	if (z<=3){
		ini=1;
	}else if (z<=6 && z>3){
		ini=4;
	}else{
		ini=7;
	}
}


bool val_sec(int a, int b, int i){
	int inix, iniy;
	tener_inicio_reg(a,inix);
	tener_inicio_reg(b,iniy);
    for (int x = 0; x < 3; x++)
        for (int y = 0; y < 3; y++)
            if (tab[x+inix][y+iniy] == i){
                return false;
            }
    return true;
}

bool alt_valida(int x,int y, int i){
	if((val_fila(y,i)==true)&&(val_col(x,i)==true)&&(val_sec(x,y,i)==true)){
		return true;
	}else{
		return false;
	}
}

void guardar_paso(int x,int y,int i){
	tab[x][y]=i;
}

void borrar_paso(int x, int y){
	tab[x][y]=0;
}

void bt_unasol(int n){
	int i=0;
	int x,y;
	ir_posicion_libre(x,y);
	do{
		obtener_sig_alt(i);
		if (alt_valida(x,y,i)){
			guardar_paso(x,y,i);
			if(n==81){
				imprimir();
				enc=true;
			}else{
				bt_unasol(n+1);

			}
			if(enc==false)
			borrar_paso(x,y);
		}		

	}while(i<9&&enc!=true); 
}

int main(){
	initab();
	bt_unasol(n);
	return 0;
}


if u can help me ty
Topic archived. No new replies allowed.