没有合适的资源?快使用搜索试试~ 我知道了~
用三维数组STATE(0:n,0:n,0:n)代表渡河过程中所有状态(合法的和非法的)。STATE(x1,x2,x3)为真,表示该状态已经出现过(“已达”);为假,表示未曾出现过(“未达”)。
资源推荐
资源详情
资源评论
#include<stdio.h>
void main()
struct INFO {
int nSavage; // 岸边野人的数量 开始为3 全部到对岸为0
int nBoanerges; // 岸边传教士的数量 开始为3 全部到对岸为0
int nSide; // 船的位置 在此岸为-1 彼岸为1
int nMoveSavage; // 渡河的野人的数量,用于递归时记录操作状态
int nMoveBoanerges; // 渡河的传教士的数量,用于递归时记录操作状态
INFO* pPrevious;
INFO* pNext;
};
// 判断是否有下一个满足条件的渡河方案
bool GetNextMove(INFO* pInfo)
{
// 渡船的优先顺序
// 此岸数量多优先,野人优先,彼岸数量少优先,传教士优先。
const int nStoreCount = 5;
struct STORE
{
int nSavage;
int nBoanerges;
};
STORE listStore[nStoreCount];
if (pInfo->nSide == -1)
{
listStore[0].nSavage = 2;
listStore[0].nBoanerges = 0;
listStore[1].nSavage = 1;
void main()
struct INFO {
int nSavage; // 岸边野人的数量 开始为3 全部到对岸为0
int nBoanerges; // 岸边传教士的数量 开始为3 全部到对岸为0
int nSide; // 船的位置 在此岸为-1 彼岸为1
int nMoveSavage; // 渡河的野人的数量,用于递归时记录操作状态
int nMoveBoanerges; // 渡河的传教士的数量,用于递归时记录操作状态
INFO* pPrevious;
INFO* pNext;
};
// 判断是否有下一个满足条件的渡河方案
bool GetNextMove(INFO* pInfo)
{
// 渡船的优先顺序
// 此岸数量多优先,野人优先,彼岸数量少优先,传教士优先。
const int nStoreCount = 5;
struct STORE
{
int nSavage;
int nBoanerges;
};
STORE listStore[nStoreCount];
if (pInfo->nSide == -1)
{
listStore[0].nSavage = 2;
listStore[0].nBoanerges = 0;
listStore[1].nSavage = 1;
listStore[1].nBoanerges = 1;
listStore[2].nSavage = 0;
listStore[2].nBoanerges = 2;
listStore[3].nSavage = 1;
listStore[3].nBoanerges = 0;
listStore[4].nSavage = 0;
listStore[4].nBoanerges = 1;
}
else
{
listStore[0].nSavage = 0;
listStore[0].nBoanerges = 1;
listStore[1].nSavage = 1;
listStore[1].nBoanerges = 0;
listStore[2].nSavage = 0;
listStore[2].nBoanerges = 2;
listStore[3].nSavage = 1;
listStore[3].nBoanerges = 1;
listStore[4].nSavage = 2;
listStore[4].nBoanerges = 0;
}
int iStart;
if (pInfo->nMoveSavage == 0 && pInfo->nMoveBoanerges == 0)
{
iStart = 0;
}
else
{
for (iStart=0; iStart<nStoreCount; iStart++)
listStore[2].nSavage = 0;
listStore[2].nBoanerges = 2;
listStore[3].nSavage = 1;
listStore[3].nBoanerges = 0;
listStore[4].nSavage = 0;
listStore[4].nBoanerges = 1;
}
else
{
listStore[0].nSavage = 0;
listStore[0].nBoanerges = 1;
listStore[1].nSavage = 1;
listStore[1].nBoanerges = 0;
listStore[2].nSavage = 0;
listStore[2].nBoanerges = 2;
listStore[3].nSavage = 1;
listStore[3].nBoanerges = 1;
listStore[4].nSavage = 2;
listStore[4].nBoanerges = 0;
}
int iStart;
if (pInfo->nMoveSavage == 0 && pInfo->nMoveBoanerges == 0)
{
iStart = 0;
}
else
{
for (iStart=0; iStart<nStoreCount; iStart++)
剩余5页未读,继续阅读
资源评论
baojeon
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功