missionaries and cannibals problem solution in c 2b 2b

Solutions on MaxInterview for missionaries and cannibals problem solution in c 2b 2b by the best coders in the world

showing results for - "missionaries and cannibals problem solution in c 2b 2b"
Awa
03 Apr 2020
1 
2// missionaries and cannibals
3#include<iostream>
4#include<iomanip>
5using namespace std;
6class game{
7public:
8int counto, i;
9char left[6], right[6];
10int m_num, c_num;
11bool side;
12int ml_count, cl_count;
13int mr_count, cr_count;
14game(){
15counto = 1;
16ml_count = cl_count = 3;
17mr_count = cr_count = 0;
18side = false;
19for (i = 0; i<3; i++){
20left[i] = 'M';
21left[i + 3] = 'C';
22right[i] = ' ';
23right[i + 3] = ' ';
24}
25}
26void get(){
27start:
28cout << "\nEnter no.of missionaries= ";
29cin >> m_num;
30cout << "\nEnter no.of cannibals= ";
31cin >> c_num;
32if (m_num>3 || c_num>3 || m_num<0 || c_num<0)
33goto start;
34else if ((m_num + c_num)>2 || (m_num + c_num == 0))
35goto start;
36}
37void displaymc(){
38cout << "\nleft side\tright side\n";
39for (i = 0; i<3; i++)
40cout << left[i] << " ";
41cout << "\t\t";
42for (i = 0; i<3; i++)
43cout << right[i] << " ";
44cout << endl;
45for (i = 3; i<6; i++)
46cout << left[i] << " ";
47cout << "\t\t";
48for (i = 3; i<6; i++)
49cout << right[i] << " ";
50cout << endl;
51if (counto % 2 == 0){
52side = true;
53cout << "\nBoat on right side of river\n";
54}
55else{
56side = false;
57cout << "\nBoat on left side of river\n";
58}
59}
60void boat_lr(){
61for (i = 0; i<m_num; i++){
62if (left[0] == 'M'){
63left[0] = ' ';
64right[0] = 'M';
65ml_count -= 1;
66mr_count += 1;
67}
68else if (left[1] == 'M'){
69left[1] = ' ';
70right[1] = 'M';
71ml_count -= 1;
72mr_count += 1;
73}
74else if (left[2] == 'M'){
75left[2] = ' ';
76right[2] = 'M';
77ml_count -= 1;
78mr_count += 1;
79}
80}
81for (i = 0; i<c_num; i++){
82if (left[3] == 'C'){
83left[3] = ' ';
84right[3] = 'C';
85cl_count -= 1;
86cr_count += 1;
87}
88else if (left[4] == 'C'){
89left[4] = ' ';
90right[4] = 'C';
91cl_count -= 1;
92cr_count += 1;
93}
94else if (left[5] == 'C'){
95left[5] = ' ';
96right[5] = 'C';
97cl_count -= 1;
98cr_count += 1;
99}
100}
101}
102void boat_rl(){
103for (i = 0; i<m_num; i++){
104if (right[0] == 'M'){
105right[0] = ' ';
106left[0] = 'M';
107ml_count += 1;
108mr_count -= 1;
109}
110else if (right[1] == 'M'){
111right[1] = ' ';
112left[1] = 'M';
113ml_count += 1;
114mr_count -= 1;
115}
116else if (right[2] == 'M'){
117right[2] = ' ';
118left[2] = 'M';
119ml_count += 1;
120mr_count -= 1;
121}
122}
123for (i = 0; i<c_num; i++){
124if (right[3] == 'C'){
125right[3] = ' ';
126left[3] = 'C';
127cl_count += 1;
128cr_count -= 1;
129}
130else if (right[4] == 'C'){
131right[4] = ' ';
132left[4] = 'C';
133cl_count += 1;
134cr_count -= 1;
135}
136else if (right[5] == 'C'){
137right[5] = ' ';
138left[5] = 'C';
139cl_count += 1;
140cr_count -= 1;
141}
142}
143}
144};
145int main(){
146game g;
147while (true){
148if (g.ml_count<g.cl_count && g.ml_count>0 || g.mr_count<g.cr_count && g.mr_count>0){
149cout << "\n\n~~~~~~~~~~~~~~~~YOU LOST!!~~~~~~~~~~~~~~~~\n";
150break;
151}
152else if (g.cr_count == g.mr_count && g.cr_count == 3 && g.mr_count == 3){
153cout << "\n\n~~~~~~~~~~~~~~~~YOU WON!!~~~~~~~~~~~~~~~~\n";
154break;
155}
156else{
157g.displaymc();
158g.get();
159if (g.side == false)
160g.boat_lr();
161else
162g.boat_rl();
163}
164g.counto++;
165}
166return 0;
167}
168