-
Notifications
You must be signed in to change notification settings - Fork 1
/
udtt.cpp
175 lines (165 loc) · 3.23 KB
/
udtt.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <bits/stdc++.h>
using namespace std;
void SinhNhiPhan()
{
int n;
int p[1000];
cout << "Do dai: ";
cin >> n;
for (int i=1;i<=n;i++)
{
p[i]=0;
cout << p[i];
}
cout<<endl;
while (true) {
bool isOver=true;
for (int i=n;i>=1;i--)
if (p[i]==0)
{
p[i]=1;
isOver=false;
break;
}
else p[i]=0;
if (isOver) break;
for (int i=1;i<=n;i++)
cout << p[i];
cout << endl;
}
}
//------------HOAN VI-----------------------
bool kt[1000];
int pp[100],a[100];
void backtrack(int i, int n)
{
for (int j=1;j<=n;j++)
if (kt[j]==false)
{
pp[i]=a[j];
kt[j]=true;
if (i<n)
{
backtrack(i+1,n);
}
else
{
for (int jjj=1;jjj<=n;jjj++)
cout << pp[jjj]<< " ";
cout << endl;
}
kt[j]=false;
}
}
void HoanVi()
{
int n,k;
cout << "n= ";cin >> n;
for (int i=1;i<=n;i++)
{
a[i]=i;
kt[i]=false;
pp[i]=0;
}
backtrack(1,n);
}
//---------FIBONACCI------------
void fibo1()
{
int n;
cout << "n= ";cin >> n;
a[1]=1;
a[2]=1;
for (int i=3;i<=n;i++)
a[i]=a[i-1]+a[i-2];
cout << a[n];
}
int Fibonacci(int i)
{
if (i==1) return 1;
if (i==2) return 1;
return Fibonacci(i-1) + Fibonacci(i-2);
}
void fibo2()
{
int n;
cout << "n= ";cin >> n;
cout << Fibonacci(n);
}
//------------THAP HA NOI---------------
void Tower(int n , char a, char b, char c ){
if(n==1){
cout<<"\t"<<a<<"-------"<<c<<endl;
return;
}
Tower(n-1,a,c,b);
Tower(1,a,b,c);
Tower(n-1,b,a,c);
}
void ThapHaNoi()
{
char a='A', b='B', c='C';
int n;
cout<<"Nhap n: ";
cin>>n;
Tower(n,a,b,c);
}
//-------------------MA DI TUAN---------------
void backtrackMa(int x, int y, int m, int n)
{
cout << "{"<<x << ";"<<y << "}"<<endl;
if (x+2 <= m && y+1<=n)
backtrackMa(x+2,y+1,m,n);
if (x-2 >=1 && y+1<=n)
backtrackMa(x-2,y+1,m,n);
if (x+2 <= m && y-1>=1)
backtrackMa(x+2,y-1,m,n);
if (x-2 >=1 && y-1>=1)
backtrackMa(x-2,y-1,m,n);
if (x+1 <= m && y+2<=n)
backtrackMa(x+1,y+2,m,n);
if (x-1 >=1 && y+2<=n)
backtrackMa(x-1,y+2,m,n);
if (x+1 <= m && y-2>=1)
backtrackMa(x+1,y-2,m,n);
if (x-1 >=1 && y-2>=1)
backtrackMa(x-1,y-2,m,n);
}
void MaDiTuan()
{
int m,n;
cout << "m= ";cin >> m;
cout << "n= ";cin >> n;
backtrackMa(1,1,m,n);
}
//-------------------8 HAU-----------------------
bool Ok(int x2,int y2){
for(int i = 1; i < x2 ;i++)
if(a[i] == y2 || abs(i-x2) == abs(a[i] - y2) )
return false;
return true;
}
void Xuat(int n){
for(int i=1;i<=n;i++)
cout << "{"<<a[i] << ";"<<i<<"} ";
cout <<endl;
}
void Try(int i,int n){
for(int j = 1;j<=n;j++){
if(Ok(i,j)){
a[i] = j;
if(i==n) Xuat(n);
Try(i+1,n);
}
}
}
void TamHau()
{
Try(1,8);
return 0;
}
int main()
{
HoanVi();
return 0;
}