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
		len := 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++
				len = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if len < 3 {
					break
				}
				dir++
				len = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				len++
			}
		}
		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 "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 0
#define dump(o) if(DBG){cerr<<#o<<" "<<(o)<<" ";}
#define dumpl(o) if(DBG){cerr<<#o<<" "<<(o)<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
int N;
bool inrange(int i, int j) { return 0 <= i&&i < N && 0 <= j&&j < N; }

signed main() {
	int d; cin >> d;
	int a[120][120] = {};
	int di[] = { -1,0,1,0 };
	int dj[] = { 0,1,0,-1 };
	rep(h, 0, d) {
		if (h != 0)cout << endl;
		memset(a, 0, sizeof(a));
		cin >> N;
		int ci = N - 1, cj = 0;
		int dir = 0;
		int cnt = 1;
		while (true) {
			a[ci][cj] = cnt;
			int ni = ci + di[dir], nj = cj + dj[dir];
			if (!inrange(ni, nj)) {
				dir = (dir + 1) % 4;
				ni = ci + di[dir], nj = cj + dj[dir];
			}
			else if (inrange(ni + di[dir], nj + dj[dir]) && a[ni + di[dir]][nj + dj[dir]] != 0) {
				dir = (dir + 1) % 4;
				ni = ci + di[dir], nj = cj + dj[dir];
			}
			dump(ci)dumpl(cj);
			dump(ni + dj[dir])dumpl(nj + dj[dir]);
			if (inrange(ni + di[dir], nj + dj[dir]) && (a[ni + di[dir]][nj + dj[dir]] != 0 || a[ni + di[(dir + 1) % 4]][nj + dj[(dir + 1) % 4]] != 0)) {
				break;
			}
			ci += di[dir];
			cj += dj[dir];

		}
		rep(i, 0, N) {
			rep(j, 0, N) {
				cout << (a[i][j] ? '#' : ' ');
			}
			cout << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdlib>
const static int SIZE = 100;
const static int MARGIN = 2;

const static int F_NONE = 0;
const static int F_WALL = 1;
const static int F_LINE = 2;

const static int DIR_N = 4;

using namespace std;


void draw( int n ) {
  int field[ SIZE + MARGIN * 2 ][ SIZE + MARGIN * 2 ] = { 0 };
  int pos_y = n + MARGIN - 1;
  int pos_x = MARGIN;
  int vy[] = { -1, 0, 1,  0 };
  int vx[] = {  0, 1, 0, -1 };
  int dir = 0;
  int lastMoveDir = -1;
  int turnNum = 0;
  for( int i = 0; i < n + MARGIN * 2; i++ ) {
    field[ i ][ 0 ] = F_WALL;
    field[ 0 ][ i ] = F_WALL;
    field[ i ][ n + MARGIN * 2 - 1 ] = F_WALL;
    field[ n + MARGIN * 2 - 1 ][ i ] = F_WALL;
  }

  while( 1 ) {
    field[ pos_y ][ pos_x ] = F_LINE;
    if(
      field[ pos_y + vy[ dir ] * 2 ][ pos_x + vx[ dir ] * 2 ] == F_NONE 
    ) {
      pos_y += vy[ dir ];
      pos_x += vx[ dir ];
      lastMoveDir = dir;
    } else {
      dir = ( dir + 1 ) % DIR_N;
      if( turnNum++ == n - 1 ) { break; }
    }
  }

  for( int i = MARGIN; i < n + MARGIN; i++ ) {
    for( int j = MARGIN; j < n + MARGIN; j++ ) {
      cout << ( field[ i ][ j ] ? '#' : ' ' ) << flush;
    }
    cout << endl;
  }
  cout << endl;
}


int main() {
  int d;
  cin >> d;
  for( int i = 0; i < d; i++ ) {
    int n;
    cin >> n;
    draw( n );    
  }
  return EXIT_SUCCESS;
}

C++: #include<iostream>
using namespace std;
bool vis[30][30],na[30][30];
int dx[]={0,1,0,-1},dy[]={-1,0,1,0};
int ex[]={-1,1,0,0},ey[]={0,0,-1,1};
int n;
void DFS(int y,int x,int k){
  vis[y][x]=true;
  na[y][x]=true;
  int xx,yy;
  bool done=false;
  for(int i=0;i<4;i++){
    xx=x+dx[k],yy=y+dy[k];
    bool ok=false;
    if(0<=xx&&xx<n&&0<=yy&&yy<n){
      if(na[yy][xx]==false){
	ok=true;
      }
    }
    if(ok){done=true;break;}
    k=(k+1)%4;
  }
  if(!done)return;
  for(int i=0;i<2;i++){
    int r=i+(k%2)*2;
    int nx=x+ex[r],ny=y+ey[r];
    if(0<=nx&&nx<n&&0<=ny&&ny<n){
      na[ny][nx]=true;
    }
  }
  DFS(yy,xx,k);
}
int main(){
  int q;cin>>q;
  for(int u=0;u<q;u++){
    if(u)cout<<endl;
    cin>>n;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	vis[i][j]=false;
	na[i][j]=false;
      }
    }
    DFS(n-1,0,0);
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++)cout<<(vis[i][j]?"#":" ");
      cout<<endl;
    }
  }
  return 0;
}

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

