hien_hien_1112

New Member
Đây là bài tâp trí tuệ nhân tạo:


Cho 2 bình có dung tích lần lượt là m và n (lit). Với nguồn nước không hạn chế, dùng 2 bình trên để đong k lit nước.


Mình dùng thuật toán tìm kiếm theo chiều rộng. Code như sau:


#include


#include


enum boolean {false, true};



/*************************** CAU HINH **************************************/


typedef struct CauHinh


{ int Truoc, x, y, SHDinh; };



/*************************** CAU TRUC MOT NUT ******************************/


class Nodes


{


public:


CauHinh Info;


Nodes *Link;


Nodes() {};


Nodes(CauHinh& temp, Nodes *next = NULL)


{ Info = temp; Link = next; }


};



/*************************** HANG DOI (QUEUE) ******************************/


class List


{


protected:


Nodes *LTop;


public:


List() { LTop = NULL; }



// Ham tra ve dia chi nut dau danh sach


Nodes *getLTop() { return LTop; }



// Kiem tra xem danh sach co rong ko?


boolean isEmpty()


{


if (LTop==NULL) return true;


else return false;


}



// Nap mot phan tu vao Queue


void pushQueue(CauHinh& temp)


{


Nodes *p = new Nodes(temp, LTop);


LTop = p;


}



// Day mot phan tu ra khoi Queue


CauHinh popQueue();



// Ham tra ve dia chi nut co SHDinh = n


Nodes *timDinh(int& n);



// Ham kiem tra mot cau hinh co thuoc danh sach hay ko?


Nodes *checkL(int& z, int& t);



// Ham noi danh sach L2 vao danh sach L1


void addList(List& L2);


};


// Day mot phan tu ra khoi Queue


CauHinh List::popQueue()


{


Nodes *p = LTop;


while (p->Link != NULL) p = p->Link;


if (LTop->Link == NULL) LTop = NULL;


CauHinh temp = p->Info;


delete p;


return temp;


}



// Ham tra ve dia chi nut co gia tri SHDinh = n


Nodes *List::timDinh(int& n)


{


Nodes *p = LTop;


while (p != NULL || p->Info.SHDinh != n) p = p->Link;


return p;


}



// Ham kiem tra mot cau hinh co thuoc danh sach hay ko?


Nodes *List::checkL(int& z, int& t)


{


Nodes *p = LTop;


while (p != NULL || (p->Info.x != z && p->Info.y != t))


p = p->Link;


return p;


}



// Ham noi danh sach L2 vao danh sach L1


void List::addList(List& L2)


{


Nodes *p = LTop;


while (p->Link !=NULL) p = p->Link;


p->Link = L2.LTop;


}



/*************************** BAI TOAN DONG NUOC ****************************/


int m, n, k;


// Cac thao tac do nuoc:


void doNuoc(CauHinh u, CauHinh& v, int& i)


{


switch (i)


{


case 1: { v.x = n; v.y = u.y; }


case 2: { v.x = u.x; v.y = m; }


case 3: { v.x = 0; v.y = u.y; }


case 4: { v.x = u.x; v.y = 0; }


case 5:


{


if (u.x + u.y <= m) { v.x = 0; v.y = u.x + u.y; }


else { v.x = u.x + u.y - m; v.y = m; }


}


case 6:


{


if (u.x + u.y <= n) { v.x = u.x + u.y; v.y = 0; }


else { v.x = n; v.y = u.x + u.y -n; }


}


}


}



// IN KET QUA


void inKetQua(List& Mo, List& Dong, CauHinh& v)


{


Mo.addList(Dong);


cout<<"("<

int temp = v.Truoc;


while (temp != 0)


{


Nodes *p = Mo.timDinh(temp);


cout<<" <-- ("<Info.x<<", "<Info.y<<")";


temp = p->Info.Truoc;


}


}



// TIM KIEM RONG


void BrFS(CauHinh& temp)


{


List Mo, Dong;


Mo.pushQueue(temp);


while (!Mo.isEmpty())


{


CauHinh u = Mo.popQueue();


Dong.pushQueue(u);


int dinh = u.SHDinh;


for (int i = 1; i <= 6; i++)


{


CauHinh v;


doNuoc(u,v,i);


if (Mo.checkL(v.x,v.y) != NULL && Dong.checkL(v.x,v.y) != NULL)


{


v.Truoc = dinh;


dinh++;


v.SHDinh = dinh;


Mo.pushQueue(v);


if (v.x == k || v.y == k)


{


inKetQua(Mo,Dong,v);


break;


}


}


}


}


cout<<" Khong thanh cong !";


}



/*************************** HAM MAIN **************************************/


int main()


{


clrscr();


CauHinh xp;


cout<<"Nhap m, n, k: ";


cin>>m>>n>>k;


xp.Truoc = 0; xp.x = 0; xp.y = 0;


xp.SHDinh = 1;


BrFS(xp);


getch();


return 0;


}


Các pro giúp mình cai nha. Thanks![/SIZE]
 

y3u_la_kj_ji

New Member
chaytuiko cái gì thế này Bạn không hiểu cái gì? Code viết bằng Borland C++ đó, sử dụng thuật toán BrFS trên danh sách móc nối đơn.
 
