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;
#define MAX_D 101


bool dx[MAX_D][MAX_D];

void resetDx();
void makeDx();
void dispDx();
bool checkField(int n);
void make2();
int d;

int main(){
    int n;
    cin >>n;
    for(int i = 0; i < n; i++){
        if(i != 0) cout <<endl;
        resetDx();
        cin >>d;
        if(d == 2){
            make2();
        } else{
        makeDx();
        }
        /*for(int i = 0; i < d; i++){
            for(int j = 0; j < d; j++){
                cout <<dx[i][j];
            }
            cout <<endl;
        }*/
    }
    return 0;
}

void resetDx(){
    for(int i = 0; i < MAX_D; i++){
        for(int j = 0; j < MAX_D; j++){
            dx[i][j] = false;
        }
    }
}

void dispDx(){
    for(int i = 0; i < d; i++){
        for(int j = 0; j < d; j++){
            if(dx[i][j] == true) cout <<'#';
            else cout <<' ';
        }
        cout <<endl;
    }
}

void makeDx(){
    for(int i = 0; i < d; i++){
        dx[i][0] = true;
        dx[0][i] = true;
        dx[i][d - 1] = true;
    }
    int x = d - 1, y = d - 1, mx[4] = {-1, 0, 1, 0}, my[4] = {0, -1, 0, 1}, i = 0;
    bool moveFlg = false;
    while(true){
        while(checkField(x + mx[i]) && checkField(y + my[i]) && !dx[y + my[i] + my[i]][x + mx[i] + mx[i]]){
            if(!moveFlg) moveFlg = true;
            dx[y + my[i]][x + mx[i]] = true;
            x += mx[i]; y += my[i];
        }
        if(!moveFlg) break;
        //cout <<"now = " <<y <<", " <<x <<" iBefore = " <<i <<endl;
        i = (i + 1) % 4;
        int conti = (i + 2) % 4;
        int ny = y + my[i], nx = x + mx[i];
        bool endFlg = false;
        //cout <<"ny = " <<y <<"+" <<my[i] <<"=" <<ny <<", nx = " <<x <<"+" <<mx[i] <<"=" <<nx <<endl;
        for(int j = 0; j < 4; j++){
            if(j == conti) continue;
            else{
                if(checkField(ny + my[j]) && checkField(nx + mx[j])){
                    //cout <<"j = " <<j <<" ny + my[j] = " <<ny + my[j] <<", nx + mx[j] = " <<nx + mx[j];
                    if(dx[ny + my[j]][nx + mx[j]]){
                        //cout <<" true" <<endl;
                        endFlg = true;
                        break;
                    } else{
                        //cout <<" false" <<endl;
                    }
                }
            }
        }
        if(endFlg){
            break;
        }
        //dispDx();
    }
    dispDx();
}

bool checkField(int n){
    bool ret = true;
    if(n < 0 || n >= d) ret = false;
    return ret;
}

void make2(){
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            if(i == 1 && j == 1){
                dx[i][j] = false;
            } else{
                dx[i][j] = true;
            }
        }
    }
    dispDx();
}

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

#define N 100
#define rep(i, n) for(int i=0; i<n; ++i)

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

bool inRange(int x, int a, int b) {
	return a<=x && x<=b;
}

void drawSpiral(int n) {
	char mat[N][N];
	rep(i, n) rep(j, n) mat[i][j] = ' ';
	int x = 0, y = n-1, dir = 1, s = 0, mv = n;
	mat[y][x] = '#';
	while(1) {
		for(int i=0; i<mv-1; ++i) {
			x += dx[dir]; y += dy[dir];
			mat[y][x] = '#';
		}
		if(mv<=2) break;
		if(s>0 && s%2==0) {
			mv -= 2;
			if(mv<=1) break;
		}
		dir = (dir + 1) % 4;
		++s;
	}
	rep(i, n) {
		rep(j, n) cout << mat[i][j];
		cout << endl;
	}
}

int main() {
	int d, n, count = 0;
	cin >> d;
	while(d--) {
		if(count++>0) cout << endl;
		cin >> n;
		drawSpiral(n);
	}
	return 0;
}

C++: // 移動ベクトルという概念
// 今埋めようとしている周囲に#が1個のみならば埋める、
// そうでなければ方向転換.方向転換後1つも埋められないなら終わり

#include<iostream>
using namespace std;

int dir[4][2] = {{-1,0} , {0,1}, {1,0}, {0,-1}};
char mat[100][100];
int n;

bool ok(int x, int y){
    int count = 0;
    for(int i = 0; i < 4; i++){
        int ni = x + dir[i][0];
        int nj = y + dir[i][1];
        if(0 <= ni && ni < n && 0 <= nj && nj < n && mat[ni][nj] == '#'){
            count++;
        }
    }
    return count <= 1;
}

int main(){
    int d;
    cin >> d;

    for(int c = 0; c < d; c++){
        if(c)   cout << endl;

        cin >> n;

        for(int i = 0; i < 100; i++){
            for(int j = 0; j < 100; j++){
                mat[i][j] = ' ';
            }
        }

        int move = 0;

        int i = n, j = 0;
        while(1){
            int ni = i + dir[move][0];
            int nj = j + dir[move][1];

            if(0 <= ni && ni < n & 0 <= nj && nj < n && ok(ni, nj)){
                mat[ni][nj] = '#';
                i = ni;
                j = nj;
            }else{
                move = (move+1)%4;
                ni = i + dir[move][0];
                nj = j + dir[move][1];
                if(0 <= ni && ni < n & 0 <= nj && nj < n && ok(ni, nj)){
                    mat[ni][nj] = '#';
                    i = ni;
                    j = nj;
                }else{
                    break;
                }
            }
        }

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                cout << mat[i][j];
            }
            cout << endl;
        }
    }

    return 0;
}

C++: #include<queue>
#include<map>
#include<cstring>
using namespace std;
#define fr first
#define sc second
#define INF 100
typedef pair<int,int>Pt;
int main(){
  int d,n,dx[]={-1,0,1,0},dy[]={0,1,0,-1};
  char mas[101][101]; //上右下左
  bool ok = false;
  cin >> d;
  while(d--){
    if(ok) cout << endl;
    memset(mas,' ',sizeof(mas));
    cin >> n;
    for(int i=0,x=n-1,MUKI=0,y=0;i<n;i++,x+=dx[MUKI],y+=dy[MUKI]) mas[x][y] = '#';
    for(int i=0,cnt=n,x=0,y=0,MUKI=1;i<n-1;i++,MUKI=(MUKI+1)%4){
      for(int j=0;j<cnt;j++,x+=dx[MUKI],y+=dy[MUKI]) mas[x][y] = '#';
      x-=dx[MUKI],y-=dy[MUKI];
      if(i%2==1)cnt-=2;
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++) cout << mas[i][j];
      cout << endl;
    }
    ok = true;
  }
}