int t,n,y,x,d,f;
char c[N][N];
int dy[4]={1,0,-1,0};
int dx[4]={0,1,0,-1};

bool check(int a,int b){
  return (a>=0&&b>=0&&a<n&&b<n);
}

int main(){
  cin>>t;
  while(t--){
    cin>>n;
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)c[i][j]=' ';
    y=x=d=0;
    c[y][x]='#';
    while(1){
      int ny=y+dy[d];
      int nx=x+dx[d];
      f=0;
      if(ny<0||nx<0||n<=ny||n<=nx)f=1;
      int a=ny+dy[d],b=nx+dx[d];
      if(check(a,b)&&c[a][b]=='#')f=1;
      if(f)d=(d+1)%4;
      ny=y+dy[d];
      nx=x+dx[d];
      a=ny+dy[d],b=nx+dx[d];
      if(check(a,b)&&c[a][b]=='#')break;
      a=ny+dy[(d+1)%4],b=nx+dx[(d+1)%4];
      if(check(a,b)&&c[a][b]=='#')break;
      y+=dy[d];
      x+=dx[d];
      c[y][x]='#';
    }
    for(int i=n-1;i>=0;i--){
      for(int j=0;j<n;j++)cout<<c[i][j];
      cout<<endl;
    }
    if(t)cout<<endl;
  }
  return 0;
}

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

using namespace std;

int main() {
	int n;
	cin >> n;
	for (int j = 0; j < n; j++) {
		int a;
		cin >> a;
		vector< vector<int> > uzu(a, vector<int>(a, 0));
		int ulimit, dlimit, rlimit, llimit;
		ulimit = 0;
		dlimit = a-1;
		rlimit = a-1;
		llimit = 2;
		int tate, yoko;
		tate = a-1;
		yoko = 0;
		if (a == 1) {
			uzu[0][0] = 1;
		}
		while (1) {
			if (yoko > rlimit || tate == ulimit) {
				break;
			}
			for (int i = tate; i >= ulimit; i--) {
				uzu[i][yoko] = 1;
			}
			tate = ulimit;
			ulimit += 2;
			if (tate > dlimit || yoko == rlimit) {
				break;
			}
			for (int i = yoko; i <= rlimit; i++) {
				uzu[tate][i] = 1;
			}
			yoko = rlimit;
			rlimit -= 2;
			if (yoko < llimit || tate == dlimit) {
				break;
			}
			for (int i = tate; i <= dlimit; i++) {
				uzu[i][yoko] = 1;
			}
			tate = dlimit;
			dlimit -= 2;
			if (tate < ulimit || yoko == llimit) {
				break;
			}
			for (int i = yoko; i >= llimit; i--) {
				uzu[tate][i] = 1;
			}
			yoko = llimit;
			llimit += 2;
		}
		if (j > 0) {
			cout << endl;
		}
		for (int i = 0; i < a; i++) {
			for (int k = 0; k < a; k++) {
				if (uzu[i][k] == 1) {
					cout << '#';
				} else {
					cout << ' ';
				}
			}
			cout << endl;
		}
	}
	return 0;
}