设为首页 加入收藏

TOP

C语言实现农夫过河代码及解析(四)
2018-11-20 22:09:58 】 浏览:1275
Tags:语言 实现 农夫 过河 代码 解析
=2; i++)
    {
        b[Step]=i;
        memcpy(a[Step+1], a[Step], 16);  /*复制上一步状态,进行下一步移动*/
        a[Step+1][3]=1-a[Step+1][3];  /*农夫过去或者回来*/
        if(i == -1)
        {
            search(Step+1);  /*进行第一步*/
        }
        else
            if(a[Step][i] == a[Step][3])  /*若该物与农夫同岸,带回*/
            {
                a[Step+1][i]=a[Step+1][3];  /*带回该物*/
                search(Step+1);  /*进行下一步*/
            }
    }
    return 0;
}


int main()
{
    printf("\n\n            农夫过河问题,解决方案如下:\n\n\n");
    search(0);
    return 0;
}


运行结果


            农夫过河问题,解决方案如下:



east: wolf  goat  cabbage  farmer              west: none



                      the 1 time
east: wolf  cabbage              west: goat  farmer 



                      the 2 time
                  <-----  farmer       
east: wolf  cabbage  farmer              west: goat 



                      the 3 time
                  ----->  farmer and wolf
east: cabbage              west: wolf  goat  farmer 



                      the 4 time
                  <-----  farmer and goat
east: goat  cabbage  farmer              west: wolf 



                      the 5 time
                  ----->  farmer and cabbage
east: goat              west: wolf  cabbage  farmer 



                      the 6 time
                  <-----  farmer       
east: goat  farmer              west: wolf  cabbage 



                      the 7 time
                  ----->  farmer and goat
east: none            west: wolf  goat  cabbage  farmer 


 


 



east: wolf  goat  cabbage  farmer              west: none



                      the 1 time
east: wolf  cabbage              west: goat  farmer 



  &n

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python与C/C++混合编程的应用 下一篇C语言矩阵转置代码及解析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目