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<vector>
#include<map>
#include<climits>
#include<set>
#include<queue>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<string>
#include<bitset>
#include<cstring>
#include<list>
using namespace std;
typedef vector<int>VI;
typedef vector<VI>VVI;
typedef vector<string>VS;
typedef pair<int,int>PII;
typedef long long LL;
typedef pair<LL,LL>PLL;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define PB push_back
#define EACH(i,c) for(typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define F first
#define S second

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

char fld[200][200];
int N;
void init(){
    fill(*fld,*fld+40000,' ');
    REP(i,N+4)fld[0][i]=fld[N+3][i]=fld[i][0]=fld[i][N+3]='#';
}

void solve(){
    int x=2,y=N+1,d=1;
    while(true){
        fld[y][x]='#';
        bool f2=false;
        REP(i,4){
            int nd=(d+i)%4;
            int ny=y+dy[nd];
            int nx=x+dx[nd];
            bool flag=true;
            REP(j,4){
                if(j==(nd+2)%4)continue;
                if(fld[ny+dy[j]][nx+dx[j]]=='#')flag=false;
            }
            if(flag){
                x=nx;
                y=ny;
                d=nd;
                f2=true;
                break;
            }

        }
        if(f2==false)return;
    }
}


int main(){
    int n;cin>>n;
    bool flag=false;
    while(n--){
        cin>>N;
        if(flag)cout<<endl;
        else flag=true;
        init();
        solve();
        if(N==2){
            cout<<"##"<<endl;
            cout<<"#"<<endl;
            continue;
        }
        FOR(i,2,N+2){
            FOR(j,2,N+2)cout<<fld[i][j];
            cout<<endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
 
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;

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

void pp(vector<vi> &v){
	rep(i, v.size()){
		rep(j, v.size()){
			cout << v[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;
}

bool check(vector<vi> &v, int nx, int ny, int revdir){
	rep(i, 4){
		if(revdir == i) continue;
		int x = nx+dx[i];
		int y = ny+dy[i];
		if(v[y][x] == 1) return false;
	}
	return true;
}

int main(){
	int d;
	cin >> d;
	rep(k, d){
		if(k)cout << endl;
		int n;
		cin >> n;
		vector<vi> v(n+4, vi(n+4, 0));
		rep(i, n+4) rep(j, n+4) if(i == 0 || i == n+3 || j == 0 || j == n+3) v[i][j] = 1;
		int x = 2;
		int y = n+3-2;
		int dir = 0;
		v[y][x] = 1;

		bool end = false;
		while(1){
			int nx = x+dx[dir];
			int ny = y+dy[dir];
			if(check(v, nx, ny, (dir+2)%4)){
				v[ny][nx] = 1;
				x = nx, y = ny;
				end = false;
			}else{
				dir = (dir+1)%4;
				if(end) break;
				end = true;
			}
		}

		//pp(v);
		for(int i = 2; i < n+2; i++){
			for(int j = 2; j < n+2; j++){
				cout << (v[i][j]?'#':' ');
			}
			cout << endl;
		}
	}

	return 0;
}

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

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

void func(){
	char table[110][110] ;
	
	for( int i=0 ; i<102 ; i++ ){
		for( int j=0 ; j<102 ; j++ ){
			if( i>=100-N+1 && i<=100 && j>=1 && j<=N ) table[i][j] = '#' ;
			else table[i][j] = ' ' ;
		}
	}
	
	int x = 1   ;
	int y = 100 ;
	int k = 0 ;
	bool f = true ;
	
	while(1){
		if( table[y+dy[k%4]][x+dx[k%4]] == ' ' ) break ;
		bool b = false ;
		while( table[y+dy[k%4]][x+dx[k%4]] != ' ' ){
			if( f || b ) table[y+dy[(k+1)%4]][x+dx[(k+1)%4]] = ' ' ;
			if( !b ) b = true  ;
			if(  f ) f = false ;
			x += dx[k%4] ;
			y += dy[k%4] ;
		}
		k++ ;
	}
	
	for( int i=100-N+1 ; i<=100 ; i++ ){
		for( int j=1 ; j<=N ; j++ ){
			cout << table[i][j] ;
		}
		cout << endl ;
	}
}

int main(){
	
	int d ;
	
	cin >> d ;
	while( d-- ){
		cin >> N ;
		func() ;
		cout << endl ;
	}
	
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]; //上右下左
  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;
  }
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

const int dy[4] = {-1,0,1,0}, dx[4] = {0,1,0,-1};
int N;
bool fld[100][100];

bool end(int y,int x){
	int cnt = 0;
	for(int i=0;i<4;i++){
		int ny,nx;
		ny = y+dy[i]; nx = x+dx[i];
		if(ny<0 || N<=ny || nx<0 || N<=nx) continue;
		if(fld[ny][nx]) cnt++;
	}
	if(cnt>=2) return true;
	return false;
}

void dfs(int n,int dir,int y,int x){
	if(n<2) return;
	int cnt = 2;
	if(n==N) cnt = 3;
	for(int i=0;i<cnt;i++){
		for(int j=0;j<n-1;j++){
			y += dy[dir]; x += dx[dir];
			if(end(y,x)) return;
			fld[y][x] = true;
		}
		dir = (dir+1)%4;
	}
	dfs(n-2,dir,y,x);
}

int main(){
	int ds;
	cin>>ds;
	for(int k=0;k<ds;k++){
		if(k!=0) puts("");
		cin>>N;
		fill_n((bool*)fld,100*100,false);
		fld[N-1][0] = true; dfs(N,0,N-1,0);
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				if(fld[i][j]) printf("#");
				else printf(" ");
			}puts("");
		}
	}
	return 0;
}