传教士和食人族问题在C++中

huangapple go评论82阅读模式
英文:

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&#39;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 &lt;iostream.h&gt;` to `#include&lt;iostream&gt;` and added `using namespace std;`
but it still shows some error while compiling:

&gt;[Error] &#39;::main&#39; must return &#39;int&#39;
In function &#39;int main()&#39;
[Warning] deprecated conversion from string constant to &#39;char*&#39; [-Wwrite-strings]
[Error] name lookup of &#39;i&#39; changed for ISO &#39;for&#39; scoping [-fpermissive]
[Note] (if you use &#39;-fpermissive&#39; G++ will accept your code)


```#include&lt;iostream.h&gt;
#include&lt;stdlib.h&gt;
#include&lt;conio.h&gt;
#include&lt;graphics.h&gt;
#include&lt;dos.h&gt;
     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&lt;=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 &amp;&amp; goal!=1)
   {
    g=pop();
   if(g.depth!=0 &amp;&amp; g.state[0]==3 &amp;&amp; g.state[1]==3 &amp;&amp; g.state[2]==1)
    continue;
     if ((g.state[0]==0 &amp;&amp; g.state[1]==2 &amp;&amp; g.state[2]==0) ||
	 (g.state[0]==1 &amp;&amp; g.state[1]==2 &amp;&amp; g.state[2]==0) ||
	 (g.state[0]==0 &amp;&amp; g.state[1]==1 &amp;&amp; g.state[2]==0) ||
	 (g.state[0]==1 &amp;&amp; g.state[1]==2 &amp;&amp; g.state[2]==1))
    continue;
     if(g.state[0]&lt;0 || g.state[1]&lt;0 || g.state[0]&gt;3 || g.state[1]&gt;3)
     continue;
     if(g.state[0]&gt;g.state[1] &amp;&amp; g.state[1]!=0)
     continue;
    if(g.state[0]==0 &amp;&amp; g.state[1]==0 &amp;&amp; g.state[2]==0)
     {
      goal=1;
      struct node mmm=g;
      return mmm;
      }
    if(g.depth&lt;l &amp;&amp; g.veiw==0)
     {
      g.veiw=1;
      push(g);
      succesor(g);
     }
   }
   return g;
  }
   void main()
  {
    //clrscr();
	   int gdriver = DETECT , gmode , errorcode;
       initgraph(&amp;gdriver, &amp;gmode, &quot;d:\\tc\\bgi&quot;);
       errorcode = graphresult();
       if (errorcode != grOk)
	 {
	   cout&lt;&lt;&quot;Graphics error: %s\n&quot;&lt;&lt; grapherrormsg(errorcode)&lt;&lt;endl;
	   cout&lt;&lt;&quot;Press any key to halt:&quot;;
	   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&lt;&lt;&quot;depth=&quot;&lt;&lt;d&lt;&lt;endl;
    while(!(km.state[0]==3 &amp;&amp; km.state[1]==3 &amp;&amp; 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&lt;index+1;i++)
    tmparay[i]=array[index-i];
      count=0;
       int aray[12][3];
       for(i=0;i&lt;12;i++)
       {
	aray[i][0]=tmparay[count];
	count++;
	aray[i][1]=tmparay[count];
	count++;
	aray[i][2]=tmparay[count];
	count++;
       }
       for(i=0;i&lt;12;i++)
	{
	for(int j=0;j&lt;3;j++)
	 cout&lt;&lt;&quot; &quot;&lt;&lt;aray[i][j];
	 cout&lt;&lt;endl;
	 }
	 cout&lt;&lt;&quot;                                            Missionaries&quot;&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl;
	 cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl&lt;&lt;endl;
	 cout&lt;&lt;&quot;                                            Cannibals&quot;;
      rectangel(150,200,250,400);
      for(i=0;i&lt;12;i++)
       {
	 if(aray[i][2]==1)
	 {
	   if((aray[i][0]==3) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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) &amp;&amp; (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();
}

传教士和食人族问题在C++中

答案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 &lt;5;++i) {
...
}
for(i = 0; i &lt;5;++i) { // error i variable doesn&#39;t exist here
...
}

this one will compile:

for(int i = 0; i &lt;5;++i) {
...
}
for(int i = 0; i &lt;5;++i) {
...
}

huangapple
  • 本文由 发表于 2020年1月3日 19:03:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定