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>
using namespace std;
bool vis[110][110],na[110][110];
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 <iostream>
#include <cstring>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

void solve(int n) {
	if (n == 1) {
		cout << '#' << endl;
		return;
	}
	char field[n][n + 1];
	memset(field, ' ', sizeof(field));
	for (int i = 0; i < n; i++) {
		field[i][n] = '\0';
	}
	int x = 0, y = n - 1, d = 0;
	while (true) {
		field[y][x] = '#';
		int newX = x + dx[d];
		int newY = y + dy[d];
		if (newX < 0 || n <= newX || newY < 0 || n <= newY
				|| field[newY+ dy[d]][newX+ dx[d]] == '#') {
			d = (d + 1) % 4;
			newX = x + dx[d];
			newY = y + dy[d];
		}
		int count = 0;
		for (int i = 0; i < 4; i++) {
			int cx = newX + dx[i];
			int cy = newY + dy[i];
			if (0 <= cx && cx < n && 0 <= cy && cy < n && field[cy][cx] == '#') count++;
		}
		if (count > 1) break;
		x = newX;
		y = newY;
	}
	for (int i = 0; i < n; i++) {
		cout << field[i] << endl;
	}
}

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

C++: #include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

char f[100][100];
int dw[4] = {0, 1, 0, -1};
int dh[4] = {-1, 0, 1, 0};
signed main(void){
  int i, j;
  int(d);
  for(int testcase = 0;testcase < d;testcase++){
    if(testcase != 0)puts("");
    int(n);
    rep(i, n)rep(j, n)f[i][j] = '.';
    int noww = 0, nowh = n - 1, k = 0;

    bool is = false;
    for(;;){
      //printf("%d %d %d\n", nowh, noww, k);
      f[nowh][noww] = '#';
      if(0 > nowh + dh[k] || nowh + dh[k] >= n || 0 > noww + dw[k] || noww + dw[k] >= n){
        //puts("a");
        if(is)break;
        is = true;
        k = (k + 1) % 4;
        continue;
      }
      if(0 <= nowh + dh[k] * 2 && nowh + dh[k] * 2 < n)
      if(0 <= noww + dw[k] * 2 && noww + dw[k] * 2 < n)
      if(f[nowh + dh[k] + dh[k]][noww + dw[k] + dw[k]] == '#'){
        //puts("b");
        if(is)break;
        is = true;
        k = (k + 1) % 4;
        continue;
      }
      if(0 <= nowh + dh[k] + dh[(k + 1) % 4] && nowh + dh[k] + dh[(k + 1) % 4] < n)
      if(0 <= noww + dw[k] + dw[(k + 1) % 4] && noww + dw[k] + dw[(k + 1) % 4] < n)
      if(f[nowh + dh[k] + dh[(k + 1) % 4]][noww + dw[k] + dw[(k + 1) % 4]] == '#'){
        //puts("c");
        if(is)break;
        is = true;
        k = (k + 1) % 4;
        continue;
      }
      if(0 <= nowh + dh[(k + 3) % 4] && nowh + dh[(k + 3) % 4] < n)
      if(0 <= noww + dw[(k + 3) % 4] && noww + dw[(k + 3) % 4] < n)
      if(f[nowh + dh[(k + 3) % 4]][noww + dw[(k + 3) % 4]] == '#'){
        //puts("d");
        if(is)break;
        is = true;
        k = (k + 1) % 4;
        continue;
      }
      //puts("e");
      is = false;
      nowh += dh[k]; noww += dw[k];
    }

    rep(i, n){
      rep(j, n)printf("%c", f[i][j]);
      puts("");
    }

//*/

//*/ break;
  }
  return 0;
}

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;
		}
		cout << endl;
	}
	return 0;
}