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:

  • Cài đặt: Hướng dẫn cài đặt Visual Studio 2008 (hình ảnh)Bước 1: Mở thư mục chứa tệp visual2008.iso –> Nháy phải chuột vào tên tệp –> Chọn PowerISO –> Chọn Mount Image to Drive ….(Bước này có thể bỏ qua nếu chúng ta có bộ cài từ DVD hoặc trong một thư mục)Bước 2: Mở ổ đĩa DVD (Ví dụ ổ F:) –> Nháy đúp chuột vào tệp chương trình cài đặtBước 3: Chọn Install Visual Studio 2008 –> Chọn Next để bắt đầu cài đặt… Read More
  • ASP.NET: Thực thi truy vấn SELECT sử dụng đối tượng SQLCommand<%@ Page Language="VB" Debug="true" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SQLClient" %><script language="VB" runat="server">Sub Page_Load(Source as Object, E as EventArgs)Dim conn As New SQLConnection("server=LOCALHOST;User id='sa';password='123';database=Northwind")Dim cmd As New SqlCommand("Select categoryNa… Read More
  • ASP.NET: Tạo lớp kết nối CSDL trong ASP.NETViệc tạo lớp (class) kết nối cơ sở dữ liệu rất có lợi, ta có thể tiết kiệm thời gian viết lại chuỗi kết nối cơ sở dữ liệu nhiều lần, lúc sửa lại Server hay CSDL thì ta không phải đến từng trang một mà chỉ cần sửa trực tiếp trên chuỗi kết nối là được.Thêm Class kết nốiKhi đã tạo dự án (project) website bằng Visual Studio vào menu Solution Explorer tạo một đối tượng (it… Read More
  • HTML: Tạo Frame sử dụng ngôn ngữ HTML1.1. KHÁI QUÁT VỀ FRAME- Có thể phân chia một trang thành các khung, cho phép người truy cập cùng một lúc có thể xem nhiều trang mà không cần cuộn màn hình, mỗi khung chứa một trang Web riêng.- Nếu tài trang sử dụng Frame thì không sử thẻ Body1.2. CÁCH TẠO MỘT FRAME LAYOUT<HTML><HEAD>                  <TITLE&… Read More
  • Cài đặt: Hướng dẫn cài đặt Visual Studio 2010 (hình ảnh)Bước 1: Click đúp vào file setup trong thư mục chứa bộ cài đặt (trong ổ cứng hoặc đĩa DVD chương trình). Màn hình lựa chọn cài đặt hiển thị, chọn Install Microsoft Visual Studio 2010Bước 2: Chương trình sẽ giải nén bộ cài đặt để sẵn sàng cho việc cài đặt. Bạn sẽ nhìn thấy màn hình lựa chọn như bên dưới.Bước 3: Chọn NextBước 4. Chương trình sẽ tự động cài đặt một số ứn… Read More
  • Cài đặt: Hướng dẫn cài đặt Visual Studio 2005 (hình ảnh)Bước 1: Đưa đĩa DVD vào ổ đĩa của máy tính, nếu Autorun được bật, quá trình cài đặt sẽ hiển thị các lựa chọn trên màn hình. Ngược lại, các bạn phải click vào file Setup trong bộ cài để cài đặt bằng tay. Màn hình lựa chọn cài đặt xuất hiện như hình dưới, lựa chọn Instal Visual Studio để bắt đầu quá trình cài đặt:Bước 2: Quá trình này sẽ copy các file cài đặt cần thiết … 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@