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

int main(){
	const int MAX_N = 100;
	const int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
	int d, n;

	std::cin >> d;
	for(int k=0;k<d;k++){
		std::cin >> n;

		if(k > 0){
			std::cout << std::endl;
		}

		if(n == 2){
			std::cout << "##\n# \n";
		}else{
			int x = 0, y = n-1, p = n-3, angle = 0;

			int cloth[MAX_N][MAX_N];
			for(int i=0;i<n;i++){
				std::fill(cloth[i], cloth[i]+n, 0);
			}
			
			for(int i=0;i<3;i++){
				cloth[y][x] = 1;
				for(int j=0;j<n-1;j++){
					x += dx[angle];
					y += dy[angle];
					cloth[y][x] = 1;
				}
				angle = (angle + 1) % 4;
			}
			
			while(p > 1){
				for(int i=0;i<2;i++){
					cloth[y][x] = 1;
					for(int j=0;j<p;j++){
						x += dx[angle];
						y += dy[angle];
						cloth[y][x] = 1;
					}
					angle = (angle + 1) % 4;						
				}
				p -= 2;
			}
			
			if(n % 2 == 0){//偶数
				x += dx[angle];
				y += dy[angle];
				cloth[y][x] = 1;
			}
			
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					if(cloth[i][j]){
						printf("#");
					}else{
						printf(" ");
					}
				}
				std::cout << std::endl;
			}
		}
	}
}

C++: #include <iostream>
using namespace std;

class Guruguru
{
public:
  Guruguru(int n);
  ~Guruguru();

  void Show();

private:
  char** m_guruguru;
  int m_n;
  int m_now_i;
  int m_now_j;
  int m_direction;    //  0:上 1:右 2:下 3:左

  void make_guruguru();
  void put_point();
  void move_point();
  void change_direction();
  bool check_point();
};

Guruguru::Guruguru(int n)
{
  m_n = n;
  m_guruguru = new char*[m_n];
  for (int i=0; i<m_n; i++)
  {
    m_guruguru[i] = new char[m_n];
  }

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

  m_now_i = m_n-1;
  m_now_j = 0;
  m_direction = 0;
  make_guruguru();
}

Guruguru::~Guruguru()
{
  for (int i=0; i<m_n; i++)
  {
    delete[] m_guruguru[i];
  }
  delete[] m_guruguru;
}

void Guruguru::make_guruguru()
{
  int num = 0;
  while(true)
  {
    put_point();
    if (! check_point() )
    {
      if ( num == m_n-1 ) return;
      change_direction();
      num++;
    }
    move_point();
  }
}

void Guruguru::put_point()
{
  m_guruguru[m_now_i][m_now_j] = '#';
}

void Guruguru::move_point()
{
  switch(m_direction)
  {
  case 0:
    m_now_i--;
    break;
  case 1:
    m_now_j++;
    break;
  case 2:
    m_now_i++;
    break;
  case 3:
    m_now_j--;
    break;
  }
}

void Guruguru::change_direction()
{
  switch(m_direction)
  {
  case 3:
    m_direction = 0;
    break;
  default:
    m_direction++;
    break;
  }
}

bool Guruguru::check_point()
{
  int next_i = m_now_i;
  int next_j = m_now_j;
  int next_next_i = next_i;
  int next_next_j = next_j;
  switch(m_direction)
  {
  case 0:
    next_i--;
    next_next_i-=2;
    break;
  case 1:
    next_j++;
    next_next_j+=2;
    break;
  case 2:
    next_i++;
    next_next_i+=2;
    break;
  case 3:
    next_j--;
    next_next_j-=2;
    break;
  }
  if ( next_i < 0 || m_n <= next_i || next_j < 0 || m_n <= next_j )
  {
    return false;
  }
  else if ( next_next_i < 0 || m_n <= next_next_i || next_next_j < 0 || m_n <= next_next_j )
  {
    return true;
  }
  else
  {
    if ( '#' == m_guruguru[next_next_i][next_next_j] )
    {
      return false;
    }
    return true;
  }
}

void Guruguru::Show()
{
  for (int i=0; i<m_n; i++)
  {
    for (int j=0; j<m_n; j++)
    {
      cout << m_guruguru[i][j] << flush;
    }
    cout << endl;
  }
}

int main()
{
  int d;  //  データセットの個数
  cin >> d;
  for (int dataset=0; dataset<d; dataset++)
  {
    int n;  //  辺の長さ
    cin >> n;
    Guruguru guruguru(n);

    guruguru.Show();
    cout << endl;
  }
}

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

const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,1,0,-1 };
int main() {
	int D; cin >> D;
	while (D--) {
		int N; cin >> N;
		vector<vector<char>>field(N, vector<char>(N, ' '));
		int way = 3;
		int nx = 0;
		int ny = N - 1;
		bool flag = true;
		while (flag) {
			field[ny][nx] = '#';
			if (N == 1)break;
			for (int i = 0; i < 2; ++i) {
				int nextx = nx + dx[way];
				int nexty = ny + dy[way];
				if (nextx >= 0 && nextx < N&&nexty >= 0 && nexty < N) {
					bool aok = true;
					for (int w = -1; w <= 1; ++w) {
						int nextway = (way + 4 + w) % 4;
						int next2x = nextx + dx[nextway];
						int next2y = nexty + dy[nextway];
						if (next2x >= 0 && next2x < N&&next2y >= 0 && next2y < N) {
							if (field[next2y][next2x] == '#') {
								aok = false;
							}
						}
					}
					if(!aok){
						if(i==1)flag = false;
						
					}
					else {
						nx = nextx;
						ny = nexty;
						break;
					}
				}
				way = (way + 3) % 4;
			}
			
		}
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				cout << field[i][j];
			}
			cout << endl;
		}
		if(D)cout << endl;
	}
	return 0;
}

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





int main(){
  int time;
  cin>>time;
  for(int i=0;i<time;i++){
  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;
}