英文:
Missionaries and cannibals problem In C++
问题
I've been searching for Missionaries and cannibals problem solving.
After lots of searching, I finally reached the code below, but it shows some errors while compiling.
I changed #include <iostream.h>
to #include<iostream>
and added using namespace std;
but it still shows some error while compiling:
>[Error] '::main' must return 'int'
In function 'int main()'
[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
[Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]
[Note] (if you use '-fpermissive' G++ will accept your code)
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void rectangel(int x1, int y1, int x2, int y2)
{
line(x1, y1, x2, y1);
line(x1, y1, x1, y2);
line(x1, y2, x2, y2);
line(x2, y1, x2, y2);
}
struct node
{
int state[3];
int action[2];
int depth;
int veiw;
} root;
struct node empty = {0, 0, 0, 0, 0, 0};
struct node stack[50];
int top = 0;
void clear()
{
for (int j = 0; j <= 50; j++)
stack[top] = empty;
}
void push(struct node t)
{
stack[top] = t;
top++;
}
struct node pop()
{
top--;
return stack[top];
}
int emptyf()
{
if (top == 0)
return 1;
else
return 0;
}
void succesor(struct node n)
{
struct node k;
if (n.state[2] == 1)
{
k.depth = n.depth + 1;
k.state[0] = n.state[0] - 1;
k.state[1] = n.state[1];
k.state[2] = 0;
k.action[0] = -1;
k.action[1] = 0;
k.veiw = 0;
push(k);
// Add more successor states here...
}
if (n.state[2] == 0)
{
// Add successor states for when n.state[2] == 0 here...
}
}
int goal = 0;
struct node IDS(int l)
{
struct node g;
while (emptyf() != 1 && goal != 1)
{
g = pop();
if (g.depth != 0 && g.state[0] == 3 && g.state[1] == 3 && g.state[2] == 1)
continue;
if ((g.state[0] == 0 && g.state[1] == 2 && g.state[2] == 0) ||
(g.state[0] == 1 && g.state[1] == 2 && g.state[2] == 0) ||
(g.state[0] == 0 && g.state[1] == 1 && g.state[2] == 0) ||
(g.state[0] == 1 && g.state[1] == 2 && g.state[2] == 1))
continue;
if (g.state[0] < 0 || g.state[1] < 0 || g.state[0] > 3 || g.state[1] > 3)
continue;
if (g.state[0] > g.state[1] && g.state[1] != 0)
continue;
if (g.state[0] == 0 && g.state[1] == 0 && g.state[2] == 0)
{
goal = 1;
struct node mmm = g;
return mmm;
}
if (g.depth < l && g.veiw == 0)
{
g.veiw = 1;
push(g);
succesor(g);
}
}
return g;
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "d:\\tc\\bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
cout << "Graphics error: %s\n" << grapherrormsg(errorcode) << endl;
cout << "Press any key to halt:";
getch();
exit(1);
}
root.state[0] = 3;
root.state[1] = 3;
root.state[2] = 1;
root.action[0] = 0;
root.action[1] = 0;
root.depth = 0;
root.veiw = 0;
int d = 0;
struct node km;
while (goal != 1)
{
clear();
push(root);
km = IDS(d);
d++;
}
int array[40];
int dd = km.depth;
int index = 0;
array[index] = km.state[2];
index++;
array[index] = km.state[1];
index++;
array[index] = km.state[0];
cout << "depth=" << d << endl;
while (!(km.state[0] == 3 && km.state[1] == 3 && km.state[2] == 1))
{
km = pop();
if (km.depth == dd)
continue;
if (km.depth == dd - 1)
{
index++;
array[index] = km.state[2];
index++;
array[index] = km.state[1];
index++;
array[index] = km.state[0];
dd = km.depth;
}
}
int count = 0;
int tmparay[40];
for (int i = 0; i < index + 1; i++)
tmparay[i] = array[index - i];
count = 0;
int aray[12][3];
for (int i = 0; i < 12; i++)
{
aray[i][0] = tmparay[count];
count++;
aray[i][1] = tmparay[count];
count++;
aray[i][2] = tmparay[count];
count++;
}
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 3; j++)
cout << " " << aray[i][j];
cout << endl;
}
cout << "Missionaries" << endl << endl << endl << endl << endl;
<details>
<summary>英文:</summary>
I've been searching for Missionaries and cannibals problem solving.
After lots of searching, I finally reached the code below, but it shows some errors while compiling.
I changed `#include <iostream.h>` to `#include<iostream>` and added `using namespace std;`
but it still shows some error while compiling:
>[Error] '::main' must return 'int'
In function 'int main()'
[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]
[Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]
[Note] (if you use '-fpermissive' G++ will accept your code)
```#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void rectangel(int x1,int y1,int x2,int y2)
{
line(x1,y1,x2,y1);
line(x1,y1,x1,y2);
line(x1,y2,x2,y2);
line(x2,y1,x2,y2);
}
struct node{
int state[3];
int action[2];
int depth;
int veiw;
}root;
struct node empty={ 0,0,0,
0,0,
0,
0
};
struct node stack[50];
int top=0;
void clear()
{
for(int j=0;j<=50;j++)
stack[top]=empty;
}
void push(struct node t)
{
stack[top]=t;
top++;
}
struct node pop()
{
top--;
return stack[top];
}
int emptyf()
{
if(top==0)
return 1;
else return 0;
}
void succesor(struct node n)
{
struct node k;
if(n.state[2]==1)
{
k.depth=n.depth+1;
k.state[0]=n.state[0]-1;
k.state[1]=n.state[1];
k.state[2]=0;
k.action[0]=-1;
k.action[1]=0;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0]-2;
k.state[1]=n.state[1];
k.state[2]=0;
k.action[0]=-2;
k.action[1]=0;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0];
k.state[1]=n.state[1]-1;
k.state[2]=0;
k.action[0]=0;
k.action[1]=-1;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0];
k.state[1]=n.state[1]-2;
k.state[2]=0;
k.action[0]=0;
k.action[1]=-2;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0]-1;
k.state[1]=n.state[1]-1;
k.state[2]=0;
k.action[0]=-1;
k.action[1]=-1;
k.veiw=0;
push(k);
}
if(n.state[2]==0)
{
k.depth=n.depth+1;
k.state[0]=n.state[0]+1;
k.state[1]=n.state[1];
k.state[2]=1;
k.action[0]=1;
k.action[1]=0;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0];
k.state[1]=n.state[1]+1;
k.state[2]=1;
k.action[0]=0;
k.action[1]=1;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0]+2;
k.state[1]=n.state[1];
k.state[2]=1;
k.action[0]=2;
k.action[1]=0;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0]+1;
k.state[1]=n.state[1]+1;
k.state[2]=1;
k.action[0]=1;
k.action[1]=1;
k.veiw=0;
push(k);
k.depth=n.depth+1;
k.state[0]=n.state[0];
k.state[1]=n.state[1]+2;
k.state[2]=1;
k.action[0]=0;
k.action[1]=2;
k.veiw=0;
push(k);
}
}
int goal=0;
struct node IDS(int l)
{
struct node g;
while(emptyf()!=1 && goal!=1)
{
g=pop();
if(g.depth!=0 && g.state[0]==3 && g.state[1]==3 && g.state[2]==1)
continue;
if ((g.state[0]==0 && g.state[1]==2 && g.state[2]==0) ||
(g.state[0]==1 && g.state[1]==2 && g.state[2]==0) ||
(g.state[0]==0 && g.state[1]==1 && g.state[2]==0) ||
(g.state[0]==1 && g.state[1]==2 && g.state[2]==1))
continue;
if(g.state[0]<0 || g.state[1]<0 || g.state[0]>3 || g.state[1]>3)
continue;
if(g.state[0]>g.state[1] && g.state[1]!=0)
continue;
if(g.state[0]==0 && g.state[1]==0 && g.state[2]==0)
{
goal=1;
struct node mmm=g;
return mmm;
}
if(g.depth<l && g.veiw==0)
{
g.veiw=1;
push(g);
succesor(g);
}
}
return g;
}
void main()
{
//clrscr();
int gdriver = DETECT , gmode , errorcode;
initgraph(&gdriver, &gmode, "d:\\tc\\bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
cout<<"Graphics error: %s\n"<< grapherrormsg(errorcode)<<endl;
cout<<"Press any key to halt:";
getch();
exit(1);
}
root.state[0]=3;
root.state[1]=3;
root.state[2]=1;
root.action[0]=0;
root.action[1]=0;
root.depth=0;
root.veiw=0;
int d=0;
struct node km;
while(goal!=1)
{
clear();
push(root);
km=IDS(d);
d++;
}
int array[40];
int dd=km.depth;
int index=0;
array[index]=km.state[2];
index++;
array[index]=km.state[1];
index++;
array[index]=km.state[0];
cout<<"depth="<<d<<endl;
while(!(km.state[0]==3 && km.state[1]==3 && km.state[2]==1))
{
km=pop();
if(km.depth==dd)
continue;
if(km.depth==dd-1)
{
index++;
array[index]=km.state[2];
index++;
array[index]=km.state[1];
index++;
array[index]=km.state[0];
dd=km.depth;
}
}
int count=0;
int tmparay[40];
for(int i=0;i<index+1;i++)
tmparay[i]=array[index-i];
count=0;
int aray[12][3];
for(i=0;i<12;i++)
{
aray[i][0]=tmparay[count];
count++;
aray[i][1]=tmparay[count];
count++;
aray[i][2]=tmparay[count];
count++;
}
for(i=0;i<12;i++)
{
for(int j=0;j<3;j++)
cout<<" "<<aray[i][j];
cout<<endl;
}
cout<<" Missionaries"<<endl<<endl<<endl<<endl<<endl;
cout<<endl<<endl<<endl<<endl<<endl;
cout<<" Cannibals";
rectangel(150,200,250,400);
for(i=0;i<12;i++)
{
if(aray[i][2]==1)
{
if((aray[i][0]==3) && (aray[i][1]==3))
{
setcolor(0);
rectangel(220,280,250,330);
setcolor(7);
rectangel(150,200,250,400);
rectangel(150,280,180,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
}
else if((aray[i][0]==3) && (aray[i][1]==0))
{
setcolor(0);
rectangel(220,280,250,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(150,280,180,330);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
}
else if((aray[i][0]==2) && (aray[i][1]==3))
{
setcolor(0);
rectangel(220,280,250,330);
rectangel(120,370,140,390);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(150,280,180,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(260,370,280,390);
}
else if((aray[i][0]==2) && (aray[i][1]==2))
{
setcolor(0);
rectangel(290,370,310,390);
rectangel(220,280,250,330);
rectangel(320,370,340,390);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(120,370,140,390);
rectangel(120,210,140,230);
setcolor(7);
rectangel(150,200,250,400);
rectangel(150,280,180,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(260,210,280,230);
rectangel(260,370,280,390);
}
else if((aray[i][0]==2) && (aray[i][1]==0))
{
setcolor(0);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
rectangel(290,370,310,390);
rectangel(120,370,140,390);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
setcolor(7);
rectangel(150,200,250,400);
rectangel(150,280,180,330);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(260,370,280,390);
}
else if((aray[i][0]==1) && (aray[i][1]==3))
{
setcolor(0);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
rectangel(220,280,250,330);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(320,370,340,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(60,370,80,390);
rectangel(150,280,180,330);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
}
else if((aray[i][0]==1) && (aray[i][1]==1))
{
setcolor(0);
rectangel(260,210,280,230);
rectangel(260,370,280,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
rectangel(220,280,250,330);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
setcolor(7);
rectangel(150,200,250,400);
rectangel(150,280,180,330);
rectangel(60,210,80,230);
rectangel(60,370,80,390);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
}
else if((aray[i][0]==1) && (aray[i][1]==0))
{
setcolor(0);
rectangel(220,280,250,330);
rectangel(320,370,340,390);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(60,370,80,390);
rectangel(150,280,180,330);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
}
}
if(aray[i][2]==0)
{
if((aray[i][0]==3) && (aray[i][1]==3))
{
setcolor(0);
rectangel(150,280,180,330);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
}
else if((aray[i][0]==3) && (aray[i][1]==0))
{
setcolor(0);
rectangel(150,280,180,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
}
else if((aray[i][0]==2) && (aray[i][1]==3))
{
setcolor(0);
rectangel(150,280,180,330);
rectangel(120,370,140,390);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(260,370,280,390);
}
else if((aray[i][0]==2) && (aray[i][1]==2))
{
setcolor(0);
rectangel(290,370,310,390);
rectangel(150,280,180,330);
rectangel(320,370,340,390);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(120,370,140,390);
rectangel(120,210,140,230);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(260,210,280,230);
rectangel(260,370,280,390);
}
else if((aray[i][0]==2) && (aray[i][1]==0))
{
setcolor(0);
rectangel(150,280,180,330);
rectangel(320,370,340,390);
rectangel(290,370,310,390);
rectangel(120,370,140,390);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(260,370,280,390);
}
else if((aray[i][0]==1) && (aray[i][1]==3))
{
setcolor(0);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
rectangel(150,280,180,330);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(320,370,340,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(60,370,80,390);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
rectangel(220,280,250,330);
}
else if((aray[i][0]==1) && (aray[i][1]==1))
{
setcolor(0);
rectangel(150,280,180,330);
rectangel(260,210,280,230);
rectangel(260,370,280,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(60,210,80,230);
rectangel(60,370,80,390);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
}
else if((aray[i][0]==1) && (aray[i][1]==0))
{
setcolor(0);
rectangel(150,280,180,330);
rectangel(320,370,340,390);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(60,370,80,390);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
}
else if((aray[i][0]==0) && (aray[i][1]==3))
{
setcolor(0);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(150,280,180,330);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
setcolor(7);
rectangel(150,200,250,400);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(220,280,250,330);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
}
else if((aray[i][0]==0) && (aray[i][1]==0))
{
setcolor(0);
rectangel(60,210,80,230);
rectangel(90,210,110,230);
rectangel(120,210,140,230);
rectangel(60,370,80,390);
rectangel(90,370,110,390);
rectangel(120,370,140,390);
rectangel(150,280,180,330);
setcolor(7);
rectangel(150,200,250,400);
rectangel(220,280,250,330);
rectangel(260,210,280,230);
rectangel(290,210,310,230);
rectangel(320,210,340,230);
rectangel(260,370,280,390);
rectangel(290,370,310,390);
rectangel(320,370,340,390);
}
}
getch();
}
getch();
}
答案1
得分: 4
编译器报告的第一个错误是:"::main 必须返回一个整数"
如果你将主函数更改为
int main(void) {
...
你的代码
...
return 0;
}
错误应该消失。
第二个错误:
你在 for 循环中声明了 "i"。在 for 循环之后,在 C++ 中不再可用。
这段代码将无法编译:
for(int i = 0; i < 5; ++i) {
...
}
for(i = 0; i < 5; ++i) { // 错误,i 变量在此处不存在
...
}
这个可以编译:
for(int i = 0; i < 5; ++i) {
...
}
for(int i = 0; i < 5; ++i) {
...
}
英文:
The first error reported by the compiler is : "::main must return an int"
if you change your main function to
int main(void) {
...
your code
...
return 0;
}
the error should go away.
Second error:
you declared "i" in the for cycle. After the for cycle it is no more available in c++.
this code will not compile:
for(int i = 0; i <5;++i) {
...
}
for(i = 0; i <5;++i) { // error i variable doesn't exist here
...
}
this one will compile:
for(int i = 0; i <5;++i) {
...
}
for(int i = 0; i <5;++i) {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论