狮子过河问题
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
for ( j = 0; j <= 6; j++ )
if ( a[curr][j]!=1 ) break;
if ( j > 6 )
found = 1;
}
}
}
}
if ( found )
{
printf( "\n" );
for ( i = 0; i <= curr; i++ )
{
printf( "%2d: ", i+1 );
}
printf( "\n" );
}
printf( "\n" );
}
else
printf( "Not Found\n" );
}
1: A a B b C c || >> A a
2: B b C c || A a << A
3: A B b C c || a >> b
4: A B C c || a b << a
curr = 0;
found = 0;
while ( !found )
{
if ( a[curr][8] == 5 )
{
a[curr][7] ++;
a[curr][8] = a[curr][7];
}
else
a[curr][8] ++;
if ( a[curr][7] > 5 )
{
curr --;
if ( curr < 0 )
for ( j=0; j < 6; j++ )
if ( a[i][j]==0 )
printf( "%c ", lion[j] );
else
printf( " " );
printf( "|| " );
for ( j=0; j < 6; j++ )
if ( a[i][j]==1 )
printf( "%c ", lion[j] );
a[tc][t1] = !a[curr][t1];
a[tc][t2] = !a[curr][t2];
a[tc][6] = !a[curr][6];
a[tc][7] = 0;
a[tc][8] = -1;
safe = 1;
if ( a[tc][0]!=a[tc][1] )
if ( (a[tc][1]==a[tc][2]) || (a[tc][1]==a[tc][4]) )
safe = 0;
his = 0;
for ( i = 0; i < curr; i+lt;= 6; j++ )
if ( a[i][j]!=a[tc][j] ) break;
if ( j > 6 )
his = 1;
}
if ( safe && !his )
{
curr = tc;
12: C c || A a B b << A a
13: A a C c || B b >> A C
14: a c || A B b C << b
15: a b c || A B C >> a b
16: c || A a B b C << a
17: a c || A B b C >> a c
18: || A a B b C c <<
break;
}
else
{
t1 = a[curr][7];
t2 = a[curr][8];
if ( (a[curr][t1]==a[curr][6]) && (a[curr][t2]==a[curr][6]) )
{
tc = curr + 1;
for ( i = 0; i <= 5; i++ )
a[tc][i] = a[curr][i];
5: A a B C c || b >> a c
6: A B C || a b c << a
7: A a B C || b c >> B C
8: A a || B b C c << B b
9: A a B b || C c >> A a
10: B b || A a C c << C c
11: B b C c || A a >> B b
else
printf( " " );
if ( a[i][6] == 0 )
printf( ">> " );
else
printf( "<< " );
if ( a[i][8]!=-1 )
{
printf( "%c ", lion[a[i][7]] );
if ( a[i][8]!=a[i][7] )
printf( "%c ", lion[a[i][8]] );
safe = 0;
if ( a[tc][2]!=a[tc][3] )
if ( (a[tc][3]==a[tc][0]) || (a[tc][3]==a[tc][4]) )
safe = 0;
if ( a[tc][4]!=a[tc][5] )
if ( (a[tc][5]==a[tc][0]) || (a[tc][5]==a[tc][2]) )
int i, j, curr, t1, t2, tc, safe, his, found;
char lion[] = { 'A', 'a', 'B', 'b', 'C', 'c' };
for ( j = 0; j <= 6; j++ )
a[0][j] = 0;
a[0][7] = 0;
a[0][8] = -1;
有三对母子狮子Aa、Bb与Cc,其中ABC为母狮,abc为小狮。要过一条河,河边只有一条船,狮子自己划船,船每次只能载两只狮子。
生存准则:一只小狮如果没有母亲的保护,则会被其它母狮吃掉。母狮之间、小狮之间是安全的。
问题:如何安全过河?
#include <stdio.h>
main()
{
int a[128][9];
if ( a[curr][j]!=1 ) break;
if ( j > 6 )
found = 1;
}
}
}
}
if ( found )
{
printf( "\n" );
for ( i = 0; i <= curr; i++ )
{
printf( "%2d: ", i+1 );
}
printf( "\n" );
}
printf( "\n" );
}
else
printf( "Not Found\n" );
}
1: A a B b C c || >> A a
2: B b C c || A a << A
3: A B b C c || a >> b
4: A B C c || a b << a
curr = 0;
found = 0;
while ( !found )
{
if ( a[curr][8] == 5 )
{
a[curr][7] ++;
a[curr][8] = a[curr][7];
}
else
a[curr][8] ++;
if ( a[curr][7] > 5 )
{
curr --;
if ( curr < 0 )
for ( j=0; j < 6; j++ )
if ( a[i][j]==0 )
printf( "%c ", lion[j] );
else
printf( " " );
printf( "|| " );
for ( j=0; j < 6; j++ )
if ( a[i][j]==1 )
printf( "%c ", lion[j] );
a[tc][t1] = !a[curr][t1];
a[tc][t2] = !a[curr][t2];
a[tc][6] = !a[curr][6];
a[tc][7] = 0;
a[tc][8] = -1;
safe = 1;
if ( a[tc][0]!=a[tc][1] )
if ( (a[tc][1]==a[tc][2]) || (a[tc][1]==a[tc][4]) )
safe = 0;
his = 0;
for ( i = 0; i < curr; i+lt;= 6; j++ )
if ( a[i][j]!=a[tc][j] ) break;
if ( j > 6 )
his = 1;
}
if ( safe && !his )
{
curr = tc;
12: C c || A a B b << A a
13: A a C c || B b >> A C
14: a c || A B b C << b
15: a b c || A B C >> a b
16: c || A a B b C << a
17: a c || A B b C >> a c
18: || A a B b C c <<
break;
}
else
{
t1 = a[curr][7];
t2 = a[curr][8];
if ( (a[curr][t1]==a[curr][6]) && (a[curr][t2]==a[curr][6]) )
{
tc = curr + 1;
for ( i = 0; i <= 5; i++ )
a[tc][i] = a[curr][i];
5: A a B C c || b >> a c
6: A B C || a b c << a
7: A a B C || b c >> B C
8: A a || B b C c << B b
9: A a B b || C c >> A a
10: B b || A a C c << C c
11: B b C c || A a >> B b
else
printf( " " );
if ( a[i][6] == 0 )
printf( ">> " );
else
printf( "<< " );
if ( a[i][8]!=-1 )
{
printf( "%c ", lion[a[i][7]] );
if ( a[i][8]!=a[i][7] )
printf( "%c ", lion[a[i][8]] );
safe = 0;
if ( a[tc][2]!=a[tc][3] )
if ( (a[tc][3]==a[tc][0]) || (a[tc][3]==a[tc][4]) )
safe = 0;
if ( a[tc][4]!=a[tc][5] )
if ( (a[tc][5]==a[tc][0]) || (a[tc][5]==a[tc][2]) )
int i, j, curr, t1, t2, tc, safe, his, found;
char lion[] = { 'A', 'a', 'B', 'b', 'C', 'c' };
for ( j = 0; j <= 6; j++ )
a[0][j] = 0;
a[0][7] = 0;
a[0][8] = -1;
有三对母子狮子Aa、Bb与Cc,其中ABC为母狮,abc为小狮。要过一条河,河边只有一条船,狮子自己划船,船每次只能载两只狮子。
生存准则:一只小狮如果没有母亲的保护,则会被其它母狮吃掉。母狮之间、小狮之间是安全的。
问题:如何安全过河?
#include <stdio.h>
main()
{
int a[128][9];