全文预览

农夫过河实验报告

上传者:读书之乐 |  格式:docx  |  页数:16 |  大小:84KB

文档介绍
ile (!isEmptyQueue(moveTo)&&(route[15]== -1)) // 队列不为空,路径未满时循环{ location = getHead(moveTo); // 从队头出队, location 表示位置, 0 为北岸, 1 为南岸 deQueue(moveTo);// 已出队的删除 for (movers = 1; movers <= 8; movers<<= 1) // 向左移位, movers 分别 0001 , 0010 , 0100 , 1000, // 也就是依次判断过河的可行性{ if ((0 != (location & 0x08)) == (0 != (location & movers))) // 判断农夫和要移动的物品是否在同岸{ newlocation = location^(0x08|movers);// 过岸 if (isSafe(newlocation) && (route[newlocation] == -1)) // 判断是否安全,以及路径是否可用{ route[newlocation] = location; enQueue(moveTo, newlocation); // 记录路径并入队,位置改变}}}} /* 打印路径*/ if(route[15] != -1) { cout<<" 过河步骤是: "<<endl; cout<<"\t\t 南岸\t\t\t 北岸"<<endl; i=7; for(location = 15; location >= 0; location = route[location]) { print[i]=location; i--; if (location == 0) break; } for(int j=i+1;j<=7;j++) { cout<<" 第"<<j<<" 步: ";

收藏

分享

举报
下载此文档