농부 의 강 건 너 기 문제 가 실현 되다.

4224 단어 cactioninclude
#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            
 *                         。

좋은 웹페이지 즐겨찾기