We found that the number of "mobile packages" used in this topic was only 0,1,2,3 valid, and 4 and 0 were the same.
So we open an array of rot[10] to record how many times these 9 packages were used.
The processing of the dictionary sequence is closely related to our enumeration order.
If we enumerate from 000000000 to 333333333来, the first qualifying result is, of course, the smallest dictionary order in all the answers.
Enumerate this arrangement we need to use 9 for, of course, can also be implemented with recursion, but I still like the visual sense of the more intense 9 for ....
In each arrangement, we need to calculate the temporary result path through these package scenarios.
Then judge whether all the clocks have reached 12 points.
If it is, then we can output it.
When the output, remember to use the number of packages ...
#include <iostream>using namespacestd;intclocks[Ten];//record the inputintrot[Ten];//Record rotation timesintMainintargcChar Const*argv[]) { for(inti =1; I <=9; ++i) {cin>>Clocks[i]; } intpath[Ten]={0}; for(rot[1] =0; rot[1] <4; ++rot[1]) for(rot[2] =0; rot[2] <4; ++rot[2]) for(rot[3] =0; rot[3] <4; ++rot[3]) for(rot[4] =0; rot[4] <4; ++rot[4]) for(rot[5] =0; rot[5] <4; ++rot[5]) for(rot[6] =0; rot[6] <4; ++rot[6]) for(rot[7] =0; rot[7] <4; ++rot[7]) for(rot[8] =0; rot[8] <4; ++rot[8]) for(rot[9] =0; rot[9] <4; ++rot[9]){ //a 4^9 of all the rotations .//determine the result according to the rotation conditionpath[1] = (clocks[1] +3* (rot[1] + rot[2] + rot[4] )) % A; path[2] = (clocks[2] +3* (rot[1] + rot[2] + rot[3] + rot[5] )) % A; path[3] = (clocks[3] +3* (rot[2] + rot[3] + rot[6] )) % A; path[4] = (clocks[4] +3* (rot[1] + rot[4] + rot[5] + rot[7] )) % A; path[5] = (clocks[5] +3* (rot[1] + rot[3] + rot[5] + rot[7] + rot[9] )) % A; path[6] = (clocks[6] +3* (rot[3] + rot[5] + rot[6] + rot[9] )) % A; path[7] = (clocks[7] +3* (rot[4] + rot[7] + rot[8] )) % A; path[8] = (clocks[8] +3* (rot[5] + rot[7] + rot[8] + rot[9])) % A; path[9] = (clocks[9] +3* (rot[6] + rot[8] + rot[9] )) % A; intsum =0; for(inti =1; I <=9; ++i) Sum+ = Path[i];//if it's all 12, it's all 0 . if(!sum) { //found the smallest solution of the first solution, the dictionary order.//Output Solution for(inti =1; I <=9; ++i) { for(intj =0; J < Rot[i]; ++j) {//the number of rot is storedcout<<i<<" "; }} cout<<Endl; return 0; } } return 0;}
"Algorithmic Learning note" 69. Enumeration method dictionary order Processing SJTU OJ 1047 The Clocks