Stats

Tuesday, 13 May 2014

     MISSIONARIES AND CANNIBAL PROBLEM C++ CODE

#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;

int im = 3, ic = 3, i, j, fm = 0, fc = 0, status = 0, flag = 0, select = 0;
void display(char bpass1, char bpass2)
{
cout << "\n\n\n";
for (int i = 0; i < fm; i++) { cout << " M "; }
for (int i = 0; i < fc; i++) { cout << " C "; }
if (flag == 0)
cout << "     __________WATER___________<B0(" << bpass1 << "," << bpass2 << ")AT>  ";
else
cout << "     <BO(" << bpass1 << "," << bpass2 << ")AT>__________WATER___________  ";
for (int i = 0; i < im; i++) { cout << " M "; }
for (int i = 0; i < ic; i++) { cout << " C "; }
}
int win()
{
if (fc == 3 && fm == 3) { return 0; }
else { return 1; }
}
void solution()
{
while (win())
{
if (flag == 0){
switch (select)
{
case 1:display('C', ' ');
ic++;
break;
case 2:display('C', 'M');
ic++; im++;
break;
}
if (((im - 2) >= ic && (fm + 2) >= fc) || (im - 2) == 0)
{
im = im - 2;
select = 1;
display('M', 'M');
flag = 1;
}
else if ((ic - 2) < im && (fm == 0 || (fc + 2) <= fm) || im == 0)
{
ic = ic - 2;
select = 2;
display('C', 'C');
flag = 1;
}
else if ((ic--) <= (im--) && (fm++) >= (fc++))
{
ic = ic - 1;
im = im - 1;
select = 3;
display('M', 'C');
flag = 1;
}
}
else
{
switch (select)
{
case 1:display('M', 'M');
fm = fm + 2;
break;
case 2:display('C', 'C');
fc = fc + 2;
break;
case 3:display('M', 'C');
fc = fc + 1;
fm = fm + 1;
break;
}
if (win())
{
if (((fc > 1 && fm == 0) || im == 0))
{
fc--;
select = 1;
display('C', ' ');
flag = 0;
}
else if ((ic + 2) > im)
{
fc--; fm--;
select = 2;
display('C', 'M');
flag = 0;
}
}
}
}
}
void main()
{
cout << "MISSIONARIES AND CANNIBAL";
display(' ', ' ');
solution();
display(' ', ' ');
cout << "\n\n";
system("pause");
}

OUTPUT

MISSIONARIES AND CANNIBAL

     __________WATER___________<B0( , )AT>   M  M  M  C  C  C

     __________WATER___________<B0(C,C)AT>   M  M  M  C

     <BO(C,C)AT>__________WATER___________   M  M  M  C

 C      <BO(C, )AT>__________WATER___________   M  M  M  C

 C      __________WATER___________<B0(C, )AT>   M  M  M  C

 C      __________WATER___________<B0(C,C)AT>   M  M  M

 C      <BO(C,C)AT>__________WATER___________   M  M  M

 C  C      <BO(C, )AT>__________WATER___________   M  M  M

 C  C      __________WATER___________<B0(C, )AT>   M  M  M

 C  C      __________WATER___________<B0(M,M)AT>   M  C

 C  C      <BO(M,M)AT>__________WATER___________   M  C

 M  C      <BO(C,M)AT>__________WATER___________   M  C

 M  C      __________WATER___________<B0(C,M)AT>   M  C

 M  C      __________WATER___________<B0(M,M)AT>   C  C

 M  C      <BO(M,M)AT>__________WATER___________   C  C

 M  M  M      <BO(C, )AT>__________WATER___________   C  C

 M  M  M      __________WATER___________<B0(C, )AT>   C  C

 M  M  M      __________WATER___________<B0(C,C)AT>   C

 M  M  M      <BO(C,C)AT>__________WATER___________   C

 M  M  M  C      <BO(C, )AT>__________WATER___________   C

 M  M  M  C      __________WATER___________<B0(C, )AT>   C

 M  M  M  C      __________WATER___________<B0(C,C)AT>

 M  M  M  C      <BO(C,C)AT>__________WATER___________

 M  M  M  C  C  C      <BO( , )AT>__________WATER___________



3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. why do you use conio.h? its not neccessary ? also why main is void? main shouldnt be void...

    ReplyDelete
  3. Can you just explain me the meaning of of,ic,im,fm,fc,flag?

    ReplyDelete