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.Printf("##\n# \n")
			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 <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,dy[]={-1,0,1,0},dx[]={0,1,0,-1};
char c[100][100];

int main(){
	cin>>n;
	int f=0;
	while(cin>>n){
		if(f)cout<<endl;
		if(n==1){cout<<"#"<<endl;f=1;continue;}
		for(int i=0;i<n*n;i++)c[i/n][i%n]=' ';
		int y=n-1,x=0,u=0,d=n-1,l=0,r=n-1;
		c[y][x]='#';
		for(int i=0;i<n;i++){
			while(1){
				y+=dy[i%4];x+=dx[i%4];
				c[y][x]='#';
				if(i%4==0&&y==u||i%4==1&&x==r||i%4==2&&y==d||i%4==3&&x==l)break;
			}
			if(i%4==0)l+=2;
			if(i%4==1)u+=2;
			if(i%4==2)r-=2;
			if(i%4==3)d-=2;
		}
		for(int i=0;i<n*n;i++){
			cout<<c[i/n][i%n];
			if(i%n==n-1)cout<<endl;
		}
		f=1;
	}
}

C++: #include <cstdio>

#define MAX 150

char sp[MAX][MAX];
//u r d l
const int dx[] = {0,1,0,-1};
const int dy[] = {-1,0,1,0};

bool range(int x,int y,int n){
  if(0 > x || x >= n || 0 > y || y >= n){
    return false;
  }
  return true;
}

void make(int n){
  int x = 0 , y = n-1 ,dir = 0;

  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < n ; j++){
      sp[i][j] = '.';
    }
  }

  int end = 0;
  sp[y][x] = '#';
  while(true){
    int nx = x + dx[dir];
    int ny = y + dy[dir];  

    bool flg = true;
    if(!range(nx,ny,n)){
      flg = false;
    }

    for(int i = 0 ; i < 4 ; i++){
      int nnx = nx + dx[i];
      int nny = ny + dy[i];

      if((x == nnx && y == nny) 
	 || !range(nnx,nny,n)){
	continue;
      }

      if(sp[nny][nnx] == '#'){
	flg = false;
	break;
      }
    }

    if(!flg){
      dir++;
      dir = dir % 4;
      end++;
      if(end == 4) return;
    }else{
      end = 0;
    }

    if(flg){
      sp[ny][nx] = '#';
      x = nx;
      y = ny;
    }
  }
}

int main(){
  int d,n;

  scanf("%d" ,&d);
  while(d--){
    scanf("%d" ,&n);

    make(n);
    for(int i = 0 ; i < n ; i++){
      for(int j = 0 ; j < n ; j++){
	if(sp[i][j] == '.'){
	  printf(" ");
	}else{
	  printf("%c" ,sp[i][j]);
	}
      }
      puts("");
    }   
    puts("");
  }

  return 0;
}

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

using namespace std;

