陽(yáng)光體育的名言
編程題、傳教士人數M,野人C,M≥C,開(kāi)始都在岸左邊,

、俅荒茌d兩人,傳教士和野人都會(huì )劃船,當然必須有人劃船
、趦砂哆叡WC野人人數不能大于傳教士人數
把所有人都送過(guò)河,設計一方案,要求編程實(shí)現。
思路:
深度搜索。
狀態(tài):左岸和右岸的人數+船的位置。
每一個(gè)狀態(tài)下,會(huì )有5種狀態(tài)可以轉移,
即:
1,運送2個(gè)傳教士到對岸;
2,運送2個(gè)野人到對岸;
3,運送1個(gè)傳教士到對岸;
4,運送1個(gè)野人到對岸;
5,運送1個(gè)傳教士和一個(gè)野人到對岸。
從初始狀態(tài)開(kāi)始搜,搜索這五種情況,
進(jìn)入下一狀態(tài),判斷該狀態(tài)是否滿(mǎn)足條件,
即兩岸野人的個(gè)數是否比該岸的傳教士多,
如果滿(mǎn)足條件,則繼續搜索該狀態(tài)下的五種情況。
深度搜索下去,直到找到最后的解。
注意:
1,如果搜索的狀態(tài)在之前已經(jīng)出現過(guò)了,就不深入下去了,
否則會(huì )出現死循環(huán),比如運兩個(gè)野人過(guò)去,再運回來(lái),狀態(tài)復原了,
如果一直這么搜下去,就沒(méi)玩沒(méi)了了。
2,狀態(tài)包括船的信息,如果兩邊的人數都是一樣,但是船的位置不一樣,
那么這是兩種狀態(tài)。
3,要搜索的目標狀態(tài)是人都在對岸且船在對岸。
PS:
當M=C>3時(shí),沒(méi)有解。
當M>C時(shí),有解。
[cpp] view plaincopyprint?
#include
#include
#include
#include
using namespace std;
bool flag = true; /pic/p>
vector
bool dfs( int M, int C, int m, int c){
if( M<0||C<0||m<0||c<0) /pic/p>
return false;
if( (M&&C>M) ||(m&&c>m)) /pic/p>
return false;
if( flag&&M==0&&C==0 ||(!flag&&m==0&&c==0)) /pic/p>
return true;
/pic/p>
char s[30];
if( !flag )
sprintf( s, "M=%d,C=%d,m=%d,c=%d,boat=left", M,C,m,c);
else
sprintf( s, "M=%d,C=%d,m=%d,c=%d,boat=right", m,c,M,C);
string str(s);
for( int i=0; i
if( visit[i]==str) /pic/p>
return false;
visit.push_back(str);
flag = !flag;
if( dfs( m+2, c, M-2,C) ){
printf("2,0\n");
printf("%s\n",s);
return true;
}
else if( dfs( m, c+2, M, C-2) ){
printf("0,2\n");
printf("%s\n",s);
return true;
}
else if( dfs( m+1, c+1, M-1, C-1) ){
printf("1,1\n");
printf("%s\n",s);
return true;
}
else if( dfs( m+1, c, M-1, C)){
printf("1,0\n");
printf("%s\n",s);
return true;
}
else if( dfs( m, c+1, M, C-1)){
printf("0,1\n");
printf("%s\n",s);
return true;
}
flag = !flag;
visit.pop_back();
return false;
}
int main(){
char s[30];
int M=6,C=6,m=0,c=0;
sprintf( s, "M=%d,C=%d,m=%d,c=%d,boat=left", M,C,m,c);
printf("%s\n",s);
if(!dfs(M,C,0,0))
cout << "Can not find the solution."<
return 0;
}
#include
#include
#include
#include
using namespace std;
bool flag = true; /pic/p>
vector
bool dfs( int M, int C, int m, int c){
if( M<0||C<0||m<0||c<0) /pic/p>
return false;
if( (M&&C>M) ||(m&&c>m)) /pic/p>
return false;
if( flag&&M==0&&C==0 ||(!flag&&m==0&&c==0)) /pic/p>
return true;
/pic/p>
char s[30];
if( !flag )
sprintf( s, "M=%d,C=%d,m=%d,c=%d,boat=left", M,C,m,c);
else
sprintf( s, "M=%d,C=%d,m=%d,c=%d,boat=right", m,c,M,C);
string str(s);
for( int i=0; i
if( visit[i]==str) /pic/p>
return false;
visit.push_back(str);
flag = !flag;
if( dfs( m+2, c, M-2,C) ){
printf("2,0\n");
printf("%s\n",s);
return true;
}
else if( dfs( m, c+2, M, C-2) ){
printf("0,2\n");
printf("%s\n",s);
return true;
}
else if( dfs( m+1, c+1, M-1, C-1) ){
printf("1,1\n");
printf("%s\n",s);
return true;
}
else if( dfs( m+1, c, M-1, C)){
printf("1,0\n");
printf("%s\n",s);
return true;
}
else if( dfs( m, c+1, M, C-1)){
printf("0,1\n");
printf("%s\n",s);
return true;
}
flag = !flag;
visit.pop_back();
return false;
}
int main(){
char s[30];
int M=6,C=6,m=0,c=0;
sprintf( s, "M=%d,C=%d,m=%d,c=%d,boat=left", M,C,m,c);
printf("%s\n",s);
if(!dfs(M,C,0,0))
cout << "Can not find the solution."<
return 0;
}
【陽(yáng)光體育的名言】相關(guān)文章:
陽(yáng)光體育作文(通用)03-08
有關(guān)陽(yáng)光體育作文05-07
陽(yáng)光體育作文(精選33篇)12-26
陽(yáng)光體育的作文(精選18篇)12-30
關(guān)于體育的名言290句02-27
有關(guān)體育的名言100句10-16
陽(yáng)光體育伴我行作文04-16
小學(xué)陽(yáng)光體育大課間活動(dòng)方案10-06
- 相關(guān)推薦