Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	out := make([][]string, 100)
	for i := range out {
		out[i] = make([]string, 101)
	}
	dx := []int{0, 1, 0, -1}
	dy := []int{-1, 0, 1, 0}
	scanner.Scan()
	a, _ := strconv.Atoi(scanner.Text())
	for a > 0 {
		a--
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				out[i][j] = " "
			}
			out[i][b] = "0"
		}
		if b == 1 {
			fmt.Println("#")
			if a != 0 {
				fmt.Println()
			}
			continue
		}
		if b == 2 {
			fmt.Printf("##\n# \n")
			if a != 0 {
				fmt.Println()
				continue
			}
		}
		row := b -1
		col := 0
		dir := 0
		l := 0
		out[row][col] = "#"
		for {
			nrow := row+dy[dir%4]
			ncol := col+dx[dir%4]
			if dir%4 == 0 && nrow == 0 || dir%4==3&&ncol==0 || dir%4==2&&nrow==b-1 || dir%4==1&&ncol==b-1 {
				row=nrow
				col=ncol
				out[row][col]="#"
				dir++
				l = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if l < 3 {
					break
				}
				dir++
				l = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				l++
			}
		}
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				fmt.Printf("%s", out[i][j])
			}
			fmt.Println()
		}
		if a != 0 {
			fmt.Println()
		}
	}
}

C++: #include <iostream>
using namespace std;

int main()
{
	int d;

	while( cin >> d ){
		for( int i = 0; i < d; ++i ){
			int n;
			cin >> n;

			int size = n + 4;
			char* map = new char[ size * size ]();
			for( int j = 0; j < size; ++j ){
				for( int k = 0; k < size; ++k ){
					map[ j * size + k ] = ' ';
				}
			}
			for( int j = 0; j < size; ++j ){
				map[ j ] = map[ ( size - 1 ) * size + j ] = map[ size * j ] = map[ ( j + 1 ) * size - 1 ] = '#';
			}
			for( int j = 2; j < size - 2; ++j ){
				map[ j * size + 2 ] = '#';
			}
			int x = 2, y = 2;
			int dir = 1;
			int dirCount = 0;
			int straightCount = 0;
			while( dirCount < 2 ){
				map[ y * size + x ] = '#';
				++straightCount;
				int nx, ny;
				switch( dir ){
					case 0: nx = x; ny = y - 2; break;
					case 1: nx = x + 2; ny = y; break;
					case 2: nx = x; ny = y + 2; break;
					case 3: nx = x - 2; ny = y; break;
				}
				if( map[ ny * size + nx ] == '#' ){
					if( straightCount < 3 ){
						break;
					}
					dir = ( dir + 1 ) % 4;
					++dirCount;
					straightCount = 0;
					continue;
				}else{
					dirCount = 0;
				}
				switch( dir ){
					case 0: y -= 1; break;
					case 1: x += 1; break;
					case 2: y += 1; break;
					case 3: x -= 1; break;
				}
			}

			for( int p = 2; p < size - 2; ++p ){
				for( int q = 2; q < size - 2; ++q ){
					cout << map[ p * size + q ];
				}
				cout << endl;
			}

			delete[] map;
		}
	}

	return 0;
}

