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<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();
        }
        /*for(int i = 0; i < d; i++){
            for(int j = 0; j < d; j++){
                cout <<dx[i][j];
            }
            cout <<endl;
        }*/
    }
    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;
    }
    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){
                dx[i][j] = false;
            } else{
                dx[i][j] = true;
            }
        }
    }
    dispDx();
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int g=0,i,j,k;
  int x,y,n,nh,b[100][100];
  vector<int> h;
  cin>>n;
  h.reserve(n);
  for(i=0;i<n;i++)
    cin>>h[i];
  for(i=0;i<n;i++){
    memset(b,0,sizeof(b));
    for(j=0;j<h[i];j++)
      b[0][j]=1;
    nh=h[i];
    h[i]--;
    x=1;
    y=0;
    for(;;){
      if(h[i]<=0)
	break;
      for(j=0;j<h[i];j++)
	b[x+j][y]=1;
      x+=h[i]-1;
      y++;
      if(h[i]==1)
	break;
      for(j=0;j<h[i];j++)
	b[x][y+j]=1;
      x--;
      y+=h[i]-1;
      h[i]-=2;
      if(h[i]<=0)
	break;
      for(j=0;j<h[i];j++)
	b[x-j][y]=1;
      x-=h[i]-1;
      y--;
      if(h[i]==1)
	break;
      for(j=0;j<h[i];j++)
	b[x][y-j]=1;
      x++;
      y-=h[i]-1;
      h[i]-=2;
    }
    if(g)
      cout<<endl;
    g=1;
    for(j=0;j<nh;j++){
      for(k=0;k<nh;k++)
	if(b[k][j])
	  cout<<"#";
	else
	  cout<<" ";
      cout<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n) 
int nx,ny,nr;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int a[100][100];
bool plot(int x, int y, int r, int n){
  int count = 0;
  bool flag = true;
  if((x+2*dx[r]>=0&&x+2*dx[r]<n)&&
      y+2*dy[r]>=0&&y+2*dy[r]<n&&a[x+2*dx[r]][y+2*dy[r]]==1)
    return false;
  x+=dx[r]; y+=dy[r];
  while((x>=0&&x<n)&&(y>=0&&y<n)){
    if((x+2*dx[r]>=0&&x+2*dx[r]<n)&&y+2*dy[r]>=0&&y+2*dy[r]<n&&a[x+2*dx[r]][y+2*dy[r]]==1) flag = false;
    a[x][y] = 1;
    x+=dx[r]; y+=dy[r];
    count++;
    if(!flag) break;
  }
  x-=dx[r];y-=dy[r]; nx=x; ny=y; nr=(r+1)%4;
  if(count<2) return false;
  return true;
}
int main(void){
  int d;
  cin>>d;
  while(d--){
    int n;
    cin>>n;
    memset(a,0,sizeof(a));
    a[0][n-1]=1;
    nx=0; ny=n-1; nr=3;
    while(plot(nx,ny,nr,n));
    REP(i,n){
      REP(j,n)
        printf("%c",(a[j][i]==1)?'#':' ');
      cout << endl;
    }
  }
  return 0;
}