mihaild

Untitled

Apr 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. for (int i = 0; i < 100000; ++i) {
  2.         vector<ipair> path;
  3.         std::random_shuffle(cells.begin(), cells.end());
  4.         path.emplace_back(cells[0]);
  5.         std::unordered_set<int> unused;
  6.         for (size_t r = 1; r < cells.size(); ++r) {
  7.             unused.insert(r);
  8.         }
  9.         for (int k = 1; k < R * C; ++k) {
  10.             int id = -1;
  11.             for (auto x : unused) {
  12.                 if (cells[x].first != path.back().first &&
  13.                     cells[x].second != path.back().second &&
  14.                     cells[x].first - cells[x].second != path.back().first - path.back().second &&
  15.                     cells[x].first + cells[x].second != path.back().first + path.back().second) {
  16.                     id = x;
  17.                     break;
  18.                 }
  19.             }
  20.             if (id == -1) {
  21.                 break;
  22.             }
  23.             path.push_back(cells[id]);
  24.             unused.erase(id);
  25.         }
  26.         if (path.size() == R * C) {
  27.             //cerr << i << endl;
  28.             cout << "POSSIBLE\n";
  29.             for (auto x : path) {
  30.                 cout << x.first + 1 << ' ' << x.second + 1 << '\n';
  31.             }
  32.             return;
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment