Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- class Rotor
- {
- public:
- std::vector<int> permutation;
- std::vector<int> notches;
- int position;
- int rotateCount;
- bool rotated;
- Rotor()
- {
- position = 1;
- rotateCount = 0;
- rotated = false;
- }
- Rotor(std::vector<int> permutation, std::vector<int> notches)
- {
- position = 1;
- rotateCount = 0;
- rotated = false;
- this->permutation = permutation;
- this->notches = notches;
- }
- Rotor(const Rotor& r) : position(1), rotateCount(0), rotated(false)
- {
- this->permutation.resize(r.permutation.size());
- for (int i = 0; i < r.permutation.size(); i++)
- {
- int k = r.permutation[i];
- this->permutation[i] = k;
- }
- this->notches.resize(r.notches.size());
- for (int i = 0; i < r.notches.size(); i++)
- {
- int k = r.notches[i];
- this->notches[i] = k;
- }
- }
- };
- class Reflector
- {
- public:
- std::vector<int> permutation;
- Reflector(std::vector<int> permutation)
- {
- this->permutation = permutation;
- }
- };
- class Enigma
- {
- public:
- std::vector<Rotor> fixed_rotors;
- std::vector<Reflector> reflectors;
- void AddRotor(Rotor rotor)
- {
- fixed_rotors.push_back(rotor);
- }
- void print_vector(std::ostream& out, const std::vector<int>& vect)
- {
- for (int i : vect)
- {
- out << i << " ";
- }
- out << std::endl;
- }
- void AddReflector(Reflector reflector)
- {
- reflectors.push_back(reflector);
- }
- Rotor setupRotor(int index)
- {
- Rotor rotor = fixed_rotors[index];
- return rotor;
- }
- void setupReflector(int index)
- {
- Reflector reflector = reflectors[index];
- }
- };
- std::vector<int> take_numbers(int number)
- {
- std::vector<int> numbers;
- for (int i = 0; i < number; i++)
- {
- int number;
- std::cin >> number;
- numbers.push_back(number);
- }
- return numbers;
- }
- void take_fixed_rotors(int number_letters, Enigma* enigma)
- {
- int numberOffixed_rotors;
- int numberOfNotches, positionOfNotch;
- //std::cout << "Give number of fixed_rotors" << std::endl;
- std::cin >> numberOffixed_rotors;
- for (int i = 0; i < numberOffixed_rotors; i++)
- {
- std::vector<int> permutation = take_numbers(number_letters);
- for (int j = 1; j <= permutation.size(); j++)
- {
- permutation[j - 1] -= j;
- }
- std::cin >> numberOfNotches;
- std::vector<int> notches = take_numbers(numberOfNotches);
- Rotor Rotor(permutation, notches);
- enigma->AddRotor(Rotor);
- }
- }
- void take_reflectors(int number_letters, Enigma* enigma)
- {
- int numberOfReflectors;
- //std::cout << "Give number of Reflectors" << std::endl;
- std::cin >> numberOfReflectors;
- for (int i = 0; i < numberOfReflectors; i++)
- {
- std::vector<int> permutation = take_numbers(number_letters);
- for (int j = 1; j <= permutation.size(); j++)
- {
- permutation[j - 1] -= j;
- }
- Reflector reflector(permutation);
- enigma->AddReflector(reflector);
- }
- }
- void rotate(Rotor& rotor)
- {
- int tempSwapBeginEnd = rotor.permutation[0];
- for (int i = 1; i < rotor.permutation.size(); i++)
- {
- rotor.permutation[i - 1] = rotor.permutation[i];
- }
- rotor.permutation[rotor.permutation.size() - 1] = tempSwapBeginEnd;
- rotor.position++;
- if (rotor.position > rotor.permutation.size())
- {
- rotor.position = 1;
- }
- rotor.rotateCount++;
- }
- int main()
- {
- Enigma enigma;
- int number_letters, move_rotor, tasks_count;
- std::cin >> number_letters;
- take_fixed_rotors(number_letters, &enigma);
- take_reflectors(number_letters, &enigma);
- std::cin >> tasks_count;
- std::vector<Rotor> tempRotors;
- while (tasks_count--)
- {
- int rotatorsCount, reflectorIndex;
- std::cin >> rotatorsCount;
- tempRotors.clear();
- tempRotors.resize(rotatorsCount);
- for (int i = 0; i < rotatorsCount; i++)
- {
- int rotatorIndex, rotatorPosition;
- std::cin >> rotatorIndex >> rotatorPosition;
- Rotor r = Rotor(enigma.fixed_rotors[rotatorIndex]);
- tempRotors[i] = r;
- while (tempRotors[i].position != rotatorPosition)
- {
- rotate(tempRotors[i]);
- }
- //enigma.print_vector(std::cout, enigma.fixed_rotors[rotatorIndex].permutation);
- }
- std::cin >> reflectorIndex;
- //enigma.print_vector(std::cout, enigma.reflectors[reflectorIndex].permutation);
- int variable;
- std::cin >> variable;
- while (variable)
- {
- for (int i = 0; i < tempRotors.size(); i++)
- {
- if (i != 0 && tempRotors[i].notches.size() == 0)
- continue;
- if (i == 0 ||
- (tempRotors[i].notches[0] == tempRotors[i - 1].position && tempRotors[i - 1].rotated)
- )
- {
- rotate(tempRotors[i]);
- }
- else
- {
- continue;
- }
- }
- for (int i = 0; i < tempRotors.size(); i++) {
- if (tempRotors[i].rotateCount > 0) {
- tempRotors[i].rotated = true;
- }
- }
- //enigma.print_vector(std::cout, tempRotors[0].permutation);
- //std::cout<<"P0 "<<tempRotors[0].position<<std::endl;
- //enigma.print_vector(std::cout, tempRotors[1].permutation);
- //std::cout<<"P1 "<<tempRotors[1].position<<std::endl;
- int result = variable;
- for (int i = 0; i < tempRotors.size(); i++)
- {
- result += tempRotors[i].permutation[result - 1];
- //std::cout<<variable<<" + "<<enigma.fixed_rotors[rotatorIndex].permutation[variable - 1]<<std::endl;
- // enigma.print_vector(std::cout, enigma.fixed_rotors[rotatorIndex].permutation);
- if (result <= 0)
- {
- result += tempRotors[i].permutation.size();
- }
- else if (result > tempRotors[i].permutation.size())
- {
- result -= tempRotors[i].permutation.size();
- }
- //std::cout<<result<<std::endl;
- }
- //std::cout << result << std::endl;
- result += enigma.reflectors[reflectorIndex].permutation[result - 1];
- if (result <= 0)
- {
- result += tempRotors[0].permutation.size();
- }
- else if (result > tempRotors[0].permutation.size())
- {
- result -= tempRotors[0].permutation.size();
- }
- //std::cout << result << std::endl;
- for (int i = tempRotors.size(); i > 0; i--)
- {
- int nextIndex = 0;
- //std::cout << result << std::endl;
- for (int j = 0; j < tempRotors[i - 1].permutation.size(); j++)
- {
- int value = tempRotors[i - 1].permutation[j] + j + 1;
- if (value <= 0)
- {
- value += tempRotors[i - 1].permutation.size();
- }
- else if (value > tempRotors[i - 1].permutation.size())
- {
- value -= tempRotors[i - 1].permutation.size();
- }
- if (value == result)
- {
- nextIndex = j;
- //std::cout << "Value: " << value << ' ' << tempRotors[i - 1].permutation[j] << ' ' << j << ' ' << std::endl;
- break;
- }
- }
- result = nextIndex + 1;
- }
- /*
- if (tempRotors.size() == 1 || tempRotors[0].permutation[result - 1] < 0)
- {
- int nextIndex = 0;
- //std::cout<<result<<std::endl;
- for (int j = 0; j < tempRotors[0].permutation.size(); j++)
- {
- int value = tempRotors[0].permutation[j] + j + 1;
- if (value <= 0)
- {
- value += tempRotors[0].permutation.size();
- }
- else if (value > tempRotors[0].permutation.size())
- {
- value -= tempRotors[0].permutation.size();
- }
- if (value == result)
- {
- nextIndex = j;
- //std::cout << "Value: " << value << ' ' << tempRotors[0].permutation[j] << ' ' << j << ' ' << std::endl;
- break;
- }
- }
- result -= tempRotors[0].permutation[nextIndex];
- if (result <= 0)
- {
- result += tempRotors[0].permutation.size();
- }
- else if (result > tempRotors[0].permutation.size())
- {
- result -= tempRotors[0].permutation.size();
- }
- }
- */
- printf("%d ", result);
- //std::cout << std::endl;
- // enigma.print_vector(std::cout, enigma.fixed_rotors[0].permutation);
- std::cin >> variable;
- }
- printf("\n");
- }
- std::cin >> number_letters;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement