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_N 300

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++) {
		if (i >= 1) { cout << endl; }
		cnt = 0;
		cin >> a;
		for (int j = 0; j < MAX_N; j++) {
			for (int k = 0; k < MAX_N; k++) {
				x[j][k] = 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;
		}
	}
}

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

using namespace std;

main(){
  bool f = true;
  int n;
  char data[100][100];
  int dx[] = {0, 1, 0, -1};
  int dy[] = {-1, 0, 1, 0};
  int t;
  cin >> t;
  //t = 100;
  for(int tc=0;tc<t;tc++){
    cin >> n;
    //n = tc+1;
    //cout << n << endl;
    if(!f) cout << endl;
    if(n == 1){
      cout << "#" << endl;
      continue;
    }
    int y = n-1;
    int x = 0;
    fill(data[0], data[100], ' ');
    data[y][x] = '#';
    int d = 0;
    while(1){
      int ty = y + dy[d];
      int tx = x + dx[d];
      if(ty<0 || ty>=n){ d = (d+1)%4; continue; }
      if(tx<0 || tx>=n){ d = (d+1)%4; continue; }
      if(data[ty][tx] == '#') break;
      int tty = ty + dy[d];
      int ttx = tx + dx[d];
      if((tty>=0 || tty<n) && (ttx>=0 || ttx<n)){
	if(data[tty][ttx] == '#'){
	  d = (d+1)%4; 
	  continue; 
	}
      }
      int cnt = 0;
      for(int i=0;i<4;i++){
	int tty = ty + dy[i];
	int ttx = tx + dx[i];
	if(tty<0 || tty>=n) continue;
	if(ttx<0 || ttx>=n) continue;
	if(data[tty][ttx] == '#') cnt++;
      }
      if(cnt > 1) break;
      data[ty][tx] = '#';
      y = ty;
      x = tx;
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cout << data[i][j];
      }
      cout << endl;
    }
    f = false;
  }
  return 0;
}

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;
    
    while(n--){
        cin>>N;
        init();
        solve();
        FOR(i,2,N+2){
            FOR(j,2,N+2)cout<<fld[i][j];
            cout<<endl;
        }
    }

    return 0;
}

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

using namespace std;

int N;
int MX;
char output[500][500];

void PRINT() {
  for(int i=0; i<MX; i++) {
    cout << output[i] << endl;
  }
}

int main() {
  
  int Tc; cin >> Tc;
  bool blank = 0;
  while(Tc--) {
    if(blank) cout << endl;
    blank = 1;
    
    cin >> N;
    MX = N;
     
    for(int i=0; i<N; i++) {
      for(int j=0; j<N; j++) {
	output[i][j] = ' ';
      }
    }
    
    for(int i=0; i<N; i++) output[i][N] = 0;
    
    int cnt = 0;
    
    //////////////// phase 1 START
    
    int x = 0, y = N-1;
    for(int i=0; i<N; i++) {
      output[y-i][x] = '#';
    }
    y -= N-1;
    cnt ++;
    if(cnt == MX) goto EXIT;
        
    for(int i=0; i<N; i++) {
      output[y][x+i] = '#';
    }
    x += N-1;
    cnt ++;
    if(cnt == MX) goto EXIT;
    
    for(int i=0; i<N; i++) {
      output[y+i][x] = '#';
    }
    y += N-1;
    cnt ++;

    if(cnt == MX) goto EXIT;
    
    //////////////// phase 1 END
    
    
    //////////////// phase 2 START
    
    while(1) {
      
      N -= 2;
      for(int i=0; i<N; i++) {
	output[y][x-i] = '#';
      }
      
      x -= N-1;
      cnt ++;
      if(cnt == MX) goto EXIT;
      
      for(int i=0; i<N; i++) {
	output[y-i][x] = '#';
      }
      y -= N-1;
      cnt ++;
      if(cnt == MX) goto EXIT;
      
      N -= 2;
      
      for(int i=0; i<N; i++) {
	output[y][x+i] = '#';
      }
      x += N-1;
      cnt ++;
      if(cnt == MX) goto EXIT;
      
      for(int i=0; i<N; i++) {
	output[y+i][x] = '#';
      }
      y += N-1;
      cnt ++;
      if(cnt == MX) goto EXIT;
    }
    //////////////// phase 2 END
    
  EXIT:;
    PRINT();
  }
  
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int main(){
long d,n,i,j,k,p,q,r;
string s[100];
cin>>d;
for(k=0;k<d;k++){
cin>>n;
for(i=0;i<n;i++){
s[i].erase();
s[i].resize(n,' ');
}
for(i=n-1;i>=0; i--)s[i][0]='#';
for(j=0;j<n/2;j+=2){
for(i=j;    i<n-j;  i++)s[j]    [i]    ='#';
if(2*j>=n-2)break;
for(i=j;    i<n-j;  i++)s[i]    [n-1-j]='#';
for(i=n-1-j;i>=j+2; i--)s[n-1-j][i]    ='#';
if(2*j>=n-4)break;
for(i=n-1-j;i>=j+2; i--)s[i]    [j+2]  ='#';
}
if(k>0)cout<<endl;
for(i=0;i<n;i++)cout<<s[i]<<endl;
}
return 0;
}