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 <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

int N, n;

char str[200][200];

void set_(int x, int y){
	if(x < 0 || y < 0 || x >= n || y >= n){
		return;
	}
	str[y][x] = ' ';
}

bool checker(int x, int y){
	if(x < 0 || y < 0 || x >= n || y >= n){
		return false;
	}
	if(str[y][x] == 'p'){
		return true;
	}
	return false;
}

bool set(int x, int y, int dx, int dy){
	if(x < 0 || y < 0 || x >= n || y >= n){
		return false;
	}
	if(str[y][x] != 'p'){
		return false;
	}
	str[y][x] = '#';
	if(dx != 0){
		if(checker(x - dx, y - 1)){
			set_(x - dx, y - 1);
		}
		if(checker(x - dx, y + 1)){
			set_(x - dx, y + 1);
		}
	}else{
		if(checker(x - 1, y - dy)){
			set_(x - 1, y - dy);
		}
		if(checker(x + 1, y - dy)){
			set_(x + 1, y - dy);
		}
	}
	if(!set(x + dx, y + dy, dx, dy)){
		if(dx > 0){
			dx = 0;
			dy = 1;
		}else if(dx < 0){
			dx = 0;
			dy = -1;
		}else if(dy > 0){
			dx = -1;
			dy = 0;
		}else{
			dx = 1;
			dy = 0;
		}
		set(x + dx, y + dy, dx, dy);
	}
	return true;
}

int main(){
	
	cin >> N;
	
	for(int loop = 0; loop < N; loop++){
		
		if(loop != 0){
			printf("\n");
		}
		
		cin >> n;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				str[i][j] = 'p';
			}
		}
		
		set(0, n - 1, 0, -1);
		
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				if(str[i][j] == 'p'){
					str[i][j] = ' ';
				}
			}
		}
		
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				printf("%c", str[i][j]);
			}
			printf("\n");
		}
	}
	
	return 0;
}

C++: #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main()
{
	int d;
	scanf("%d", &d);
	rep(i, d){
		int n;
		scanf("%d", &n);
		
		char field[128][128];
		rep(j, n){
			fill(field[j], field[j]+n, ' ');
			field[j][n] = '\0';
		}
		
		const int dy[] = {-1, 0, 1, 0};
		const int dx[] = {0, 1, 0, -1};
		int y=n-1, x=0;
		field[y][x] = '#';
		int dir = 0;
		int okok = 0;
		for(;;){
			int py = y + dy[dir];
			int px = x + dx[dir];
			
			bool ok = true;
			if(py<0 || n<=py || px<0 || n<=px)
				ok = false;
			
			rep(j, 4){
				int ny = py + dy[j];
				int nx = px + dx[j];
				if(ny<0 || n<=ny || nx<0 || n<=nx || (y==ny && x==nx))
					continue;
				
				if(field[ny][nx] == '#'){
					ok = false;
					j=3;
					break;
				}
			}
			
			if(!ok){
				dir = (dir+1) % 4;
				++okok;
				if(okok == 4)
					break;
				continue;
			}
			else
				okok = 0;
			
			y = py;
			x = px;
			field[y][x] = '#';
		}
		
		if(i) puts("");
		rep(j, n)
			puts(field[j]);
	}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int dr[] = { -1, 0, 1, 0 }, dc[] = { 0, 1, 0, -1 };
int a[110][110];
int main()
{
	ios_base::sync_with_stdio(0);
	int d; cin >> d;
	while (d--) {
		int n; cin >> n;
		int cnt = n;
		if (n % 2 == 0) {
			for (int i=1; n-i>1; i+=2) cnt += 2*(n-i);
			cnt++;
		}
		else {
			for (int i=1; n-i>1; i+=2) cnt += 2*(n-i);
		}
		memset(a, 0, sizeof(a));
		int r = n, c = 1, k = 0, x = 0;
		while (x < cnt) {
			a[r][c] = ++x;
			r += dr[k], c += dc[k];
			if (r <= 0 || r >= n+1 || c <= 0 || c >= n+1) {
				r -= dr[k], c -= dc[k];
				k = (k + 1) % 4;
				r += dr[k], c += dc[k];
			}
			else if (a[r+dr[k]][c+dc[k]]) {
				a[r][c] = 0;
				r -= dr[k], c -= dc[k];
				k = (k + 1) % 4;
				r += dr[k], c += dc[k];
			}
		}
		REP(i, 1, n+1) {
			REP(j, 1, n+1) putchar(a[i][j] ? '#' : ' ');
			//REP(j, 1, n+1) printf("%3d", a[i][j]);
			puts("");
		}
		if (d) puts("");
	}
	return 0;
}

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

int n;
int input(){
  if(cin>>n)return true;
  return false;
}

vector<string> ans;
void solve(){
  ans.resize(n);
  for(int i=0;i<ans.size();i++){
    ans[i]="";
    for(int j=0;j<n;j++){
      ans[i] += " ";
    }
  }
  int x=0;
  int y=0;
  int d=0;
  int m=n+2;
  const int dx[] = {1,0,-1,0};
  const int dy[] = {0,1,0,-1};
  for(int i=0;i<n;i++){
    for(int j=0;j<min(m-1,n-1);j++){
      ans[x][y]='#';
      x += dx[d];
      y += dy[d];
      ans[x][y]='#';
    }
    d=(d+1)%4;;
    if(d%2){
      m-=2;
    }
  }
  ans = vector<string>(ans.rbegin(),ans.rend());
}

void print(){
  for(auto i:ans){
    cout<<i<<endl;
  }
}


int main(){
  while(input()){
    solve();
    print();
  }
}

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

using namespace std;

const int MAX = 101;
char data[MAX][MAX];
int n;
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
 
void print(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++)
      cout << data[i][j];
    cout << endl;
  }
}

bool movable(int d, int x, int y){
  return data[y+dy[d]][x+dx[d]] == ' ';
}

void solve(){

  int x = 2,y = n-1;
  int d = 0;

  for(int i = 0; i < n; i++)
    data[0][i] = data[n-1][i] = data[i][0] = data[i][n-1] = '#';

  data[n-1][1] = ' ';

  if(n == 4) return;

  while(1){
    int num = 0;

    while(1){
      x += dx[d];
      y += dy[d];
      if(movable(d,x,y)){
	num++;
	data[y][x] = '#';
      }else{
	x -= dx[d];
	y -= dy[d];
	break;
      }
    }
    if(num < 2) break;
    d++;
    d%=4;
  }
}

void init(){
  memset(data,' ',sizeof(data));
}

int main(){

  bool line = false;
  int x;
  cin >> x;
  
  while(x--){
    if(line) cout << endl;
    line = true;
    init();
    cin >> n;
    solve();
    print();
  }
  return 0;
}