Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Rotor
  5. {
  6. public:
  7.     std::vector<int> permutation;
  8.     std::vector<int> notches;
  9.     int position;
  10.     int rotateCount;
  11.     bool rotated;
  12.     Rotor()
  13.     {
  14.         position = 1;
  15.         rotateCount = 0;
  16.         rotated = false;
  17.     }
  18.     Rotor(std::vector<int> permutation, std::vector<int> notches)
  19.     {
  20.         position = 1;
  21.         rotateCount = 0;
  22.         rotated = false;
  23.         this->permutation = permutation;
  24.         this->notches = notches;
  25.     }
  26.     Rotor(const Rotor& r) : position(1), rotateCount(0), rotated(false)
  27.     {
  28.         this->permutation.resize(r.permutation.size());
  29.         for (int i = 0; i < r.permutation.size(); i++)
  30.         {
  31.             int k = r.permutation[i];
  32.             this->permutation[i] = k;
  33.         }
  34.  
  35.         this->notches.resize(r.notches.size());
  36.         for (int i = 0; i < r.notches.size(); i++)
  37.         {
  38.             int k = r.notches[i];
  39.             this->notches[i] = k;
  40.         }
  41.     }
  42. };
  43.  
  44. class Reflector
  45. {
  46. public:
  47.     std::vector<int> permutation;
  48.     Reflector(std::vector<int> permutation)
  49.     {
  50.         this->permutation = permutation;
  51.     }
  52. };
  53.  
  54. class Enigma
  55. {
  56. public:
  57.     std::vector<Rotor> fixed_rotors;
  58.     std::vector<Reflector> reflectors;
  59.  
  60.     void AddRotor(Rotor rotor)
  61.     {
  62.         fixed_rotors.push_back(rotor);
  63.     }
  64.     void print_vector(std::ostream& out, const std::vector<int>& vect)
  65.     {
  66.         for (int i : vect)
  67.         {
  68.             out << i << " ";
  69.         }
  70.         out << std::endl;
  71.     }
  72.     void AddReflector(Reflector reflector)
  73.     {
  74.         reflectors.push_back(reflector);
  75.     }
  76.     Rotor setupRotor(int index)
  77.     {
  78.         Rotor rotor = fixed_rotors[index];
  79.         return rotor;
  80.     }
  81.     void setupReflector(int index)
  82.     {
  83.         Reflector reflector = reflectors[index];
  84.     }
  85. };
  86.  
  87. std::vector<int> take_numbers(int number)
  88. {
  89.     std::vector<int> numbers;
  90.     for (int i = 0; i < number; i++)
  91.     {
  92.         int number;
  93.         std::cin >> number;
  94.         numbers.push_back(number);
  95.     }
  96.     return numbers;
  97. }
  98.  
  99. void take_fixed_rotors(int number_letters, Enigma* enigma)
  100. {
  101.     int numberOffixed_rotors;
  102.     int numberOfNotches, positionOfNotch;
  103.     //std::cout << "Give number of fixed_rotors" << std::endl;
  104.     std::cin >> numberOffixed_rotors;
  105.     for (int i = 0; i < numberOffixed_rotors; i++)
  106.     {
  107.         std::vector<int> permutation = take_numbers(number_letters);
  108.  
  109.         for (int j = 1; j <= permutation.size(); j++)
  110.         {
  111.             permutation[j - 1] -= j;
  112.         }
  113.  
  114.         std::cin >> numberOfNotches;
  115.         std::vector<int> notches = take_numbers(numberOfNotches);
  116.  
  117.         Rotor Rotor(permutation, notches);
  118.         enigma->AddRotor(Rotor);
  119.     }
  120. }
  121.  
  122. void take_reflectors(int number_letters, Enigma* enigma)
  123. {
  124.     int numberOfReflectors;
  125.     //std::cout << "Give number of Reflectors" << std::endl;
  126.     std::cin >> numberOfReflectors;
  127.     for (int i = 0; i < numberOfReflectors; i++)
  128.     {
  129.         std::vector<int> permutation = take_numbers(number_letters);
  130.  
  131.         for (int j = 1; j <= permutation.size(); j++)
  132.         {
  133.             permutation[j - 1] -= j;
  134.         }
  135.  
  136.         Reflector reflector(permutation);
  137.         enigma->AddReflector(reflector);
  138.     }
  139. }
  140.  
  141. void rotate(Rotor& rotor)
  142. {
  143.     int tempSwapBeginEnd = rotor.permutation[0];
  144.     for (int i = 1; i < rotor.permutation.size(); i++)
  145.     {
  146.         rotor.permutation[i - 1] = rotor.permutation[i];
  147.     }
  148.     rotor.permutation[rotor.permutation.size() - 1] = tempSwapBeginEnd;
  149.     rotor.position++;
  150.     if (rotor.position > rotor.permutation.size())
  151.     {
  152.         rotor.position = 1;
  153.     }
  154.     rotor.rotateCount++;
  155. }
  156.  
  157. int main()
  158. {
  159.     Enigma enigma;
  160.     int number_letters, move_rotor, tasks_count;
  161.     std::cin >> number_letters;
  162.     take_fixed_rotors(number_letters, &enigma);
  163.     take_reflectors(number_letters, &enigma);
  164.  
  165.     std::cin >> tasks_count;
  166.     std::vector<Rotor> tempRotors;
  167.     while (tasks_count--)
  168.     {
  169.         int rotatorsCount, reflectorIndex;
  170.         std::cin >> rotatorsCount;
  171.         tempRotors.clear();
  172.         tempRotors.resize(rotatorsCount);
  173.  
  174.         for (int i = 0; i < rotatorsCount; i++)
  175.         {
  176.             int rotatorIndex, rotatorPosition;
  177.             std::cin >> rotatorIndex >> rotatorPosition;
  178.  
  179.             Rotor r = Rotor(enigma.fixed_rotors[rotatorIndex]);
  180.             tempRotors[i] = r;
  181.  
  182.  
  183.             while (tempRotors[i].position != rotatorPosition)
  184.             {
  185.                 rotate(tempRotors[i]);
  186.             }
  187.             //enigma.print_vector(std::cout, enigma.fixed_rotors[rotatorIndex].permutation);
  188.         }
  189.  
  190.         std::cin >> reflectorIndex;
  191.         //enigma.print_vector(std::cout, enigma.reflectors[reflectorIndex].permutation);
  192.  
  193.         int variable;
  194.         std::cin >> variable;
  195.         while (variable)
  196.         {
  197.             for (int i = 0; i < tempRotors.size(); i++)
  198.             {
  199.                 if (i != 0 && tempRotors[i].notches.size() == 0)
  200.                     continue;
  201.  
  202.                 if (i == 0 ||
  203.                     (tempRotors[i].notches[0] == tempRotors[i - 1].position && tempRotors[i - 1].rotated)
  204.                     )
  205.                 {
  206.                     rotate(tempRotors[i]);
  207.                 }
  208.                 else
  209.                 {
  210.                     continue;
  211.                 }
  212.             }
  213.  
  214.             for (int i = 0; i < tempRotors.size(); i++) {
  215.                 if (tempRotors[i].rotateCount > 0) {
  216.                     tempRotors[i].rotated = true;
  217.                 }
  218.             }
  219.             //enigma.print_vector(std::cout, tempRotors[0].permutation);
  220.             //std::cout<<"P0 "<<tempRotors[0].position<<std::endl;
  221.             //enigma.print_vector(std::cout, tempRotors[1].permutation);
  222.             //std::cout<<"P1 "<<tempRotors[1].position<<std::endl;
  223.             int result = variable;
  224.             for (int i = 0; i < tempRotors.size(); i++)
  225.             {
  226.                 result += tempRotors[i].permutation[result - 1];
  227.  
  228.                 //std::cout<<variable<<" + "<<enigma.fixed_rotors[rotatorIndex].permutation[variable - 1]<<std::endl;
  229.                 // enigma.print_vector(std::cout, enigma.fixed_rotors[rotatorIndex].permutation);
  230.                 if (result <= 0)
  231.                 {
  232.                     result += tempRotors[i].permutation.size();
  233.                 }
  234.                 else if (result > tempRotors[i].permutation.size())
  235.                 {
  236.                     result -= tempRotors[i].permutation.size();
  237.                 }
  238.                 //std::cout<<result<<std::endl;
  239.             }
  240.             //std::cout << result << std::endl;
  241.             result += enigma.reflectors[reflectorIndex].permutation[result - 1];
  242.  
  243.             if (result <= 0)
  244.             {
  245.                 result += tempRotors[0].permutation.size();
  246.             }
  247.             else if (result > tempRotors[0].permutation.size())
  248.             {
  249.                 result -= tempRotors[0].permutation.size();
  250.             }
  251.             //std::cout << result << std::endl;
  252.  
  253.             for (int i = tempRotors.size(); i > 0; i--)
  254.             {
  255.                 int nextIndex = 0;
  256.                 //std::cout << result << std::endl;
  257.                 for (int j = 0; j < tempRotors[i - 1].permutation.size(); j++)
  258.                 {
  259.                     int value = tempRotors[i - 1].permutation[j] + j + 1;
  260.                     if (value <= 0)
  261.                     {
  262.                         value += tempRotors[i - 1].permutation.size();
  263.                     }
  264.                     else if (value > tempRotors[i - 1].permutation.size())
  265.                     {
  266.                         value -= tempRotors[i - 1].permutation.size();
  267.                     }
  268.                     if (value == result)
  269.                     {
  270.                         nextIndex = j;
  271.                         //std::cout << "Value: " << value << ' ' << tempRotors[i - 1].permutation[j] << ' ' << j << ' ' << std::endl;
  272.                         break;
  273.                     }
  274.                 }
  275.  
  276.                 result = nextIndex + 1;
  277.             }
  278.  
  279.             /*
  280.             if (tempRotors.size() == 1 || tempRotors[0].permutation[result - 1] < 0)
  281.             {
  282.                 int nextIndex = 0;
  283.                 //std::cout<<result<<std::endl;
  284.                 for (int j = 0; j < tempRotors[0].permutation.size(); j++)
  285.                 {
  286.                     int value = tempRotors[0].permutation[j] + j + 1;
  287.                     if (value <= 0)
  288.                     {
  289.                         value += tempRotors[0].permutation.size();
  290.                     }
  291.                     else if (value > tempRotors[0].permutation.size())
  292.                     {
  293.                         value -= tempRotors[0].permutation.size();
  294.                     }
  295.                     if (value == result)
  296.                     {
  297.                         nextIndex = j;
  298.                         //std::cout << "Value: " << value << ' ' << tempRotors[0].permutation[j] << ' ' << j << ' ' << std::endl;
  299.                         break;
  300.                     }
  301.                 }
  302.  
  303.                 result -= tempRotors[0].permutation[nextIndex];
  304.                 if (result <= 0)
  305.                 {
  306.                     result += tempRotors[0].permutation.size();
  307.                 }
  308.                 else if (result > tempRotors[0].permutation.size())
  309.                 {
  310.                     result -= tempRotors[0].permutation.size();
  311.                 }
  312.             }
  313.             */
  314.  
  315.             printf("%d ", result);
  316.             //std::cout << std::endl;
  317.             // enigma.print_vector(std::cout, enigma.fixed_rotors[0].permutation);
  318.             std::cin >> variable;
  319.         }
  320.         printf("\n");
  321.     }
  322.  
  323.     std::cin >> number_letters;
  324.  
  325.     return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement