Task: Maze generation

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

#define DOUBLE_SPACE 1

#if DOUBLE_SPACE
#	define SPC "　"
#else
#	define SPC " "
#endif

wchar_t glyph[] = L""SPC"│││─┘┐┤─└┌├─┴┬┼"SPC"┆┆┆┄╯╮ ┄╰╭ ┄";

typedef unsigned char byte;
enum { N = 1, S = 2, W = 4, E = 8, V = 16 };

byte **cell;
int w, h, avail;
#define each(i, x, y) for (i = x; i <= y; i++)

int irand(int n)
{
	int r, rmax = n * (RAND_MAX / n);
	while ((r = rand()) >= rmax);
	return r / (RAND_MAX/n);
}

void show()
{
	int i, j, c;
	each(i, 0, 2 * h) {
		each(j, 0, 2 * w) {
			c = cell[i][j];
			if (c > V) printf("\033[31m");
			printf("%lc", glyph[c]);
			if (c > V) printf("\033[m");
		}
		putchar('\n');
	}
}

inline int max(int a, int b) { return a >= b ? a : b; }
inline int min(int a, int b) { return b >= a ? a : b; }

static int dirs[4][2] = {{-2, 0}, {0, 2}, {2, 0}, {0, -2}};
void walk(int x, int y)
{
	int i, t, x1, y1, d[4] = { 0, 1, 2, 3 };

	cell[y][x] |= V;
	avail--;

	for (x1 = 3; x1; x1--)
		if (x1 != (y1 = irand(x1 + 1)))
			i = d[x1], d[x1] = d[y1], d[y1] = i;

	for (i = 0; avail && i < 4; i++) {
		x1 = x + dirs[ d[i] ][0], y1 = y + dirs[ d[i] ][1];

		if (cell[y1][x1] & V) continue;

		
		if (x1 == x) {
			t = (y + y1) / 2;
			cell[t][x+1] &= ~W, cell[t][x] &= ~(E|W), cell[t][x-1] &= ~E;
		} else if (y1 == y) {
			t = (x + x1)/2;
			cell[y-1][t] &= ~S, cell[y][t] &= ~(N|S), cell[y+1][t] &= ~N;
		}
		walk(x1, y1);
	}
}

int solve(int x, int y, int tox, int toy)
{
	int i, t, x1, y1;

	cell[y][x] |= V;
	if (x == tox && y == toy) return 1;

	each(i, 0, 3) {
		x1 = x + dirs[i][0], y1 = y + dirs[i][1];
		if (cell[y1][x1]) continue;

		
		if (x1 == x) {
			t = (y + y1)/2;
			if (cell[t][x] || !solve(x1, y1, tox, toy)) continue;

			cell[t-1][x] |= S, cell[t][x] |= V|N|S, cell[t+1][x] |= N;
		} else if (y1 == y) {
			t = (x + x1)/2;
			if (cell[y][t] || !solve(x1, y1, tox, toy)) continue;

			cell[y][t-1] |= E, cell[y][t] |= V|E|W, cell[y][t+1] |= W;
		}
		return 1;
	}

	
	cell[y][x] &= ~V;
	return 0;
}

void make_maze()
{
	int i, j;
	int h2 = 2 * h + 2, w2 = 2 * w + 2;
	byte **p;

	p = calloc(sizeof(byte*) * (h2 + 2) + w2 * h2 + 1, 1);

	p[1] = (byte*)(p + h2 + 2) + 1;
	each(i, 2, h2) p[i] = p[i-1] + w2;
	p[0] = p[h2];
	cell = &p[1];

	each(i, -1, 2 * h + 1) cell[i][-1] = cell[i][w2 - 1] = V;
	each(j, 0, 2 * w) cell[-1][j] = cell[h2 - 1][j] = V;
	each(i, 0, h) each(j, 0, 2 * w) cell[2*i][j] |= E|W;
	each(i, 0, 2 * h) each(j, 0, w) cell[i][2*j] |= N|S;
	each(j, 0, 2 * w) cell[0][j] &= ~N, cell[2*h][j] &= ~S;
	each(i, 0, 2 * h) cell[i][0] &= ~W, cell[i][2*w] &= ~E;

	avail = w * h;
	walk(irand(2) * 2 + 1, irand(h) * 2 + 1);

	
	each(i, 0, 2 * h) each(j, 0, 2 * w) cell[i][j] &= ~V;
	solve(1, 1, 2 * w - 1, 2 * h - 1);

	show();
}