thế cậu hỏi gì


tìm chỗ sai à


tui học cái này còn gà lắm nên thuật toán không được nuột cho lắm


copy hẳn cái notepad c++ cho tui xem được không cả hai cùng học


thuật toán sai do một số lỗi vô tình không để ý trong thuật toán
 

vitconxauxi0780

New Member
chaytuiko cái gì thế này làm ra (tạo) node , sử dụng danh sách liên kết đơn và đôi , đọc lý thuyết nhiều vào rồi trả lời
 

bigq_301

New Member
cu phap khong sai.nhung ma giai thuat thi co van de do


nhap vao 5,4,3 khong dc 8,5,2 cung khong dc.xem lai giai thuat di
 

kt_05

New Member
các bạn xem giúp bài tập này nhé thầy mình bảo viết bằng lập trình hướng đối tượng c++ nó khó quá nên nhờ mấy bạn giúp dùm:


LAB 1


Goal:


• To familiarize yourself with coding in Object Oriented Programming


• Getting started with Classes, Objects and their usage



Problem Description


In this lab, you are going to develop a calendar system which prints out the dates (along with corresponding days) for the given a month and year. User inputs the following


details to Giúp you calculate the required month’s days:


1. Year – Year for which the calendar is required


2. Month – Month for which you have to output the calendar


3. New Year Day – Day on which Jan 1st falls. For example, for 2006, it would


be Sunday


Year should be used to check whether it is a Leap year or not. YYYY is leap year if


following two conditions hold:


• should be perfectly divisible by 4


• If it is divisible by 100 then it should not be divisible by 400


New Year Day (NYD) is required so that you can calculate the starting day of the


required month i.e., unless you know that year 2006 starts with Sunday, you will not be


able to know that May 2006 starts with Monday.



Format of Input


• Year – YYYY (Ex: 1999)


• Month – MM (Ex: 11, 04 or 4. Leading zero is optional i.e., user has a


choice to enter either 04 or 4)


• New Year Day (NYD) – One of the following characters


o M – Monday


o T – Tuesday


o W – Wednesday


o R – Thursday


o F – Friday


o S – Saturday


o U – Sunday


Appropriate error should be raised if any of the input is not in the correct format.


Examples of bad input are: 99 for year, 15 for month, ‘T’ for NYD.



Example Input & Output


Year: 2006


Month: 06


NYD: R


Output:


Su Mo Tu We Th Fr Sa


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




Year: 2008


Month: 4


NYD: T


Output:


Su Mo Tu We Th Fr Sa


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



Note that, NYD can be any day. It need not be the exact starting day of the year. For


example, in the first example it is valid to input NYD as Monday though 2006 starts with


a Sunday. It should not matter because you will be calculating according to the day


given and you do not have any other way to get the starting day of the year. While testing your program, you can test with the actual day of the year. If you give arbitrary NYD, then verification of your output can be daunting.



Suggested Approach


Here is how I would do this problem. It is just to give you a hint. You can do whatever


way you can. Intelligent solutions will enjoy extra credits!! Create an array of 12


numbers which stores the number of days in each month. Here, take care of the leap year!


Based on the NYD and this array, calculate the first day of the required month. Once you


get that, it is straight forward to printout the calendar. You just have to start with that


date and print dates by making sure to insert a new line after every Saturday. You also


need to print enough blank spaces so that Date 1 starts from appropriate position in the


line. You should also print the header line (Su Mo Tu We Th Fr Sa).



Requirements of the Program


Though this program can be done without using C++ features or classes, since it is a


C++ course, you SHOULD use classes and objects to code. To be specific, I need you to


have two classes with data memebers:


• Date


o nDay – Number (1..31)


o sDay – Day (U/M/T/W/R/F/S)


• Month


o Month Number (1..12)


o noDays _ # of days (28/29/30/31)


o Starting day – Day of date 1 of this month


o Array of objects of Date class (# of objects in array should be noDays)


Note that, you can add as many variables as you want to this list. All I need is a Date


class to store the Day’s number and day (M/T..) and a class for Month which contains an


array of Date objects. You are free to add more information if you think is required.


Each Class should have enough constructors. You can create as many constructors as


you want based on your program’s design and based on the way you want to initialize


the objects. You SHOULD NOT depend on the constructor provided by the compiler.


At the minimum, you should have at least a default constructor.


You should allocate memory for Array using Dynamic Memory Allocation (new


operator).


You should de_allocate the memory allocated on Heap (using delete operator)


whenever you are done playing with Month object. (Hint: To make this process simple,


you can make use of Destructors).


All the code should present in some member functions of classes. Remember that


creation of Helper Functions (private functions in each class) and Public Interface


(public functions in each class) will Giúp you in autograph the code that is well_organized.


DO NOT just write C code in just one function of a class.



You can create as many files as you want. Make sure to create a README file which


briefly describes the functioning and design of your program. That will Giúp me


understanding your code better. And, write comments in the code wherever you think is


required.
 

Brady

New Member
sao không ai giup mình vậy.mình đang cần gấp.đây là lập trình hướng đối tương.yêu cầu viêt in ra lịch năm nhưng mình không viết được mong các bạn giup gium mình
 

Các chủ đề có liên quan khác

Top