Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "dungeon.h"
  4.  
  5. #include <stdexcept>
  6. #include <unordered_set>
  7. #include <queue>
  8.  
  9. Room* FindFinalRoom(Room* starting_room) {
  10. std::unordered_set<std::string> keys;
  11. std::unordered_set<Door*> doors;
  12. std::unordered_set<Room*> used_rooms;
  13. std::queue<Room*> queue;
  14. queue.push(starting_room);
  15. used_rooms.insert(starting_room);
  16. Room* result = nullptr;
  17. while (!queue.empty() && result == nullptr) {
  18. Room* cur_room = queue.front();
  19. queue.pop();
  20. if (cur_room->IsFinal()) {
  21. result = cur_room;
  22. break;
  23. }
  24. for (size_t i = 0; i < cur_room->NumKeys(); ++i) {
  25. std::string key = cur_room->GetKey(i);
  26. keys.insert(key);
  27. for (auto& door : doors) {
  28. door->TryOpen(key);
  29. if (door->IsOpen()) {
  30. if (used_rooms.find(door->GoThrough()) == used_rooms.end()) {
  31. queue.push(door->GoThrough());
  32. used_rooms.insert(door->GoThrough());
  33. }
  34. doors.erase(door);
  35. }
  36. }
  37. }
  38. for (size_t i = 0; i < cur_room->NumDoors(); ++i) {
  39. Door* cur_door = cur_room->GetDoor(i);
  40. for (auto& key : keys) {
  41. cur_door->TryOpen(key);
  42. }
  43. if (cur_door->IsOpen()) {
  44. if (used_rooms.find(cur_door->GoThrough()) == used_rooms.end()) {
  45. queue.push(cur_door->GoThrough());
  46. used_rooms.insert(cur_door->GoThrough());
  47. }
  48. } else {
  49. doors.insert(cur_door);
  50. }
  51. }
  52.  
  53. }
  54. return result;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement