三人三鬼过河问题的解

int h1[10][3]={{0,0,0},{0,1,-1},{0,2,-1},{0,3,-1},{1,1,-1},{2,2,-1}, {3,0,-1},{3,1,-1},{3,2,-1},{3,3,-1}}; int h2[10][3]={{0,0,0},{0,1,-1},{0,2,-1},{0,3,-1},{1,1,-1},{2,2,-1}, {3,0,-1},{3,1,-1},{3,2,-1},{3,3,-1}}; int a[20][2]; void test(int i) {int l,d,m,n,f,t1,t2,k; if(i>=20)return; if(h1[9][2]!=-1) {for(l=0;l<20;l++)    {for(d=0;d<2;d++)      printf("%d",a[l][d]);    } printf("/n"); } if(h1[9][2]==-1) {k=i%2?-1:1;    if(k==1)    {    for(m=0;m<=2;m++)    for(n=0;n<=2;n++)    if(m+n<=2&&m+n>0)     {     t1=a[i][0]+m;     t2=a[i][1]+n;     for(f=0;f<10;f++)      if(h1[f][0]==t1&&h1[f][1]==t2&&h1[f][2]==-1)         {h1[f][2]=0;             a[i+1][0]=t1;           a[i+1][1]=t2;           test(i+1);           h1[f][2]=-1;        }     }   } if(k==-1) { for(m=0;m<=2;m++) for(n=0;n<=2;n++) if(m+n<=2&&m+n>0)   {    t1=a[i][0]-m;    t2=a[i][1]-n;    for(f=0;f<10;f++)    if(h2[f][0]==t1&&h2[f][1]==t2&&h2[f][2]==-1)         {h2[f][2]=0;                      a[i+1][0]=t1;           a[i+1][1]=t2;           test(i+1);                                  h2[f][2]=-1;        }   } } } } main() {test(0); getch(); }

 还是过河问题,这次有了结果.

另外发现八皇后问题也可用次方法解决.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值