int main(int c, char **v)
{
	setlocale(LC_ALL, "");
	if (c < 2 || (w = atoi(v[1])) <= 0) w = 16;
	if (c < 3 || (h = atoi(v[2])) <= 0) h = 8;

	make_maze();

	return 0;
}







Python: g_size = 10
background_color = color(80, 80, 220)
runner = color(255, 50, 50)
visited_color = color(220, 240, 240)
done_color = color(100, 160, 250)

def setup():
    global cell, done, visit, run_cell, c_size
    size(600, 600)
    frameRate(20)
    smooth(4)
    strokeCap(ROUND)
    c_size = max(width / g_size, height / g_size)
    cell = [[None] * g_size for _ in range(g_size)]
    
    for i in range(g_size):
        for j in range(g_size):
            cell[i][j] = Cell(i, j)

    for i in range(g_size):
        for j in range(g_size):
            cell[i][j].add_neighbor()

    run_cell = cell[0][0]
    visit, done = [], []
    visit.append(run_cell)


def draw():
    global run_cell
    
    background(background_color)
    
    for i in range(g_size):
        for j in range(g_size):
            cell[i][j].draw_cell()
            cell[i][j].draw_wall()

    if len(visit) < g_size * g_size:
        if run_cell.check_sides():
            chosen = run_cell.pick_neighbor()
            done.append(run_cell)
            run_cell.stacked = True
            if chosen.i - run_cell.i == 1:
                run_cell.wall[1] = False
                chosen.wall[3] = False
            elif chosen.i - run_cell.i == -1:
                run_cell.wall[3] = False
                chosen.wall[1] = False
            elif chosen.j - run_cell.j == 1:
                run_cell.wall[2] = False
                chosen.wall[0] = False
            else:
                run_cell.wall[0] = False
                chosen.wall[2] = False
            run_cell.current = False
            run_cell = chosen
            run_cell.current = True
            run_cell.visited = True
        elif done:
            run_cell.current = False
            run_cell = done.pop()
            run_cell.stacked = False
            run_cell.current = True


class Cell:

    def __init__(self, i, j):
        self.i = i
        self.j = j
        self.wall = [True, True, True, True]
        self.visited = False
        self.stacked = False
        self.current = False

    def pick_neighbor(self):
        from random import choice
        unvisited = [nb for nb in self.neighbor
                          if nb.visited == False]
        return choice(unvisited)

    def add_neighbor(self):
        i, j = self.i, self.j
        neighbor = []
        if i > 0:
            neighbor.append(cell[i - 1][j])
        if i < g_size - 1:
            neighbor.append(cell[i + 1][j])
        if j > 0:
            neighbor.append(cell[i][j - 1])
        if j < g_size - 1:
            neighbor.append(cell[i][j + 1])
        self.neighbor = neighbor

    def check_sides(self):
        for nb in self.neighbor:
            if not nb.visited:
                return True
        return False

    def draw_cell(self):
        s = c_size
        noStroke()
        noFill()
        if self.current:
            fill(runner)
        elif self.stacked:
            fill(done_color)
        elif self.visited:
            fill(visited_color)
        rect(self.j * s, self.i * s, s, s)

    def draw_wall(self):
        i, j = self.i, self.j
        wall = self.wall
        stroke(0)
        strokeWeight(5)
        if wall[0]: line(j * c_size, i * c_size, j * c_size, (i + 1) * c_size)
        if wall[1]: line(j * c_size, (i + 1) * c_size, (j + 1) * c_size, (i + 1) * c_size)
        if wall[2]: line((j + 1) * c_size, (i + 1) * c_size, (j + 1) * c_size, i * c_size)
        if wall[3]: line((j + 1) * c_size, i * c_size, j * c_size, i * c_size)