농부 의 강 건 너 기 문제 가 실현 되다.
#include <iostream>
using namespace std;
struct Condition
{
int farmer;
int wolf;
int sheep;
int cabbage;
};
//
Condition conditions[100];
char* action[100];
//
void takeWolfOver(int i)
{
action[i]="take wolf over.";
conditions[i+1].wolf=1;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void takeWolfBack(int i)
{
action[i]="take wolf back.";
conditions[i+1].wolf=0;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void takeSheepOver(int i)
{
action[i]="take sheep over.";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=1;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void takeSheepBack(int i)
{
action[i]="take sheep back.";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=0;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void takeCabbageOver(int i)
{
action[i]="take cabbage over.";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=1;
}
void takeCabbageBack(int i)
{
action[i]="take cabbage back.";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=0;
}
//
void getOverBarely(int i)
{
action[i]="get over barely.";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void getBackBarely(int i)
{
action[i]="get back barely.";
conditions[i+1].wolf=conditions[i].wolf;
conditions[i+1].sheep=conditions[i].sheep;
conditions[i+1].cabbage=conditions[i].cabbage;
}
void showResult(int i)
{
int c;
for(c=0;c<i;c++)
{
cout<<c+1<<"."<<action[c]<<endl;
}
cout<<"Nice job!"<<endl;
}
void tryOneStep(int i)
{
int c,j;
//
if(conditions[i].farmer==1&&
conditions[i].wolf==1&&
conditions[i].sheep==1&&
conditions[i].cabbage==1)
{
showResult(i);
return;
}
//
if(conditions[i].farmer!=conditions[i].wolf&&conditions[i].wolf==conditions[i].sheep||
conditions[i].farmer!=conditions[i].sheep&&conditions[i].sheep==conditions[i].cabbage)
{
return;
}
//
for(c=0;c<i;c++)
{
if(conditions[c].farmer==conditions[i].farmer&&
conditions[c].wolf==conditions[i].wolf&&
conditions[c].sheep==conditions[i].sheep&&
conditions[c].cabbage==conditions[i].cabbage)
{
return;
}
}
j=i+1;
if(conditions[i].farmer==0)
{
conditions[j].farmer=1;
getOverBarely(i);
tryOneStep(j);
if(conditions[i].wolf==0)
{
takeWolfOver(i);
tryOneStep(j);
}
if(conditions[i].sheep==0)
{
takeSheepOver(i);
tryOneStep(j);
}
if(conditions[i].cabbage==0)
{
takeCabbageOver(i);
tryOneStep(j);
}
}else
{
conditions[j].farmer=0;
getBackBarely(i);
tryOneStep(j);
if(conditions[i].wolf==1)
{
takeWolfBack(i);
tryOneStep(j);
}
if(conditions[i].sheep==1)
{
takeSheepBack(i);
tryOneStep(j);
}
if(conditions[i].cabbage==1)
{
takeCabbageBack(i);
tryOneStep(j);
}
}
}
int main()
{
conditions[0].farmer=0;
conditions[0].wolf=0;
conditions[0].sheep=0;
conditions[0].cabbage=0;
tryOneStep(0);
getchar();
return 0;
}
1. 。 、 、 、 A、B、C、D。 ABCD ,
* 。 、 , , .
*
* 2. 。 , ABCD ( ):0000,
* :1111。 0000 1111
* 。 , 16 。 ,
* , 。 0000 (
* ) , 1111 。
*
* 3. 。 , ABCD ( ):0000,
* :1111。 0000 1111
* 。 , 16 。 0000 ,
* , , 。
* , 。 1111 。
*
* 4. 。 3 , 。 3
* 。
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Docker를 사용한 React 및 .NET Core 6.0 샘플 프로젝트 - 1부이 기사에서는 Entity Framework Core Code First 접근 방식을 사용하는 ASP.NET Core 6.0 WEP API의 CRUD(만들기, 읽기, 업데이트 및 삭제) 작업에 대해 설명합니다. 웹 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.