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, 100)
	}
	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>
#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 <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>


typedef long long ll;
const double Eps = 1e-6;

using namespace std;

int main()
{
	int d;
	scanf("%d", &d);
	while (d--)
	{
		int n;
		scanf("%d", &n);
		bool maze[126][126];
		memset(maze, 0, sizeof(maze));
		for (int i = 0; i < n+4; ++i)
			maze[0][i] = maze[i][0] = maze[n+3][i] = maze[i][n+3] = true;

		int x = 2;
		int y = n+1;
		int dx[] = { 0, 1, 0, -1 };
		int dy[] = { -1, 0, 1, 0 };
		int dir = 0;
		while (true)
		{
			int c = 0;
			for (int i = 0; i < 4; ++i)
				if (maze[y+dy[i]][x+dx[i]])
					++c;
			if (c > 1)
				break;

			maze[y][x] = true;
			int tx = x + 2*dx[dir];
			int ty = y + 2*dy[dir];
			if (maze[ty][tx])
				dir = (dir+1) % 4;

			x += dx[dir];
			y += dy[dir];
		}
		for (int i = 2; i < n+2; ++i)
		{
			for (int j = 2; j < n+2; ++j)
				putchar(maze[i][j] ? '#' : ' ');
			putchar('\n');
		}
		if (d)
			putchar('\n');
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int n;
char s[105][105];
int dx[]{ -1,0,1,0 }, dy[]{ 0,1,0,-1 };

void dfs(int idx, int x, int y) {
	if (s[x][y] == '#')return;
	s[x][y] = '#';
	int nx = x + dx[idx], ny = y + dy[idx];
	if (nx < 0 || n <= nx || ny < 0 || n <= ny) {
		idx = (idx + 1) % 4;
		dfs(idx, x + dx[idx], y + dy[idx]);
		return;
	}
	int kx = nx + dx[idx], ky = ny + dy[idx];
	if (!(0 <= kx&&kx < n && 0 <= ky&&ky < n&&s[kx][ky] == '#')) {
		dfs(idx, nx, ny);
		return;
	}
	idx = (idx + 1) % 4;
	nx = x + dx[idx], ny = y + dy[idx];
	kx = nx + dx[idx], ky = ny + dy[idx];
	int jx = nx + dx[(idx + 1) % 4], jy = ny + dy[(idx + 1) % 4];
	if ((!(0 <= kx&&kx < n && 0 <= ky&&ky < n&&s[kx][ky] == '#'))&& (!(0 <= jx&&jx < n && 0 <= jy&&jy < n&&s[jx][jy] == '#'))) {
		dfs(idx, nx, ny);
		return;
	}
}
int main() {
	int d; scanf("%d", &d);
	rep(i, d) {
		scanf("%d", &n);
		if (n == 1) {
			puts("#"); continue;
		}
		memset(s, 0, sizeof(s));
		rep(j, n)rep(k, n)s[j][k] = ' ';
		dfs(0, n - 1, 0);
		rep(j, n)printf("%s\n", s[j]);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;





int main(){
  char t='u';
  int n;
  cin>>n;
  char a[1000][1000]={};
  fill(&a[0][0],&a[999][1000],' ');
  a[n-1][0]='#';
  pair<int,int> w=make_pair(n-1,0);
  while(1){
    cout<<t<<" "<<w.first<<" "<<w.second<<endl;
    if(t=='u'){
      if(a[w.first-1][w.second]=='#' || a[w.first-1][w.second+1]=='#'){break;}
      if(w.first==0 || a[w.first-2][w.second]=='#'){t='r'; continue;}
      //w=make_pair(w.first+1,w.second);
      w.first--;
      a[w.first][w.second]='#';
    }
    if(t=='r'){
      if(a[w.first][w.second+1]=='#' || a[w.first+1][w.second+1]=='#'){break;}
      if(w.second==n-1 || a[w.first][w.second+2]=='#'){t='d';  continue;}
      //w=make_pair(w.first,w.second+1);
      w.second++;
      a[w.first][w.second]='#';
    }
    if(t=='d'){
      if(a[w.first+1][w.second]=='#' || a[w.first+1][w.second-1]=='#'){break;}
      if(w.first==n-1 || a[w.first+2][w.second]=='#'){t='l';  continue;}
      //w=make_pair(w.first-1,w.second);
      w.first++;
      a[w.first][w.second]='#';
    }
    if(t=='l'){
      if(a[w.first][w.second-1]=='#' || a[w.first+1][w.second-1]=='#'){break;}
      if(w.second==0 || a[w.first][w.second-2]=='#'){t='u'; continue;}
      //w=make_pair(w.first,w.second-1);
      w.second--;
      a[w.first][w.second]='#';
    }
  }
  for(int i=0;i<n;i++){
    for(int t=0;t<n;t++){
      cout<<a[i][t];
    }
    cout<<endl;
  }

  

  
  return 0;
}