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++: #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++){
        resetDx();
        cin >>d;
        if(d == 2){
            make2();
        } else{
        makeDx();
        }
    }
    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){
                cout <<' ';
            } else{
                cout <<'#';
            }
        }
        cout <<endl;
    }
}

C++: #include <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

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

int main()
{
  rep(i, in()){
    int field[128][128] = {};
    int n = in();
    if (i != 0) puts("");
    rep(i, n + 1){
      field[0][i] = 1;
      field[n + 1][i] = 1;
      field[i][0] = 1;
      field[i][n + 1] = 1;
    }

    int x, y, d;
    x = 1;
    y = n;
    d = 0;
    while (1){
      field[y][x] = 2;
      int ny, nx;
      nx = x + dx[d];
      ny = y + dy[d];
      if (field[ny][nx] > 0){
        d = (d + 1) % 4;
        nx = x + dx[d];
        ny = y + dy[d];
        if (field[ny][nx] > 0){
          break;
        }
      }
      rep(i, 4){
        int nnx, nny;
        nnx = x + dx[i];
        nny = y + dy[i];
        if (nnx == nx && nny == ny) continue;
        if (field[nny][nnx] != 0) continue;
        field[nny][nnx] = 1;
      }
      x = nx;
      y = ny;
    }
    lep(i, n){
      lep(j, n){
        printf("%c", " #"[field[i][j] == 2]);
      }
      puts("");
    }
  }
  return 0;
}

C++: #include<iostream>
#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]; //上右下左
  cin >> d;
  while(d--){
    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;
    }
    cout << endl;
  }
}