Professional Documents
Culture Documents
Evaluare: Utilizarea Tehnicilor Clasice de Programare
Evaluare: Utilizarea Tehnicilor Clasice de Programare
Evaluare: Utilizarea Tehnicilor Clasice de Programare
DISCIPLINA
Evaluare
Chișinău, 2019
Problema 1
Rezolvare:
#include <fstream>
using namespace std;
ifstream fin("lee.in");
ofstream fout("lee.out");
struct Pos {
int lin, col;
};
Pos a, b;
int m, n;
int mat[DMAX][DMAX];
int vf;
Pos st[DMAX * DMAX];
void scan() {
fin >> m >> n;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
fin >> mat[i][j];
fin >> a.lin >> a.col;
fin >> b.lin >> b.col;
}
void surround() {
for (int i = 0; i <= m + 1; i++)
mat[i][0] = mat[i][n + 1] = -1;
for (int j = 1; j <= n; j++)
mat[0][j] = mat[m + 1][j] = -1;
}
void lee() {
q[0] = a;
in = sf = 0;
mat[a.lin][a.col] = 1;
Pos pos;
while (in <= sf && !mat[b.lin][b.col]) {
pos = q[in++];
for (int k = 0; k < 4; k++) {
Pos ngh;
ngh.lin = pos.lin + addLin[k];
ngh.col = pos.col + addCol[k];
if (!mat[ngh.lin][ngh.col]) {
mat[ngh.lin][ngh.col] = mat[pos.lin][pos.col] + 1;
q[++sf] = ngh;
}
}
}
}
void print() {
Pos pos = st[++vf] = b;
while (mat[pos.lin][pos.col] > 1)
for (int k = 0; k < 4; k++) {
Pos ngh;
ngh.lin = pos.lin + addLin[k];
ngh.col = pos.col + addCol[k];
if (mat[ngh.lin][ngh.col] == mat[pos.lin][pos.col] - 1) {
st[++vf] = pos = ngh;
break;
}
}
int main() {
scan();
surround();
lee();
print();
fout.close();
return 0;
}
Lee.in:
Lee.out:
11
12
13
14
15
16
26
36
46
47
48
49
4 10
5 10
6 10
7 10
7 10
Problema 2
Problema 3
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
int n,v[100],sol;
void afisare()
{int i,j,x;
sol++;
cout<<"\n Solutia: "<<sol<<'\n';
for (i=1;i<=n;i++)
{for (j=1;j<=n;j++)
if (v[i]==j) cout<<"T";
else cout<<"_ ";
cout<<'\n';
}
}
//Subprograma valid controleaza daca damele se intersecteaza pe linii,coloane sau
diagonale
int valid(int k)
{int i;
for (i=1;i<=k-1;i++)
if ((v[i]==v[k]))
return 0;
return 1;}
int solutie(int k)
{if (k==n)
return 1;
return 0;}
//Subprograma bk este folosita pentru a controla daca damele nu se intersecteaza,si a
afisa rezultatele
void BK(int k)
{int i;
for (i=1;i<=n;i++)
{v[k]=i;
if (valid(k)==1)
{if (solutie(k)==1)
afisare();
else
BK(k+1);
}
}
}
int main()
{cout<<"n= ";cin>>n;
BK(1);
}