Browse code

Remember last restart position

Robert Cranston authored on 21/05/2025 15:15:40
Showing 2 changed files

... ...
@@ -10,7 +10,7 @@ Features:
10 10
     -   Hunt And Kill
11 11
         -   Handles mazes of any size
12 12
         -   Random with seed (time in seconds since the epoch by default)
13
-        -   Fairly slow (1000*1000 cells in ~4000ms)
13
+        -   Fairly fast (10000*10000 cells in ~1200ms)
14 14
 
15 15
 [`cxx-maze`]: https://git.rcrnstn.net/rcrnstn/cxx-maze
16 16
 
... ...
@@ -19,7 +19,7 @@ Features:
19 19
 ```
20 20
 $ make
21 21
 $ ./maze dummy
22
-3844ms
22
+1264ms
23 23
 $ ./maze
24 24
 ██  ████████████████████████████████████████████████████████████████████████████
25 25
 ██  ██                          ██              ██      ██          ██      ████
... ...
@@ -56,8 +56,8 @@ Maze hunt_and_kill(int w, int h, unsigned seed, C callback = [](Maze &){})
56 56
     // Entrance.
57 57
     maze(1, 0) = maze(1, 1) = Maze::PATH;
58 58
     // Interior.
59
-    auto cx = 1;
60
-    auto cy = 1;
59
+    auto cx = 1; auto rx = 1;
60
+    auto cy = 1; auto ry = 1;
61 61
     auto done = false;
62 62
     while (!done)
63 63
     {
... ...
@@ -86,19 +86,21 @@ Maze hunt_and_kill(int w, int h, unsigned seed, C callback = [](Maze &){})
86 86
             continue;
87 87
         }
88 88
         // No neighbors, done unless there's an uncarved cell to restart at.
89
-        done = true;
90
-        for (auto ry = 1; ry < h && done; ry += 2)
91
-        for (auto rx = 1; rx < w && done; rx += 2)
92
-            if (maze(rx, ry) == Maze::WALL)
93
-                for (auto ny = -2; ny <= 2 && done; ny += 2)
94
-                for (auto nx = -2; nx <= 2 && done; nx += 2)
95
-                    if (!nx != !ny)
96
-                    if (maze(rx+nx, ry+ny) == Maze::PATH)
97
-                    {
98
-                        cx = rx+nx;
99
-                        cy = ry+ny;
100
-                        done = false;
101
-                    }
89
+        done = [&]() {
90
+            for (; ry < h; ry += 2, rx = 1)
91
+            for (; rx < w; rx += 2)
92
+                if (maze(rx, ry) == Maze::WALL)
93
+                    for (auto ny = -2; ny <= 2; ny += 2)
94
+                    for (auto nx = -2; nx <= 2; nx += 2)
95
+                        if (!nx != !ny)
96
+                        if (maze(rx+nx, ry+ny) == Maze::PATH)
97
+                        {
98
+                            cx = rx+nx;
99
+                            cy = ry+ny;
100
+                            return false;
101
+                        }
102
+            return true;
103
+        }();
102 104
     }
103 105
     // Exit.
104 106
     w -= (w + 1) % 2;
... ...
@@ -121,7 +123,7 @@ int main(int argc, char const * [])
121 123
     else
122 124
     {
123 125
         auto const t1 = steady_clock::now();
124
-        hunt_and_kill(1000, 1000, 0);
126
+        hunt_and_kill(10000, 10000, 0);
125 127
         auto const t2 = steady_clock::now();
126 128
         std::cout << duration_cast<milliseconds>(t2 - t1).count() << "ms\n";
127 129
     }