-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtabel.cpp
76 lines (74 loc) · 1.47 KB
/
tabel.cpp
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
# include <fstream>
using namespace std;
ifstream f("tabel.in");
ofstream g("tabel.out");
int i,j,v,l,c,lipsa,n,m,p;
int a[100][100],L[100],C[100];
void linie (int l)
{
int i,ok=1,s=0,c=0;
for (i=1; i<m; ++i)
if (a[l][i]==-1) ok=0,c=i;
else s+=a[l][i];
if (ok) {//este elem de pe ultima coloana
a[l][m]=s;
--L[l]; --C[m]; --lipsa;
}
else {
a[l][c]=a[l][m]-s;
--L[l]; --C[c]; --lipsa;
}
}
void coloana (int c)
{
int i,ok=1,s=0,l=0;
for (i=1; i<n; ++i)
if (a[i][c]==-1) ok=0,l=i;
else s+=a[i][c];
if (ok) {
a[n][c]=s;
--L[n]; --C[c]; --lipsa;
}
else {
a[l][c]=a[n][c]-s;
--L[l]; --C[c]; --lipsa;
}
}
void rezolva()
{
int i;
while (lipsa)
{
//verificam liniile
for (i=1; i<=n; ++i)
if (L[i]==1) linie(i);
for (i=1; i<=m; ++i)
if (C[i]==1) coloana(i);
}
}
int main ()
{
f>>n>>m>>p;
lipsa=n*m;
for (i=1; i<=n; ++i)
for (j=1; j<=m; ++j)
a[i][j]=-1;
for (i=1; i<=n; ++i)
L[i]=m;
for (i=1; i<=m; ++i)
C[i]=n;
for (i=1; i<=p; ++i)
{
f>>l>>c>>v;
a[l][c]=v;
--L[l]; --C[c]; --lipsa;
}
rezolva();
for (i=1; i<=n; ++i)
{
for (j=1; j<=m; ++j)
g<<a[i][j]<<" ";
g<<"\n";
}
return 0;
}