--- title: "Maze Solution with CA" type: "article" slug: "maze-solution-with-ca" url: "http://localhost/article/maze-solution-with-ca/" markdown_url: "http://localhost/article/maze-solution-with-ca.md" published_at: "2025-11-27T13:48:32+00:00" modified_at: "2026-06-21T23:24:10+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "I've been playing with Cellular Automata (CA) since College. (See QHJ #4 for a more in depth discussion of CA) I've seem some articles that talk about how CA can be used in Physics and Chemistry to simulate various chemical reactions and particle simulations. I have not seen any fairly practical applications that can be…" category: - name: "QL Hacker's Journal" slug: "ql-hackers-journal" taxonomy: "category" url: "http://localhost/category/periodicals/ql-hackers-journal/" post_tag: - name: "C (programming language)" slug: "c-programming-language" taxonomy: "post_tag" url: "http://localhost/tag/c-programming-language/" - name: "Full Text" slug: "fulltext" taxonomy: "post_tag" url: "http://localhost/tag/fulltext/" - name: "QL" slug: "ql" taxonomy: "post_tag" url: "http://localhost/tag/ql/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" model: - name: "Cambridge Z88" slug: "cambridge-z88" taxonomy: "model" url: "http://localhost/model/cambridge-z88/" - name: "Sinclair QL" slug: "sinclair-ql" taxonomy: "model" url: "http://localhost/model/sinclair-ql/" indiv: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" publication_r: id: 33686 title: "QL Hacker’s Journal" type: "periodical" url: "http://localhost/periodical/ql-hackers-journal/" authors_r: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" issue: "12" issues_articles: - id: 61548 title: "QL Hacker’s Journal 12" type: "issue" url: "http://localhost/issue/ql-hackers-journal-12/" pubdate: "January 1993" archive_link: true --- # Maze Solution with CA I’ve been playing with Cellular Automata (CA) since College. (See QHJ #4 for a more in depth discussion of CA) I’ve seem some articles that talk about how CA can be used in Physics and Chemistry to simulate various chemical reactions and particle simulations. I have not seen any fairly practical applications that can be easy demonstrated to show how useful CA can be. That was before I read an article in the most recent issue of “Dr Dobb’s Journal.” Basem Nayfeb of Stanford University gives a fresh approach to finding the solution to a maze. Past algorithms for solving mazes were brute force approaches to solve the maze just like a mouse. Be recursively searching all possible paths the solution will be eventually found. This takes time and lots of memory for stack space. Basem Nayfeb used CA to solve the problem with no extra extra memory needed. Given a maze stored in an array. Cells with a wall have a value of 1, free cells are 0. In the maze possible moves are north, west, east, and south. This translates to a neighborhood of the 4 cells that share a side with a cell. Basem defines a counting rule that states: A free cell will become a wall cell if there are three or more walls in its neighborhood. The only cells that have 3 or more walls nearby are obviously dead-ends. Over time all dead-ends will convert to walls and only the true path(s) will stay free of walls. By defining a simple neighborhood and that one simple rule, Basem has defined a CA that will fairly quickly find the solution to the maze. Below is the a SuperBasic program that implements the algorithm. You can see how short and simple it is. Consider how much code it would take to solve the problem the recursive way. ``` ** Maze_ssb ** Tim Swenson ** ** Based on an algorithm by Basem A. Nayfeb ** Dr Dobb's Journal January 1993 ** wall = 1 free = 0 ** My example maze is 16x16. Change ** to what ever you need. maze_x = 16 maze_y = 16 DIM maze(maze_x,maze_y) read_file display REPeat loop eval dislay IF change = 0 THEN EXIT loop END REPeat loop DEFine PROCedure read_file OPEN_IN #4,flp1_maze_data FOR y = 1 TO maze_y INPUT #4,in$ FOR x = 1 TO maze_x maze(x,y) = in$(x) END FOR x END FOR y CLOSE #4 END DEFine read_file DEFine PROCedure eval LOCal count change = 0 FOR y = 2 TO maze_y-1 FOR x = 2 TO maze_x-1 count = 0 IF maze(x,y+1) = wall THEN count=count+1 IF maze(x+1,y) = wall THEN count=count+1 IF maze(x,y-1) = wall THEN count=count+1 IF maze(x-1,y) = wall THEN count=count+1 IF count >= 3 THEN maze(x,y) = wall change = 1 END IF END FOR x END FOR y END DEFine eval DEFine PROCedure display CLS FOR y = 1 TO maze_y FOR x = 1 TO maze_x IF maze(x,y) = wall THEN PRINT "*"; ELSE PRINT " "; END IF END FOR x PRINT END FOR y PAUSE 4E4 END DEFine display ``` Here is an example data file that I used in my testing of the program: ``` 1111111111111111 1000000010000101 1010111010110101 1010101010110101 1010101010110101 1010101010111101 1110101010110001 0000101000110100 1110101010110101 1010101010110101 1010101010110101 1010101010110101 1010100010110101 1011111110111101 1000000000000001 1111111111111111 ```