895.cpp
上传用户:today604
上传日期:2022-08-03
资源大小:1009k
文件大小:7k
开发平台:

Visual C++

  1. #include <IOSTREAM>    
  2. #include <VECTOR>    
  3. #include <STDIO.H> 
  4. #include<fstream.h>   
  5. ofstream   r_out("传教士和野人过河.txt",ios::in||ios::app);   //文件流 ,用来输出结果
  6. using namespace std;   
  7. /*  
  8. 定义三元组:(m, c, b)   
  9. 其中: 0 <= m <=3,表示传教士在河左岸的人数。  
  10.     0<= c <=3,表示野人在河左岸的认输。  
  11.     b=1,表示船在左岸,b=0,表示船在右岸。  
  12. 2,规则集   
  13. 按每次渡河的人数分别写出每一个规则,  
  14. 共 (1 1)、(1 0)、(0 1)、(0,2),(2,0)3种渡河的可能(其中(x y)  
  15. 表示x个传教士和y个野人上船渡河),因此共有10个规则  
  16.   (从左岸到右岸、右岸到左岸各3个)。  
  17.   规则集如下:   
  18.   r1:IF (m, c, 1) THEN (m-1, c-1, 0)  
  19.   r2:IF (m, c, 1) THEN (m-1, c, 0)   
  20.   r3:IF (m, c, 1) THEN (m, c-1, 0)  
  21.   r4:IF (m, c, 1) THEN (m, c-2, 0)  
  22.   r5:IF (m, c, 1) THEN (m-2, c, 0)  
  23.   r4:IF (m, c, 0) THEN (m+1, c+1, 1)  
  24.   r5:IF (m, c, 0) THEN (m+1, c, 1)  
  25.   r6:IF (m, c, 0) THEN (m, c+1, 1)   
  26.   r6:IF (m, c, 0) THEN (m, c+2, 1)   
  27.   r6:IF (m, c, 0) THEN (m+2, c, 1)   
  28.   3,初始状态:(3, 3, 1)  
  29.   4,结束状态:(0, 0, 0)   
  30. */   
  31. typedef struct _State   
  32. {   
  33.     int m;  // 表示传教士在河左岸的人数    
  34.     int c;  // 表示野人在河左岸的人数  
  35.     int b;  // b=1,表示船在左岸,b=0,表示船在右岸    
  36. }State;   
  37. typedef struct _Rule   
  38. {   
  39.     int m;   
  40.     int c;   
  41. }Rule; 
  42. int ms;
  43. int k;
  44. int r; 
  45. Rule rule[1000];  // 规则集 
  46. class MoveGroup 
  47. {
  48. public:
  49.     Rule move[500]; //算符集
  50. int numMove; //可用算符的总数
  51. MoveGroup(){}
  52.     int Group(int MAX_ON_BOAT) {    //利用构造器求算符集
  53. int m, c, i = 0;
  54. for (m = 0; m <=MAX_ON_BOAT; m++)
  55. for (c = 0; c <= MAX_ON_BOAT; c++){
  56. if (c==0 && m!=0) {
  57. move[i].m=m;
  58. move[i].c=0;
  59. i++;
  60. }
  61. else if (m==0 && c!=0) {
  62. move[i].m=0;
  63. move[i].c=c;
  64. i++;
  65. }
  66. else if (m!=0&&c!=0&&m+c<=MAX_ON_BOAT&& m>=c) {
  67. move[i].m = m;
  68. move[i].c = c;
  69. i++;
  70. }
  71. numMove = i;
  72. }return numMove;
  73. }
  74. };
  75. long int d=1;  
  76. void OutResult(const vector<int>& ruleSet, const vector< State >& StateSet, const int index)   
  77. {   r_out <<"第"<<d<<"种方案:" <<endl; 
  78. r_out <<"   " << "初始状态" << "         " << "(" << ms << "," << ms << ")";   
  79. r_out << "         " << "(" << 0 << "," << 0 << ")" << "    " << "left" << endl;   
  80. for(int i = 0; i <= index; i ++)   
  81. {   
  82. r_out << i +1 << "  ";   
  83. if( i % 2 == 0 )   
  84. r_out << "L-to-R" << "(" << rule[ ruleSet[i] ].m    
  85. << "," << rule[ ruleSet[i] ].c << ")";   
  86. else    
  87. r_out << "R-to-L" << "(" << rule[ ruleSet[i] ].m    
  88. << "," << rule[ ruleSet[i] ].c << ")";   
  89. r_out << "      (" << StateSet[i].m << "," << StateSet[i].c << ")";   
  90. r_out << "         (" << ms-StateSet[i].m << "," <<ms-StateSet[i].c << ")";   
  91. if( StateSet[i].b == 1 )   
  92. r_out << "    " << "left " << endl;   
  93. else    
  94. r_out << "    " << "right " << endl;   
  95. d++;
  96. }   
  97. bool IsExist(const vector< State >& StateSet, const State& state, const int index)   
  98. {   
  99.     if( state.m == ms && state.c == ms && state.b == 1) return true;   
  100.     for(int i = 0; i <= index; i ++)   
  101.         if(StateSet[i].m == state.m && StateSet[i].c == state.c && StateSet[i].b == state.b)   
  102.             return true;   
  103. return false;   
  104. }   
  105. bool SearchRule(vector<int>& ruleSet,    
  106.                 vector< State >& StateSet, int index,   
  107.                 State NowState)   
  108. {   
  109.     if(NowState.b == 0 && NowState.c == 0 && NowState.m == 0) // 达到最终状态    
  110.     {   
  111.         OutResult(ruleSet, StateSet, index);   
  112.         return true;   
  113.     }   
  114.     int iSize = ruleSet.size();   
  115.     if( iSize <= index )   
  116.     {   
  117.         r_out << "full" << endl;   
  118.         return false;   
  119.     }   
  120.     State state;   
  121.     for(int i = 0; i < r; i ++)   
  122.     {   
  123.         state = NowState;   
  124.         if( NowState.b == 1 ) // 船在左岸    
  125.         {   
  126.             state.m -= rule[i].m;   
  127.             state.c -= rule[i].c;   
  128.             if( state.m < 0 || state.c < 0 ) continue;   
  129.             // state为安全状态    
  130.             if( state.m == state.c ||  state.m == ms || state.m == 0 )   
  131.             {   
  132.                 state.b = 0;   
  133.                 if( IsExist(StateSet, state, index) ) continue;   
  134.                 ruleSet[ index + 1 ] = i;   
  135.                 StateSet[ index + 1 ] = state;   
  136. //      if( SearchRule( ruleSet, StateSet, index+1, state) ) return true;    
  137.                 SearchRule(ruleSet, StateSet, index+1, state);   
  138.             }   
  139.         }   
  140.         else  // 在右岸    
  141.         {   
  142.             state.m += rule[i].m;   
  143.             state.c += rule[i].c;   
  144.             if( state.m > ms || state.c >ms) continue;   
  145.             if( state.m == state.c ||  state.m == ms || state.m == 0 )   
  146.             {   
  147.                 state.b = 1;   
  148.                 if( IsExist(StateSet, state, index) ) continue;   
  149.                 ruleSet[ index + 1 ] = i;   
  150.                 StateSet[ index + 1 ] = state;   
  151. //  if( SearchRule(ruleSet, StateSet, index+1, state) ) return true;    
  152.                 SearchRule(ruleSet, StateSet, index+1, state);   
  153.             }      
  154.         }   
  155.     }   
  156.     return false;   
  157. bool IsExist2(int m,int n)
  158. {
  159. if(m<=0)
  160. return false;
  161. else if(m<6)
  162. {
  163. if(n>=m/2+1)
  164. return true;
  165. else
  166. return false;
  167. }
  168. else
  169. {
  170. if(n>=4)
  171. return true;
  172. else
  173. return false;
  174. }
  175. }  
  176. int main()   
  177. {   r_out<<"输入人数m,和船的载重量k:"<<endl;
  178. std::cout<<"输入人数m,和船的载重量k:"<<endl;
  179. std::cin>>ms>>k;
  180. r_out<<"传教士和野人数各为:"<<ms<<" 船的载重量: "<<k<<endl;
  181. if(IsExist2(ms,k)==1)
  182. {
  183. MoveGroup p;
  184. r=p.Group (k);
  185. for(int i=0;i<r;i++)
  186. rule[i]=p.move[i];
  187.     State InitState = {ms,ms, 1};  // 初始状态    
  188.     vector< int > ruleSet;          // 存放摆渡规则在规则集中的序号    
  189.     vector< State > StateSet;    // 存放状态    
  190.     ruleSet.resize( 10000 );   
  191.     StateSet.resize( 10000 );   
  192.     r_out << "假设初始状态,野人、传教士和船都在左岸" << endl;   
  193.     r_out << "   摆渡规则" << "     " << "左岸(m,c)" << "     " << "右岸(m,c)"   
  194. << " " << "船的状态" << endl;   
  195.     SearchRule(ruleSet, StateSet, -1, InitState);   
  196.     r_out << "结果说明:" << endl;   
  197.     r_out << "L-to-R(2,0)  (1,1)   (2,2)   right" << endl;   
  198.     r_out << "表示从左岸向右岸运2个传教士和一个野人,然后左岸传教" << endl;   
  199.     r_out << "士和野人为(1,1), 右岸为(2,2), 船到右岸" << endl;   
  200.     r_out << endl;   
  201.     r_out << "姓名:汪潮" << endl;   
  202.     r_out << "学号:0705030121" << endl;   
  203.     r_out << "专业:智能科学与技术" << endl; 
  204. }
  205. else
  206. {
  207.    r_out<<"不可能事件:"<<endl;
  208. }
  209. getchar();   
  210. return 1;   
  211. }