Advertisement
Guest User

Solution for "Exploring The Maze

a guest
Jan 11th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include "assert.h"
  2. #include <algorithm>
  3. #include <bitset>
  4. #include <cctype>
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <deque>
  8. #include <functional>
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <map>
  12. #include <queue>
  13. #include <set>
  14. #include <sstream>
  15. #include <stack>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <vector>
  22.  
  23. using namespace std;
  24.  
  25. #define FOR(i, a, b) for(int i = a; i < b ; ++i)
  26.  
  27. #define endl '\n'
  28. template<typename T> struct argument_type;
  29. template<typename T, typename U> struct argument_type<T(U)> { typedef U type; };
  30. #define next(t, i) argument_type<void(t)>::type i; cin >> i;
  31.  
  32. void skip_intro() {
  33.     next(int, n);
  34.     next(int, m);
  35. }
  36.  
  37. void out(string s) {
  38.     cout << s << endl;
  39.     fflush(stdin);
  40. }
  41.  
  42. bool inp() {
  43.     next(string, buf);
  44.     if (buf == "EXIT") {
  45.         exit(0);
  46.     }
  47.     return buf == "OK";
  48. }
  49.  
  50. vector<int> dx = { -1 , 0 , 0 , 1 };
  51. vector<int> dy = { 0 , 1 , -1 , 0 };
  52.  
  53. bool move_forward(int dir) {
  54.     switch(dir) {
  55.         case 0: out("MOVE LEFT"); break;
  56.         case 1: out("MOVE UP"); break;
  57.         case 2: out("MOVE DOWN"); break;
  58.         case 3: out("MOVE RIGHT"); break;
  59.     }
  60.     return inp();
  61. }
  62.  
  63. void move_back() {
  64.     out("BACK");
  65.     assert(inp());
  66. }
  67.  
  68. struct pos {
  69.     int dx, dy, c1;
  70. };
  71.  
  72. bool operator < (const pos &p1 , const pos &p2) {
  73.     if (p1.dx != p2.dx) return p1.dx < p2.dx;
  74.     if (p1.dy != p2.dy) return p1.dy < p2.dy;
  75.     if (p1.c1 != p2.c1) return p1.c1 < p2.c1;
  76.     return false;
  77. }
  78.  
  79. set<pos> visited_positions;
  80.  
  81. // after first 3 moves DSF all (x,y,distance % 4) combinations
  82. void dfs2(pos cur_pos) {
  83.     if (visited_positions.count(cur_pos)) return;
  84.  
  85.     visited_positions.insert(cur_pos);
  86.  
  87.     FOR (dir, 0, 4) {
  88.         {
  89.             auto next_pos = cur_pos;
  90.             next_pos.dx += dx[dir];
  91.             next_pos.dy += dy[dir];
  92.             next_pos.c1++;
  93.             next_pos.c1 &= 3;
  94.  
  95.             if (!visited_positions.count(next_pos) && move_forward(dir)) {
  96.                 dfs2(next_pos);
  97.  
  98.                 move_back();
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104. // DFS all combinations of three moves
  105. void dfs1(int moves_to_make) {
  106.     if (moves_to_make == 0) {
  107.         pos cur_pos = { 0 , 0, 0 };
  108.  
  109.         visited_positions.clear();
  110.  
  111.         dfs2(cur_pos);
  112.     } else {
  113.         FOR (nextDir, 0, 4) {
  114.             if (!move_forward(nextDir)) continue;
  115.            
  116.             dfs1(moves_to_make - 1);
  117.             move_back();
  118.         }
  119.     }
  120. }
  121.  
  122. int main() {
  123.     int n, m;
  124.     skip_intro();
  125.  
  126.     dfs1(3);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement