fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <limits>
  4. #include <set>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. void generate(vector<int>& temp, int& target, const size_t width, const size_t i) {
  10. const auto replacement = temp[i];
  11.  
  12. if (target > replacement) {
  13. replace(begin(temp), next(begin(temp), min(temp.size(), i + width - 1)), +target, replacement);
  14. } else {
  15. target = replacement;
  16. }
  17. }
  18.  
  19. int main() {
  20. const vector<char> rooms = { 0b1101, 0b110, 0b1101, 0b110, 0b1100, 0b101, 0b110,
  21. 0b1110, 0b1001, 0b110, 0b1011, 0b1010, 0b1111, 0b1010,
  22. 0b1000, 0b101, 0b11, 0b1110, 0b1011, 0b1110, 0b1010,
  23. 0b1011, 0b1101, 0b101, 0b1, 0b101, 0b11, 0b1011 };
  24. const size_t width = 7U;
  25. auto result = 0;
  26. vector<int> temp(rooms.size());
  27.  
  28. for (size_t i = 0U; i < rooms.size(); ++i) {
  29. const auto toWest = (rooms[i] & 0b1000) == 0;
  30. const auto toNorth = (rooms[i] & 0b100) == 0;
  31. const auto toEast = (rooms[i] & 0b10) == 0;
  32. const auto toSouth = (rooms[i] & 0b1) == 0;
  33. const auto west = toWest && temp[i - 1] != 0 ? temp[i - 1] : numeric_limits<int>::max();
  34. const auto north = toNorth && temp[i - width] != 0 ? temp[i - width] : numeric_limits<int>::max();
  35. const auto east = toEast && temp[i + 1] != 0 ? temp[i + 1] : numeric_limits<int>::max();
  36.  
  37. temp[i] = min({ temp[i] != 0 ? temp[i] : numeric_limits<int>::max(), result + 1, west, north, east });
  38.  
  39. if (temp[i] == result + 1) ++result;
  40.  
  41. if (toWest) generate(temp, temp[i - 1], width, i);
  42. if (toNorth) generate(temp, temp[i - width], width, i);
  43. if (toEast) generate(temp, temp[i + 1], width, i);
  44. if (toSouth) temp[i + width] = temp[i];
  45. }
  46.  
  47. for (auto it = cbegin(temp); it != cend(temp);) {
  48. for (auto i = 0; i < width; ++i, ++it) cout << *it << '\t';
  49. cout << endl;
  50. }
  51.  
  52. cout << endl << set<int>(cbegin(temp), cend(temp)).size() << endl;
  53. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
1	1	2	2	3	3	3	
1	1	1	2	3	5	3	
1	1	1	6	3	6	3	
1	6	6	6	6	6	3	

5