Академический Документы
Профессиональный Документы
Культура Документы
#define DIM 9
#define BLANK 0
#define SPACE " "
#define LINE "|"
#define NEW_ROW "-------------------------------------"
#define GRID_FULL std::make_pair(9, 9)
// Consider digits 1 to 9
for (int num = 1; num <= 9; num++)
{
// If placing the current number in the current
// unassigned location is valid, go ahead
if (is_safe(grid, row, col, num))
{
// Make tentative assignment
grid[row][col] = num;
// If we have gone through all possible numbers for the current unassigned
// location, then we probably assigned a bad number early. Lets backtrack
// and try a different number for the previous unassigned locations.
return false;
}
int main()
{
cout << "********************************\n\n\tSudoku
Solver\n\n********************************" << endl << endl;
int grid[DIM][DIM] = { { 0, 9, 0, 0, 0, 0, 8, 5, 3 },
{ 0, 0, 0, 8, 0, 0, 0, 0, 4 },
{ 0, 0, 8, 2, 0, 3, 0, 6, 9 },
{ 5, 7, 4, 0, 0, 2, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 0, 6, 3, 7 },
{ 9, 4, 0, 1, 0, 8, 5, 0, 0 },
{ 7, 0, 0, 0, 0, 6, 0, 0, 0 },
{ 6, 8, 2, 0, 0, 0, 0, 9, 0 } };
print_grid(grid);
if (true == solve_soduko(grid))
{
print_grid(grid);
}
else
{
cout << "No solution exists for the given Soduko" << endl << endl;
}
return 0;
}
https://medium.com/@george.seif94/solving-sudoku-using-a-simple-search-algorithm-3ac44857fee8
https://github.com/GeorgeSeif/Sudoku-Solver
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using std::cout;
using std::endl;
#define DIM 9
#define BLANK 0
#define SPACE " "
#define LINE "|"
#define NEW_ROW "-------------------------------------"
#define GRID_FULL std::make_pair(9, 9)
// Consider digits 1 to 9
for (int num = 1; num <= 9; num++)
{
// If placing the current number in the current
// unassigned location is valid, go ahead
if (is_safe(grid, row, col, num))
{
// Make tentative assignment
grid[row][col] = num;
// If we have gone through all possible numbers for the current unassigned
// location, then we probably assigned a bad number early. Lets backtrack
// and try a different number for the previous unassigned locations.
return false;
}
int main()
{
cout << "********************************\n\n\tSudoku
Solver\n\n********************************" << endl << endl;
int grid[DIM][DIM] = { { 0, 9, 0, 0, 0, 0, 8, 5, 3 },
{ 0, 0, 0, 8, 0, 0, 0, 0, 4 },
{ 0, 0, 8, 2, 0, 3, 0, 6, 9 },
{ 5, 7, 4, 0, 0, 2, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 0, 6, 3, 7 },
{ 9, 4, 0, 1, 0, 8, 5, 0, 0 },
{ 7, 0, 0, 0, 0, 6, 0, 0, 0 },
{ 6, 8, 2, 0, 0, 0, 0, 9, 0 } };
print_grid(grid);
if (true == solve_soduko(grid))
{
print_grid(grid);
}
else
{
cout << "No solution exists for the given Soduko" << endl << endl;
}
return 0;
}
C how to_program_6th
http://docerp.ro/exercitii-de-programare-in-c-c/
http://docerp.ro/all_titles/
#include<iostream>
//permutari
#include<fstream>
#include<bitset>
using namespace std;
int st[9],n;
bitset <9> d;
ofstream g("permutari.out");
void afiseaza(int n)
{
int i;
for(i=1;i<=n;i++)
g<<st[i]<<" ";
g<<'\n';
}
void bktr(int p)
{
int pval;
for(pval=1;pval<=n;pval++)
if(d[pval]==0) {
d[pval]=1;
st[p]=pval;
if(p==n)
afiseaza(p);
else bktr(p+1);
d[pval]=0;
}
}
int main ()
{
ifstream f("permutari.in");
f>>n;
f.close();
bktr(1);
g.close();
return 0;
}
subm
#include<iostream>
#include<fstream>
using namespace std;
#define NMAX 17
ofstream g("submultimi.out");
int st[NMAX],n;
inline void afisare(int n)
{
int i;
for(i=1;i<=n;i++)
g<<st[i]<<" ";
g<<'\n';
}
inline void bktr(int p)
{
int pval;
for(pval=st[p-1]+1;pval<=n;pval++) {
st[p]=pval;
afisare(p);
if(p<n)
bktr(p+1);
}
}
int main ()
{
ifstream f("submultimi.in");
f>>n;
f.close();
bktr(1);
g.close();
return 0;
}
Combin
#include<iostream
>
#include<fstream>
using namespace std;
#define NMAX 19
ofstream g("combinari.out");
int st[NMAX],n,k;
inline void afisare()
{
int i;
for(i=1;i<=k;i++)
g<<st[i]<<" ";
g<<'\n';
}
inline void bktr(int p)
{
int pval;
for(pval=st[p-1]+1;pval<=n-k+p;pval++) {
st[p]=pval;
if(p==k)
afisare();
else bktr(p+1);
}
}
int main ()
{
ifstream f("combinari.in");
f>>n>>k;
f.close();
bktr(1);
g.close();
return 0;
}
Scriet¸i un program C/C++ care cite¸ste din fi¸sierul standard de intrare (tastatura)
¸siruri de caractere de forma cuvˆant#tip, unde cuvˆant este un ¸sir oarecare de litere
iar tip poate fi una din literele S,P sau C, semnificat¸ia fiind subiect, predicat sau
complement. Aplicat¸ia va afi¸sa pe ecran toate propozit¸iile avˆand structura subiect
predicat complement ce pot fi formate cu ajutorul cuvintelor citite. Datele de intrare
se consider˘a a fi corecte.
Exemplu: dac˘a la intrare s-au introdus ¸sirurile: Ion#S Vasile#S alearga#P
repede#C scrie#P, atunci pe ecran se va afi¸sa: Ion scrie repede, Ion alearga
repede, Vasile scrie repede, Vasile alearga repede
back(nivel)
{
if(nivel==4) //am pus si subiect si predicat si complement
afisare();
else
{
for ( int i = 1; i <= M[ nivel ][ 0 ]; i++ ) //M[nivel][0] =
cardinalul multimii M[nivel], multime in care stochezi cele 3 multimi S,P,C; ai un
masiv 3-dimensional unde stochezi sirurile
{
st[ nivel ] = M[ nivel ][ i ];
back(nivel+1);
}
}
}
criet¸i un program C/C++ care cite¸ste din fi¸sierul standard de intrare (tastatura)
¸siruri de caractere de forma cuvˆant#tip, unde cuvˆant este un ¸sir oarecare de litere
iar tip poate fi una din literele S,P sau C, semnificat¸ia fiind subiect, predicat sau
complement. Aplicat¸ia va afi¸sa pe ecran toate propozit¸iile avˆand structura subiect
predicat complement ce pot fi formate cu ajutorul cuvintelor citite. Datele de intrare
se consider˘a a fi corecte.
Exemplu: dac˘a la intrare s-au introdus ¸sirurile: Ion#S Vasile#S alearga#P
repede#C scrie#P, atunci pe ecran se va afi¸sa: Ion scrie repede, Ion alearga
repede, Vasile scrie repede, Vasile alearga repede
#include<fstream>
using namespace std;
int n,ST[100],as,ev,k,y=0,l=0,z=0;
ifstream f("paranteze.in");
ofstream g("paranteze.out");
void init()
{
ST[k]=-1;
void succ()
{
if(ST[k]<2)
{
ST[k]++;
as=1;
}
else as=0;
void valid()
{
ev=1;
for(int i=1;i<=k;i++)
{
if(ST[i]==0)
{
y++;
for(int j=i+1;j<=k;j++)
{
if(ST[j]==1)
l++;}}
else z++;
}
if(l==0)
ev=0;
if(z!=y!=n/2)
ev=0;
}
void tipar()
{
for(int i=1;i<=k;i++)
g<<ST[i]<<" ";
g<<endl;
}
int sol()
{
return (k==n);
}
void back()
{
k=1;
init();
while(k)
{
do{
succ();
if(as)
valid();
}
while(as && !ev);
if(as) if(sol())
tipar();
else{
k++;
init();
}
else k--;
}
int main()
{
f>>n;
back();
f.close();
g.close();
return 0;
}
https://www.geeksforgeeks.org/c-programs-gq/cc-backtracking-programs-gq/
Sudoku | Backtracking-7
https://www.geeksforgeeks.org/sudoku-
backtracking-7/
Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty
cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from
1 to 9.
Naive Algorithm
The Naive Algorithm is to generate all possible configurations of numbers from 1 to 9 to fill the empty
cells. Try every configuration one by one until the correct configuration is found.
Backtracking Algorithm
Like all other Backtracking problems, we can solve Sudoku by one by one assigning numbers to empty
cells. Before assigning a number, we check whether it is safe to assign. We basically check that the
same number is not present in the current row, current column and current 3X3 subgrid. After checking
for safety, we assign the number, and recursively check whether this assignment leads to a solution or
not. If the assignment doesn’t lead to a solution, then we try next number for the current empty cell.
And if none of the number (1 to 9) leads to a solution, we return false.
Following are the implementation for Sudoku problem. It prints the completely filled grid as output.
filter_none
edit
play_arrow
brightness_4
// Checks whether it will be legal to assign num to the given row, col
bool isSafe(int grid[N][N], int row, int col, int num);
// consider digits 1 to 9
for (int num = 1; num <= 9; num++)
{
// if looks promising
if (isSafe(grid, row, col, num))
{
// make tentative assignment
grid[row][col] = num;
return 0;
}
Output:
3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9
Se dă un labirint reprezentat sub forma unei matrici cu n linii şi m coloane, conţinând doar valori 0 şi 1. O
succesiune de 1 reprezintă un drum iar o (zero) reprezintă un zid. Dintr-un punct iniţial, a cărui coordonate se
citesc, porneşte o persoană. Să se afişeze toate posibilităţile ca acea persoană să ajunga in pozitia finala, ştiind
că deplasarea se poate face doar prin valori 1 ale matricei, iar salturile se pot face în stânga, dreapta, jos şi sus.
158
211
336
401000010
501111110
601000010
711111110
800001000
Cu urmatoarea semnificatie:
Nord: (-1, 0)
Est: (0, +1)
Sud: (+1, 0)
Vest: (0, -1)
Sa trecem la rezolvarea problemei, o sa explic fiecare functie in parte mai jos, si de asemenea o sa fac si
o mica animatie, dupa cum v-am obisnuit.
Primeste ca si parametrii doua numere intregi i si j (sunt numere intregi deoarece pot sa primeasca si -1,
altfel le-am fi declarat „unsigned int”)
Functia noastra verifica prima oara daca noile coordonate sunt in-afara matricei
Mai apoi, accepta toate casutele ce sunt marcate cu 1 (drum disponibil, inca nevizitat) si de-asemenea
casuta de final (casuta de final va avea o valoare diferita de 1, in cazul in care se parcurge a doua oara –
a trebuit tratat separat acest caz)
La final respingem toate casutele de 0 sau casutele diferite de 1 (casute disponibile, dar vizitate)
Este o functie recursiva ce parcurge pe rand vecinii, iar mai apoi se intoarce de unde a plecat – similar
algoritmului lui Lee
Aceasta functie verifica pe rand toti cei 4 vecini. In cazul in care casuta poate fi vizitata, aceasta functie
o viziteaza, iar cand se intoarce din recursivitate marcheaza casuta cu „0” (drum indisponibil)
https://tutoriale-pe.net/problema-labirintului-in-c-backtracking/
#include <fstream>
using namespace std;
ifstream fin("labirint.in");
int N, M;
void Citire()
{
fin >> N >> M;
fin >> poz_init_X >> poz_init_Y;
fin >> poz_finala_X >> poz_finala_Y;
if(matrice[i][j] == 0 || matrice[i][j])
return false;
return true;
}
void afisare()
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
cout << matrice[i][j] << " ";
cout << "\n";
}
cout << "\n\n";
}
int main()
{
Citire();
Rezolvare(poz_init_X, poz_init_Y, 2);
return 0;
}
Enuntul problemei:
Se da o tabla de sah cu dimensiunea NxN. Un cal se gaseste in linia 1 si coloana 1. Gasiti un sir de
mutari ale calului astfel incat acesta sa acopere intreaga tabla de sah fara a trece printr-o casuta de 2 ori.
Acesta problema este foarte asemanatoare cu problema labirintului. Doar ca difera ceva, de data asta nu
ne mai deplasam nord – sud – est – vest, ne deplasam cum se deplaseaza calul pe tabla de sah.
Am incercat mai sus sa desenez toate mutarile posibile pe care le poate face calutul nostru. Daca
transpunem poza in doi vectori de directii, rezultatul este urmatorul:
#include <iostream>
int n;
int matrice[105][105], xi = 0, yi = 0;
void Citire()
cin >> n;
matrice[xi][yi] = 1;
}
bool isFull()
if (matrice[i][j] == 0)
return false;
return true;
void Solutie()
static int w = 1;
w ++;
{
if(i > n - 1 || i < 0)
return false;
return false;
if(matrice[i][j])
return false;
return true;
static int r = 2;
if(isFull())
Solutie();
//exit(1);
return ;
if (isOK(i_vecin, j_vecin))
matrice[i_vecin][j_vecin] = r++;
Rezolvare(i_vecin, j_vecin);
matrice[i_vecin][j_vecin] = 0;
r--;
}
int main()
Citire();
Rezolvare(xi, yi);
return 0;
Primeste ca si parametrii doua numere intregi x si y (sunt numere intregi deoarece pot sa primeasca si -
1, altfel le-am fi declarat „unsigned int”)
Functia noastra verifica prima oara daca noile coordonate sunt in-afara matricei
De asemenea, casuta este respinsa daca a mai fost vizitata in trecut (valoarea mai mare ca 1)
Este o functie recursiva ce parcurge pe rand vecinii, iar mai apoi se intoarce de unde a plecat –
similar algoritmului lui Lee
Aceasta functie verifica pe rand toti cei 8 vecini. In cazul in care casuta poate fi vizitata, aceasta functie
o viziteaza, iar cand se intoarce din recursivitate marcheaza casuta cu „0” (casuta nevizitata)
https://tutoriale-pe.net/problema-calului-in-c-backtracking/
http://docerp.ro/modelling_chess