301-22 guruh talabasi Pirimqulova Zilola
Hanoiy minoralari masalasi
Masalaning qo’yilishi: Masalaning maqsadi quyidagi qoidalarga rioya qilgan holda butun to'plamni oxirgi tayoqqa o'tkazishdir:
1.Bir vaqtning o'zida faqat bitta diskni ko'chirish mumkin.
2.Har bir harakat yuqori diskni stacklardan biridan olish va uni boshqa stack ustiga yoki bo'sh novda ustiga qo'yishdan iborat.
3. Undan kichikroq diskning ustiga disk qo'yish mumkin emas.
Dastur codi:
say(N,From,To) :-
write(N),
write(' radiusli diskni '),
write(From),
write('-minoradan '),
write(To),
write('-minoraga ko''chiring.'),
nl.
move(1, From, To, _) :-
say(1, From, To).
move(N,From,To,Helper) :-
N>1,
M is N-1,
move(M,From,Helper,To),
say(N, From, To),
move(M,Helper,To,From).
main :-
N is 3,
From is 1,
To is 3,
Helper is 2,
move(N, From, To, Helper).
:- main.
Natija:
Do'stlaringiz bilan baham: |