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.Println("##")
			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++: //Name: Spiral Pattern
//Level: 2
//Category: シミュレーション
//Note: 

/**
 * n×nの領域を用意して、左下隅から渦巻きをたどるようにして埋めていく。
 * 現在の進行方向について、壁にぶつかるときか、もう1歩進むと他の線に干渉するというときには右に曲がる。
 */
#include <iostream>
#include <vector>
#include <string>

using namespace std;

// ostream support for vector.
template<typename T>
ostream& operator <<(ostream &os, const vector<T> &v) {
    for(const T &t : v) {
        os << t << endl;
    }
    return os;
}

template<typename T>
struct Pair {
    T a, b;

    Pair() : a(T()), b(T()) {}
    Pair(const T &a, const T &b) : a(a), b(b) {}

    Pair& operator += (const Pair &p) {
        a += p.a;
        b += p.b;
        return *this;
    }

    friend Pair operator +(Pair p1, const Pair &p2) {
        p1 += p2;
        return p1;
    }

    friend ostream& operator <<(ostream &os, const Pair &p) {
        return os << "(" << p.a << ", " << p.b << ")";
    }

    /* vector accessor mixin {{{*/
    template<typename U>
    auto of(U &u) const -> decltype(u[a][b]) {
        return u[a][b];
    }

    template<typename U>
    bool valid_in(const U &u) const {
        return a < u.size() && b < u[0].size();
    }
    /*}}}*/
};

static const Pair<int> DELTA[4] = {
    Pair<int>(-1, 0), Pair<int>(0, -1), Pair<int>(1, 0), Pair<int>(0, 1)
};

bool solve(bool first) {
    if(!first) cout << endl;
    int N;
    cin >> N;

    vector<string> f(N, string(N, ' '));
    Pair<int> pos(N-1, 0);
    int dir = 1;
    bool stucked = false;
    while(true) {
        pos.of(f) = '#';
        auto next = pos + DELTA[dir];
        auto nn = next + DELTA[dir];
        auto nl = next + DELTA[(dir+3)%4];
        if(nl.valid_in(f) && nl.of(f) == '#') {
            break;
        }
        if(!next.valid_in(f) || (nn.valid_in(f) && nn.of(f) == '#')) {
            if(stucked) break;
            dir = (dir + 3) % 4;
            stucked = true;
        } else {
            stucked = false;
            pos = next;
        }
    }
    cout << f;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(2);

    bool first = true;
    int N;
    cin >> N;
    while(N-- && solve(first)) {
        first = false;
    }
    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
		} // end if
	} // end whlie

	return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

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

inline int next(int d)
{
    return (d + 1) % 4;
}

int main()
{
    int dataset, n;

    cin >> dataset;
    while (dataset--) {
        cin >> n;

        if (n == 1) {
            cout << "#\n";
            continue;
        }
        else if (n == 2) {
            cout << "# \n# \n";
            continue;
        }
        else if (n == 3) {
            cout << "###\n# #\n# #\n";
            continue;
        }

        vector<vector<bool> > map(n, vector<bool>(n, false));
        int d = 0, x = 0, y = n - 1;
        bool flag = true;

        while (flag) {
            map[y][x] = true;

            int nx = x + dx[d];
            int ny = y + dy[d];

            if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
                d = next(d);
                x += dx[d];
                y += dy[d];
                continue;
            }

            for (int i = 0; i < 4; ++i) {
                if ((i + 2) % 4 == d)
                    continue;
                int nnx = nx + dx[i];
                int nny = ny + dy[i];

                if (nnx < 0 || nnx >= n || nny < 0 || nny >= n)
                    continue;
                if (map[nny][nnx]) {
                   d =  next(d);
                   break;
                }
            }
            x += dx[d];
            y += dy[d];

            for (int i = 0; i < 4; ++i) {
                if ((i + 2) % 4 == d)
                    continue;
                nx = x + dx[i];
                ny = y + dy[i];

                if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                    continue;
                if (map[ny][nx]) {
                    flag = false;
                    break;
                }
            }
        }

        // Output
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (map[i][j])
                    cout << "#";
                else
                    cout << " ";
            }
            cout << "\n";
        }
    }

    return 0;
}