Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. /*
  2. ID: mohd_sh2
  3. LANG: C++11
  4. TASK: lamps
  5. */
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <vector>
  11. #include <bitset>
  12. #include <algorithm>
  13. using namespace std;
  14.  
  15. typedef bitset<101> bit;
  16.  
  17. int N, C, t; // using 1-based index
  18. bit final_on, final_off; // store final configuration
  19.  
  20. vector<string> store_bits;
  21.  
  22. void run_switch(bit &lamps, int option) {
  23. switch (option) {
  24. case 1:
  25. lamps.flip();
  26. break;
  27. case 2:
  28. for (int i = 1; i <= N; i += 2) lamps.flip(i);
  29. break;
  30. case 3:
  31. for (int i = 2; i <= N; i += 2) lamps.flip(i);
  32. break;
  33. case 4:
  34. for (int i = 1; i <= N; i += 3) lamps.flip(i);
  35. break;
  36. }
  37. }
  38.  
  39. void combination(bit &lamps, int prev, int depth) {
  40.  
  41. bit check_on = lamps & final_on;
  42. bit check_off = lamps & final_off;
  43.  
  44. if(check_on.count() == final_on.count()) // check if ON final condition mets
  45. if(check_off.count() == 0) { // check if OFF final condition mets
  46.  
  47. // stores bit-set as string
  48.  
  49. string tmp;
  50. for(int i=1; i<=N; i++) tmp += (lamps[i]+'0');
  51.  
  52. if(find(store_bits.begin(), store_bits.end(), tmp) == store_bits.end())
  53. store_bits.push_back(tmp);
  54. }
  55.  
  56. // base case
  57. if (depth <= 0)
  58. return;
  59.  
  60. for (int i = prev + 1; i <= 4; i++) {
  61. run_switch(lamps, i); // do
  62. combination(lamps, i, depth-1); // run next combination
  63. run_switch(lamps, i); // undo
  64. }
  65. }
  66.  
  67. int main() {
  68.  
  69. #ifndef STDIN
  70. freopen("lamps.out", "w", stdout);
  71. freopen("lamps.in", "r", stdin);
  72. #endif
  73.  
  74. cin >> N >> C;
  75. while (cin >> t, t >= 0) final_on.set(t, 1);
  76. while (cin >> t, t >= 0) final_off.set(t, 1);
  77.  
  78. bit initial;
  79. for(int i=1; i<=N; i++) initial.set(i, 1); // set all lamps ON on initial
  80.  
  81. // find all possible states
  82. C = C > 3 ? 4 : C; // there will be no solutions after 2^4
  83. combination(initial, 0, C);
  84.  
  85. // store the bits
  86. sort(store_bits.begin(), store_bits.end(), [](const string a, const string b) -> bool {
  87. return a < b;
  88. });
  89.  
  90. if(store_bits.empty()) {
  91. cout << "IMPOSSIBLE" << endl;
  92. } else {
  93. // print all bits
  94. for(string each : store_bits) {
  95. cout << each << endl;
  96. }
  97. }
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement