Daily small Lianbi 10-Little monk carrying water (backtracking method)

Source: Internet
Author: User

Topic
7 Monks in a temple: take turns to carry water, in order to not conflict with other tasks, each person will have a vacant number of days listed as follows:
Monk 1: Tuesday, four;
Monk 2: Monday, six;
Monk 3: Wednesday, day;
Monk 4: Friday;
Monk 5: Monday, four, six;
Monk 6: Tuesday, five;
Monk 7: Wednesday, six, day;
Please arrange all reasonable time for carrying water to the table
Solution to the method of thinking back to the moon
The return method, which is one step, attempts to expand the decomposition of the part on the basis of the current partial solution. Expand, first check whether the expansion of the violation of the constraints, if not violated, then expand, and then continue on this basis in accordance with a similar method, until the complete solution; if violated, discard the step and the part of the solution it can generate, and then try other possible ways of widening in a similar way, Until I tried all the way to enlarge it.

Please enter Monk 1 idle time: 0 1 0 1 0 0 0
Please enter Monk 2 idle time: 1 0 0 0 0 1 0
Please enter Monk 3 idle time: 0 0 1 0 0 0 1
Please enter Monk 4 idle time: 0 0 0 0 1 0 0
Please enter Monk 5 idle time: 1 0 0 1 0 1 0
Please enter Monk 6 idle time: 0 1 0 0 1 0 0
Please enter Monk 7 idle time: 0 0 1 0 0 1 1

This is the simplified version of the eight Queen's problem.

The retrospective recursive function of the eight queen is simplified to

void  queue  (int  num) {for  (int  i=0 ; I<7 ; ++i) {if  (Table[num][i]== ' 1 ' ) //add This Code  {time[num]=i; if  (Test (Time,num)) {//COUT<<NUM<<ENDL;  if  (Num==6 ) {Q_print (); } else  queue  (num+1 ); } } }}

Complete the Code for

#include <iostream>#include <fstream>#include <algorithm>#include <vector>#include <string>using namespace STD;inttime[7]; vector <string>TableintCntBOOLTestintTime[],intNUM) {if(num = =0)return true; for(intI=0; i<num;++i) {if(Time[i]==time[num])return false; }return true;}voidQ_print () {cout<<endl; for(intI=0;i<7; ++i)cout<<time[i]+1<<" ";cout<<endl; cnt++;}void Queue(intNUM) { for(intI=0;i<7; ++i) {if(table[num][i]==' 1 ') {time[num]=i;if(Test (Time,num)) {//cout<<num<<endl;                if(num==6) {q_print (); }Else                    Queue(num+1); }        }    }}intMain () {stringBuff Cnt=0; Freopen ("Test.txt","R", stdin); for(intI=0;i<8; ++i) {getline (Cin, buff);    Table.push_back (Buff); }Queue(0);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Daily small Lianbi 10-Little monk carrying water (backtracking method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.