资源描述
商仆过河的C程序及运行截屏#include using namespace std;struct Nodeint nMer;int nSer;int length;class Apublic:A();A();void Tspt();/过河的动作void doLeft(int nhead,int ntail,int nlength);private:bool islegal(int nm,int ns); /判断是否满足约束条件,满足为trueNode *funTspt(int nm,int ns,bool flag);/添加STEPhead可以向后延伸的节点bool noRepeat(int nm,int ns);/没有重复返回TRUEvoid funshow(int a2,int ntail);bool funLeft(Node nd,int b1,int b2,int n);void show(int s,int p2,int &top,int &count,int a);int head;int tail;int n;/商仆的对数int nB;/船最多的载人数目Node *STEP;A:A()free(STEP);A:A()coutn;if(n=1)nB=2; cout船最多载人的数目K=nB;else if(n=2) cout船最多载人的数目可以取:;for(int x=n;x=2*n;x+)coutx、;coutendl;coutnB;else if(n=3) cout船最多载人的数目可以取:;for(int x=n-1;x=2*n;x+)coutx、; coutendl;coutnB;else if(n=4) cout船最多载人的数目可以取:;for(int x=n-1;x=2*n;x+)coutx、; coutendl;coutnB;else if(n=5&n=100) cout船最多载人的数目可以取:;for(int x=4;x=2*n;x+)coutx、; coutendl;coutnB;else if(n100) cout本程序仅在S=(0100)以内保证其正确性endl; cout请重新输入商仆的对数S=;goto F;STEP = (Node *)malloc(sizeof(Node)*10000);memset(STEP,0,sizeof(Node)*10000);head = tail = 0;STEP0.nMer = STEP0.nSer = n;int main() cout问题描述: S个商人各带一个随从乘船过河,一只小船只能容纳K人,由他们自己划船。商人们窃听到随从们密谋,在河的任意一岸上,只要随从的人数比商人多,就杀掉商人。但是如何乘船渡河的决策权在商人手中,商人们如何安排渡河计划确保自身安全?endl;A a;a.Tspt();return 0;void A:show(int s,int p2,int &top,int &count,int a)if(top = -1)return ;/已找到目标状态需,输出数据if(top = STEPhead.length)cout* +count *endl;funshow(p,top + 1);B:top-;if(top = -1)return ;C:stop-;if(STEP(stop).length != top)/退过了stop = atop;goto B;if(funLeft(STEP(stop),ptop - 10,ptop - 11,top - 1) = false)goto C;ptop0 = STEP(stop).nMer;ptop1 = STEP(stop).nSer;show(s,p,top,count,a);return ;/在中间加入节点STEP(stop + 1) if(funLeft(STEP(stop + 1),ptop0,ptop1,top) = true)/符合条件top+;ptop0 = STEP(stop).nMer;ptop1 = STEP(stop).nSer;show(s,p,top,count,a);return ;else/不符合条件E:stop + 1-;if(STEP(stop + 1).length = top)/退过了,到了下一层stop + 1 = atop + 1;D:stop-;if(STEP(stop).length != top)/退过了,到了下一层for(int i = top; i = STEPhead.length; i+)si = ai;top-;if(top = -1)return ;goto D;if(top = 0)return ;if(funLeft(STEP(stop),ptop - 10,ptop - 11,top - 1) = false)goto D;ptop0 = STEP(stop).nMer;ptop1 = STEP(stop).nSer;show(s,p,top,count,a);return ;if(funLeft(STEP(stop + 1),ptop0,ptop1,top) = false)goto E;top+;ptop0 = STEP(stop).nMer;ptop1 = STEP(stop).nSer;show(s,p,top,count,a);void A:doLeft(int nhead,int ntail,int nlength)int a1000;int a11000;int sp10002;bool flag = false;memset(a,0xff,4000);memset(a1,0xff,4000);memset(sp,0xff,8000);if(STEPhead.length%2 = 0)flag = true;while(STEPhead.length = nlength - 1)funTspt(STEPhead.nMer,STEPhead.nSer,flag);head+;for(int i = 0; i head + 1; i+)a(STEPi.length) = i;a1(STEPi.length) = i;sp00 = sp01 = n;STEPhead.nMer = STEPhead.nSer = 0;int top = 0;int count = 0;show(a1,sp,top,count,a);bool A:funLeft(Node nd,int b1,int b2,int n)bool flag = abs(nd.nMer - b1) + abs(nd.nSer - b2) 0;if(flag = false)return false;if(n%2 = 0 & b1 = nd.nMer & b2 = nd.nSer)return true;if(n%2 = 1 & b1 = nd.nMer & b2 = nd.nSer)return true;return false;void A:Tspt()Node *temp = new Node;temp = NULL;bool flag = false;while(head tail) cout此问题无解!nMer,temp-nSer,temp-length);/temp-nMer表示headdelete temp;Node* A:funTspt(int nm,int ns,bool flag)/flag = true 向对岸运输Node *nd = NULL;int temp = 1;int tM = STEPhead.nMer;/可供运输的商人数int tS = STEPhead.nSer;/可供运输的仆人数if(flag = false)/向此岸运输tM = n - STEPhead.nMer;tS = n - STEPhead.nSer;temp = -1;for(int i = 0; i tM + 1 & i nB + 1; i+)/i表示运输的商人数for(int j = 0; j tS + 1 & j length = STEPhead.length + 1;nd-nMer = head;nd-nSer = tail;return nd;tail+;STEPtail.length = STEPhead.length + 1;STEPtail.nMer = p;STEPtail.nSer = q;return nd;bool A:noRepeat(int nm,int ns)int j1 = 0;if(STEPhead.length%2 = 0)j1 = 1;for(int i = j1; i tail + 1; i+)if(STEPi.length%2 = j1 & nm = STEPi.nMer & ns = STEPi.nSer)return false;return true;bool A:islegal(int nm,int ns)/商人数少于仆人数或者商人数为0if(nm = 0) | (nm = n) | (nm = ns)return true;return false;void A:funshow(int a2,int ntail)coutendl;cout 商人数 仆人数endl;for(int i = 0; i ntail; i+)cout第i次 ai0 ai1endl;if(i != ntail - 1 & i%2 = 0)cout (abs(ai + 10 - ai0),abs(ai + 11 - ai1)endl;else if(i != ntail - 1 & i%2 = 1)cout - (abs(ai + 10 - ai0),abs(ai + 11 - ai1)endl;coutendl;
展开阅读全文