int main(void){
  int d;cin>>d;
  const int POS[][2]={{0,-1},{1,0},{0,1},{-1,0}};
  while(d--){
    int n;cin>>n;
    vector<vector<char> > field(n+2);
    for(int i=0;i<=n+1;++i){
      field[i].reserve(n+2);
      for(int j=0;j<=n+1;++j)field[i][j]=(i==n+1||j==n+1)?1:0;
    }
    int x=0,y=n-1,p=0;
    for(;;){
    	if(x<0||y<0)break;
      int dx=POS[p][0],dy=POS[p][1];
      int px=POS[(p+4+1)%4][0],py=POS[(p+4+1)%4][1],qx=POS[(p+4-1)%4][0],qy=POS[(p+4-1)%4][1];
      field[y][x]=1;
      x+=dx,y+=dy;
      if(y==0&&x==0||(y+2*dy>=0&&x+2*dx>=0&&field[y+2*dy][x+2*dx]==1))p=(p+1)%4;
      if((y+dy+py>=0&&x+dx+px>=0&&field[y+dy+py][x+dx+px]==1)||(y+dy+qy>=0&&x+dx+qx>=0&&field[y+dy+qy][x+dx+qx]==1))
      	break;
    }
    if(n==2)field[1][1]=0;
    for(int i=0;i<n;++i){
      for(int j=0;j<n;++j)cout<<((field[i][j])?'#':' ');
      cout<<endl;
    }
    cout<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int m,n,x,y,p,xx,yy,d,l,o;
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
main(){
  cin>>m;
  while(m--){
   if(o++)cout<<endl;
    cin>>n;l=0;
    string s[n];
    r(i,n)r(j,n)s[i]+='#';
    s[n-1][1]=' ';
    xx=0,yy=n-1,d=0;
    while(1){
      int c=0;
      r(i,4){
        if(!l)c--;
        y=dy[i]+yy,x=dx[i]+xx;
        if(y<0||y>=n||x<0||x>=n){
          c++;
          continue;
        }
        else if(s[y][x]==' ')c++;
      }
      if(c>=3)break;
      y=dy[d]+yy,x=dx[d]+xx;
      if(y<0||y>=n||x<0||x>=n)d=(d+1)%4;
      else if(s[y][x]==' ')d=(d+1)%4;
      else{
        yy=y+dy[d],xx=x+dx[d];
        if(yy>=0&&yy<n&&xx>=0&&xx<n&&s[yy][xx]!=' '){
          p=(d+1)%4;
          yy=y+dy[p],xx=x+dx[p];
          s[yy][xx]=' ';
        }
        yy=y,xx=x;
      }l++;
    }
    r(i,n)cout<<s[i]<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
int dy[]={-1,0,1,0};
int dx[]={0,1,0,-1};
int n;
char t[200][200];
   
int main(){
  int d,D;
  int i,j;
  scanf("%d",&d);
     
  for(D=0;D<d;D++){
    if(D>0)printf("\n");
       
    scanf("%d",&n);
       
    for(i=0;i<=n+3;i++){
      for(j=0;j<=n+3;j++){
        if(i==0||j==0||i==n+3||j==n+3)t[i][j]='#';
        else t[i][j]=' ';
      }
    }
       
    int y=n+1,x=2,dir=0,cnt=1;
    while(1){
      while(1){
        t[y][x]='#';
        if(t[y+dy[dir]*2][x+dx[dir]*2]=='#')break;
        cnt++;
        y+=dy[dir];
        x+=dx[dir];
      }
      if(cnt<=1)break;
      dir=(dir+1)%4;
      cnt=0;
    }
       
    for(i=2;i<2+n;i++){
      for(j=2;j<2+n;j++){
        printf("%c",t[i][j]);
      }
      printf("\n");
    }
  }
  return 0;
}

C++: #include <iostream>

struct Point {
	int x;
	int y;
	Point() {}
	Point(int x, int y) : x(x), y(y) {}
};

void show (char** a, int n)
{
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) std::cout << a[i][j];
		std::cout << std::endl;
	}
}

int main()
{
	int d; std::cin >> d;
	for (int k=0; k<d; k++) {
		int n; std::cin >> n;
		char** spiral = new char*[n];
		for (int i=0; i<n; i++) spiral[i] = new char[n];
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) spiral[i][j] = ' ';
		}
		
		Point p = Point(n-1, 0);
		int state = 0; // 0 : up, 1 : right, 2 : down, 3 : left
		bool finished = false;
		while (!finished) {
			spiral[p.x][p.y] = '#';
			switch (state) {
			case 0:
				if (p.x == 0 || (p.x >= 2 && spiral[p.x-2][p.y] == '#')) {
					if (n==1 || (n%4==1 && spiral[p.x][p.y+2] == '#')) {
						finished = true;
					}
					else state = 1;
				}
				else p.x--;
				break;
			case 1:
				if (p.y == n-1 || (p.y <= n-3 && spiral[p.x][p.y+2] == '#')) {
					if (n==2 || (n%4==2 && spiral[p.x+3][p.y] == '#')) {
						finished = true;
					}
					else state = 2;
				}
				else p.y++;
				break;
			case 2:
				if (p.x == n-1 || (p.x <= n-3 && spiral[p.x+2][p.y] == '#')) {
					if (n==3 || (n%4==3 && spiral[p.x][p.y-2] == '#')) {
						finished = true;
					}
					else state = 3;
				}
				else p.x++;
				break;
			case 3:
				if (spiral[p.x][p.y-2] == '#') {
					if (spiral[p.x-3][p.y] == '#') {
						finished = true;
					}
					else state = 0;
				}
				else p.y--;
				break;
			}
		}
		if (k>0) std::cout << std::endl;
		show(spiral, n);
		for (int i=0; i<n; i++) delete[] spiral[i];
		delete[] spiral;
	}
	
	return 0;
}