-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathback iterativ.cpp
59 lines (58 loc) · 964 Bytes
/
back iterativ.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
# include <fstream>
using namespace std;
ifstream f("back.in");
ofstream g("back.out");
int n,i,st[100],as,ev,vi,vf,rel;
void init (int k)
{
st[k]=vi-rel;
}
void succesor (int k, int &as)
{
if (st[k]+rel<=vf) st[k]+=rel,as=1;
else as=0;
}
void valid (int k, int &ev)
{
int i; ev=1;
for (i=1; i<k; ++i)
if (st[i]==st[k]) ev=0;
}
int solutie (int k)
{
if (k==n) return 1;
else return 0;
}
void tipar ()
{
for (int i=1; i<=n; ++i)
g<<st[i]<<" ";
g<<"\n";
}
void back (int k)
{
init (k);
while (k>0)
{
do {
succesor (k, as);
if (as) valid (k, ev);
}while (as && !ev);
if (as)
{
if (solutie(k)) tipar();
else {
++k;
init(k);
}
}
else --k;
}
}
int main ()
{
f>>n;
vi=1; vf=n; rel=1;
back (1);
return 0;
}