Recent Posts

Code C-C++: Bài toán Tháp Hà Nội

-

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

#define MAX 12
#define BegPos   105
#define AuxPos   305
#define EndPos   505

int width;
typedef struct disc
  {
char val1[MAX];
char top,pos;
};

void push(disc *tt,int x);
pop(disc *tt);
void tower(int,disc *,disc *,disc *);
void draw_stack(disc *beg,disc *,disc *);
int main(void)
{

   int gdriver = DETECT, gmode, errorcode;
   int i,x=2;
   disc beg,end,aux;
   printf("

TOWER OF HANOI
");
   printf("=======================================================");
   printf("

How Many Disks[1-10]:-  ");
   scanf("%d",&x);

   initgraph(&gdriver, &gmode, "d:\TC\BGI");
   errorcode = graphresult();
   if (errorcode != grOk)
   {
 printf("Graphics error: %s
", grapherrormsg(errorcode));
 printf("Press any key to halt:");
 getch();
 exit(1);
   }
width=50/x;

beg.top=end.top=aux.top=0;
beg.pos=1;end.pos=3;aux.pos=2;

for(i=0;i<x;i++)
push(&beg,(x-i)+1);

draw_stack(&beg,&end,&aux);
tower(x,&beg,&end,&aux);

   closegraph();
   return 0;
}
void tower(int n,disc *beg,disc *aux,disc *end)
{
if(n>0)
/* {
push(end,pop(beg));
draw_stack(beg,end,aux);
}
else*/
{
tower(n-1,beg,end,aux);
push(end,pop(beg));
draw_stack(beg,end,aux);
tower(n-1,aux,beg,end);
}
//
}
void push(disc *tt,int x)
{
tt->val1[tt->top]=x;
tt->top++;
}

pop(disc *tt)
{
int a;
tt->top--;
a=tt->val1[tt->top];
tt->val1[tt->top]=0;
return a;
}

void draw_stack(disc *beg,disc *end,disc *aux)
{
int ypos=295,i,height=10,xpos;
int ver=0;
cleardevice();

setfillstyle(1,2);
bar(20,300,580,310);

bar(100,100,110,300);
bar(300,100,310,300);
bar(500,100,510,300);

rectangle(20,300,580,310);

rectangle(100,100,110,300);
rectangle(300,100,310,300);
rectangle(500,100,510,300);

/* END TOWER*/
ypos=295;
if(end->pos==1)
xpos=BegPos;
else if(end->pos==2)
xpos=AuxPos;
else if(end->pos==3)
xpos=EndPos;

for(i=0;i<end->top;i++)
{
setfillstyle(end->val1[i],end->val1[i]);

bar(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height);

rectangle(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height);
ypos-=(height+2);
}
ver=end->pos;

/* BEG TOWER*/
if(beg->pos==1)
xpos=BegPos;
else if(beg->pos==2)
xpos=AuxPos;
else if(beg->pos==3)
xpos=EndPos;

ypos=295;
for(i=0;i<beg->top;i++)
{
setfillstyle(beg->val1[i],beg->val1[i]);

bar(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height);

rectangle(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height);
ypos-=(height+2);
}

/* AUX TOWER*/
ver=ver*10+beg->pos;

if(ver<20)
{
if(ver%10==2)
xpos=EndPos;
else
xpos=AuxPos;
}
else if(ver<30)
{
if(ver%10==1)
xpos=EndPos;
else
xpos=BegPos;
}
else if(ver<40)
{
if(ver%10==1)
xpos=AuxPos;
else
xpos=BegPos;
}

ypos=295;
for(i=0;i<aux->top;i++)
{
setfillstyle(aux->val1[i],aux->val1[i]);

bar(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height);

rectangle(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height);
ypos-=(height+2);
}
getch();
}

Related Post:

  • Code C-C++: Bài toán Tháp Hà Nội#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>#define MAX 12#define BegPos   105#define AuxPos   305#define EndPos   505int width;typedef struct disc   { char val1[MAX]; char top,pos; };void push(disc *tt,int x);pop(disc *tt);void tower(int,disc *,disc *,disc *);void draw_stack(disc *beg,d… Read More
  • Assembly: Hiển thị bộ nhớ (Show Memory)kbd             equ     16h             ;keyboard irqmsdos           equ     21h             ;MSDOS irqreset           equ     0dh             ;disk resetdfopen   &n… Read More
  • PHP: Hàm trả về vị trí xuất hiện của chuỗi s1 trong chuỗi s2<?php /*  This  function  returns  the  position  of  string  s1  within  string  s2.       The  position  is  1  based.    If  s1  is  not  in  s2,  0  is  returned. */ function  InStr($s1,  $s2)&… Read More
  • XML: Tài liệu XML có cấu trúc CâyCác tài liệu XML phải có một yếu tố gốc. Yếu tố này là "cha mẹ" của tất cả các yếu tố khác.Các yếu tố trong một tài liệu XML tạo thành một cây tài liệu. Cây bắt đầu ở gốc và các nhánh đến mức thấp nhất của cây.Tất cả các yếu tố có thể có yếu tố phụ (các phần tử con):<root>  <child>    <subchild>.....</subchild>  </ch… Read More
  • Java: Xây dựng chương trình Client - Server ở chế độ có nối kết (TCP)          Dịch vụ Echo được thiết kế theo kiến trúc Client-Server sử dụng Socket làm phương tiện giao tiếp. Cổng mặc định dành cho Echo Server là 7, bao gồm cả hai chế độ có nối kết và không nối kết. Chương trình TCPEchoClient sẽ nối kết đến EchoServer ở chế độ có nối kết, lần lượt gửi đến Echo Server 10 ký tự từ ‘0’ đến '9', chờ nhận kế… Read More
  • Java: Xây dựng chương trình Client - Server ở chế độ KHÔNG kết nối (UDP)Chương trình UDPEchoServer cài đặt Echo Server ở chế độ không nối kết, cổng mặc định là 7. Chương trình chờ nhận từng gói tin, lấy dữ liệu ra khỏi gói tin nhận được và gởi ngược dữ liệu đó về Client.1. UDPEchoServer.javaimport java.net.*;import java.io.*; public class UDPEchoServer {     public final static int port = 7; // Cong mac dinh cua Server &nbs… Read More

2 nhận xét

Maaf Komentar Anda telah dihapus/disembunyikan. Kemungkinan hal ini terjadi karena Anda menuliskan komentar disertai dengan link aktif yang tidak diperlukan pembaca/tidak sesuai dengan diskusi. Terima kasih banyak atas kontribusi Anda.

Maaf Komentar Anda telah dihapus/disembunyikan. Kemungkinan hal ini terjadi karena Anda menuliskan komentar disertai dengan link aktif yang tidak diperlukan pembaca/tidak sesuai dengan diskusi. Terima kasih banyak atas kontribusi Anda.




Klik untuk melihat kode: :) =( :s :D :-D ^:D ^o^ 7:( :Q :p T_T @@, :-a :W *fck* x@