C++: #include <stdio.h>
#pragma warning(disable : 4996)
int n, d, x, y, e, f; char c[100][101];
const int dx[4] = { 0, 1, 0, -1 };
const int dy[4] = { -1, 0, 1, 0 };
bool ok() {
	for (int i = 0; i < 4; i++) {
		if (i == (e + 2) % 4) continue;
		int ex = x + dx[i];
		int ey = y + dy[i];
		if (0 <= ex && ex < d && 0 <= ey && ey < d) {
			if (c[ey][ex] == '#') {
				return false;
			}
		}
	}
	return true;
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &d);
		x = 0, y = d - 1, e = 0, f = 0;
		for (int j = 0; j < d; j++) {
			for (int k = 0; k < d; k++) {
				c[j][k] = ' ';
			}
			c[j][d] = '\0';
		}
		while (f++ < d * d) {
			c[y][x] = '#';
			x += dx[e % 4];
			y += dy[e % 4];
			if (0 <= x && x < d && 0 <= y && y < d) {
				if (!ok()) {
					x -= dx[e % 4];
					y -= dy[e % 4];
					e++;
				}
			}
			else {
				x -= dx[e % 4];
				y -= dy[e % 4];
				e++;
			}
		}
		for (int i = 0; i < d; i++) printf("%s\n", c[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>

int d,n;
int fie[101][101];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};

int max(int a,int b){
	if(a>b)return a;
	return b;
}

int main(void){
	scanf("%d",&d);
	for(int data=0;data<d;data++){
		if(data>0)printf("\n");
		scanf("%d",&n);
		memset(fie,0,sizeof(fie));
		for(int i=0;i<n;i++){
			fie[0][i]=1;
		}
		int len=0,mlen=n-1,x=0,y=0,dir=1,cnt=0;
		while(mlen>0){
			x+=dx[dir];
			y+=dy[dir];
			fie[x][y]=1;
			len++;
			if(len==mlen){
				cnt++;
				if(mlen==1)break;
				dir=(dir+1)%4;
				len=0;
			}
			if(cnt==2){
				mlen-=2;
				cnt=0;
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				printf("%c",fie[j][i]==1?'#':' ');
			}
			printf("\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstring>
#define rep(i,n) for(int i=0;i<(n);i++)
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LIFT 3

using namespace std;

bool p[100][100];

void dfs (int len, int dir, int r, int c, int cnt )
{
	if (len <= 1 )
		return;

	dir = (dir + 1) % 4;
	if (cnt != 0 && cnt % 2 == 0 ){
		len -= 2;
	} // end if 

	int cr = r, cc = c;
	switch (dir ){
		case UP:
			for (int i = 0; i < len; i++, cr-- )
				p[cr][cc] = true;
			cr++;
			break;
		case DOWN:
			for (int i = 0; i < len; i++, cr++ )
				p[cr][cc] = true;
			cr--;
			break;	
		case RIGHT:
			for (int j = 0; j < len; j++, cc++ )
				p[cr][cc] = true;
			cc--;
			break;
		case LIFT:
			for (int j = 0; j < len; j++, cc-- )
				p[cr][cc] = true;
			cc++;
			break;
	} // end switch

	if (len == 2 )
		return;

	dfs (len, dir, cr, cc, cnt + 1 );
}

void disp_p (int m )
{
	rep (i, m ){
		rep (j, m ){
			if (p[i][j] )
				cout << '#';
			else
				cout << ' ';
		}// end rep
		cout << endl;
	} // end rep
}

int main()
{
	int n;

	while (cin >> n && n ){
		for (int i = 0; i < n; i++ ){
			int m;
			cin >> m;
			if (m == 1 ){
				cout << "#" << endl;
			}else if (m == 2 ){
				cout << "##" << endl;
				cout << "# " << endl;
			}else{
				memset (p, false, sizeof (p ) );
				for (int r = m - 1; r >= 0; r-- )
					p[r][0] = true;
				for (int c = 0; c < m; c++ )
					p[0][c] = true;
				for (int r = 0; r < m; r++ )
					p[r][m-1] = true;
				dfs (m-2, DOWN, m-1, m-1, 0 );
				disp_p (m );
			} // end if
			if (i != n - 1 ){
				cout << endl;
			} // end if
		} // end if
	} // end whlie

	return 0;
}

C++: d = gets.to_i
d.times do |i|
    n = gets.to_i
    a = Array.new(n).map { Array.new(n, 0) }
    a.map! {|r| [9] + r + [9]}
    a.unshift [9] * (n + 2)
    a<< [9] * (n + 2)
    y = n
    x = 1
    dy = -1
    dx = 0
    loop do
        a[y][x] = 1
        if a[y + dy][x + dx] == 0
            a[y + dx][x - dy] = 9
            y += dy
            x += dx
        else
            dy, dx = dx, -dy
            if a[y + dy][x + dx] == 0
                a[y - dx][x + dy] = 9
                y += dy
                x += dx
            else
                break
            end
        end
    end
    a[1..-2].each do |r|
        puts r.map {|x| x == 1 ? '#' : ' '} * ''
    end
    puts if i < d - 1
end

C++: #include<iostream>
using namespace std;

#define MAX_N 3000

int x[MAX_N][MAX_N];
int n, a, cx, cy, cnt;

int dy[4] = { -1,0,1,0 };
int dx[4] = { 0,1,0,-1 };

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cnt = 0;
		cin >> a;
		for (int j = 0; j < MAX_N; j++) {
			for (int k = 0; k < MAX_N; k++) {
				x[i][j] = 0;
			}
		}
		cx = 0; cy = a - 1;
		x[cy][cx] = 1;
		while (cnt < a) {
			while (true) {
				int Y1 = cy + dy[cnt % 4];
				int X1 = cx + dx[cnt % 4];
				int Y2 = cy + dy[cnt % 4] * 2;
				int X2 = cx + dx[cnt % 4] * 2;
				if (Y1 >= 0 && Y1 < a && X1 >= 0 && X1 < a) {
					if (Y2 >= 0 && Y2 < a && X2 >= 0 && X2 < a) {
						if (x[Y2][X2] == 1) {
							break;
						}
					}
				}
				else {
					break;
				}
				cx = X1; cy = Y1;
				x[cy][cx] = 1;
			}
			cnt++;
		}
		for (int j = 0; j < a; j++) {
			for (int k = 0; k < a; k++) {
				if (x[j][k] == 1) {
					cout << '#';
				}
				else {
					cout << ' ';
				}
			}
			cout << endl;
		}
	}
}