Guest User

Sticks

a guest
Apr 21st, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <set>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. void generate(std::set<std::string> &set,
  6.     std::pair<int, int>* sticks,
  7.     const int sticksSize,
  8.     int index) {
  9.  
  10.      if (index == sticksSize){
  11.          std::string temp;
  12.          for (int i = 0; i < sticksSize; i++) {
  13.              temp += "|" + std::to_string(sticks[i].first) + "-";
  14.              temp += std::to_string(sticks[i].second) + "|";
  15.              temp += (i == sticksSize - 1 ? "" : " # ");
  16.          }
  17.          set.insert(temp);
  18.      }else{
  19.          for (int i = index; i < sticksSize; i++){
  20.              std::swap(sticks[index],sticks[i]);
  21.              generate(set, sticks, sticksSize, i + 1);
  22.              
  23.              std::swap(sticks[index].first,sticks[index].second);
  24.              generate(set, sticks, sticksSize, i + 1);
  25.              
  26.              std::swap(sticks[index].first,sticks[index].second);
  27.              std::swap(sticks[index],sticks[i]);
  28.          }
  29.      }
  30. }
  31.  
  32. int main() {
  33.     std::set<std::string> set;
  34.     int steps; std::cin >> steps;
  35.     std::pair<int, int> sticks[steps];
  36.    
  37.     for (int i = 0; i < steps; i++) {
  38.         int first, second;
  39.         std::cin >> first >> second;
  40.         sticks[i] = std::make_pair(first, second);
  41.     }
  42.  
  43.     generate(set, sticks, steps, 0);
  44.    
  45.     std::cout << set.size() << std::endl;
  46.     std::set<std::string>::iterator it=set.begin();
  47.     for (it; it!=set.end(); ++it)
  48.         std::cout << *it << std::endl;
  49.     return 0;
  50. }
Add Comment
Please, Sign In to add comment