Problem p00141 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>

/*

 #####
 #   #
 # # #
 # # #
 # ###

 ######
 #    #
 # ## #
 # #  #
 # #  #
 # ####

 */
using namespace std;

void f(char guruguru[100][100], int l, int d, int x, int y) {
  while (1) {
    if (l <= 0)
      return;

    if (d == 0) { // 右下
      for (int i = 0; i < l; i++) {
        guruguru[y][x + i] = '#';
      }
      x = x + (l - 1);

      if (l == 2)
        return;

      for (int i = 0; i < l; i++) {
        guruguru[y + i][x] = '#';
      }
      y = y + (l - 1);
      l = l - 2;
      d = 1;
      // f(guruguru,l,d,x,y);
    }

    if (d == 1) { // 左上

      for (int i = 0; i < l; i++) {
        guruguru[y][x - i] = '#';
      }
      x = x - (l - 1);

      if (l <= 2)
        return;

      for (int i = 0; i < l; i++) {
        guruguru[y - i][x] = '#';
      }
      y = y - (l - 1);

      l = l - 2;
      d = 0;
      // f(guruguru,l,d,x,y);
    }
  }
}

void f0(char guruguru[100][100], int l, int d, int x, int y) {
  for (int i = 0; i < l; i++) {
    guruguru[i][0] = '#';
  }

  f(guruguru, l, 0, 0, 0);
}

/*
 int main(){

 for(int i=0; i<100; i++){
 for(int j=0; j<100; j++){
 guruguru_0[i][j]=' ';
 guruguru_1[i][j]=' ';
 guruguru_2[i][j]=' ';
 guruguru_3[i][j]=' ';
 }
 }


 //f0(guruguru_0, 100, 0, 0, 0);
 //f0(guruguru_3, 99, 0, 0, 0);
 //f0(guruguru_2, 98, 0, 0, 0);
 //f0(guruguru_1, 97, 0, 0, 0);



 int n;
 cin>>n;
 for(int roop=0; roop<n; roop++){
 int l;
 cin>>l;
 if(l%4==0){
 int x=50-l/2;
 int y=50-l/2;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_0[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 }else if(l%4==1){
 int x=50-l/2-2;
 int y=50-l/2-2;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_1[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 } else if(l%4==2){
 int x=50-l/2-1;
 int y=50-l/2-1;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_2[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 }else if(l%4==3){
 int x=50-l/2-1;
 int y=50-l/2-1;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_3[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 }
 }

 }
 */
int main() {

  int n;
  cin >> n;
  for (int roop = 0; roop < n; roop++) {
    int l;
    cin >> l;
    char guruguru[100][100];
    // memcpy(guruguru,init,sizeof(init));
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        guruguru[i][j] = ' ';
      }
    }

    f0(guruguru, l, 0, 0, 0);

    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        cout << guruguru[i][j];
      }
      cout << endl;
    }
    if (roop == n - 1)
      break;
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>

/*

#####
#   #
# # #
# # #
# ###

######
#    #
# ## #
# #  #
# #  #
# ####

*/
using namespace std;

void f(char guruguru[100][100], int l, int d, int x, int y) {

  if (l <= 0)
    return;

  if (d == 0) { // 右下
    for (int i = 0; i < l; i++) {
      guruguru[y][x + i] = '#';
    }
    x = x + (l - 1);

    if (l == 2)
      return;

    for (int i = 0; i < l; i++) {
      guruguru[y + i][x] = '#';
    }
    y = y + (l - 1);
    l = l - 2;
    d = 1;
    f(guruguru, l, d, x, y);
  }

  if (d == 1) { // 左上

    for (int i = 0; i < l; i++) {
      guruguru[y][x - i] = '#';
    }
    x = x - (l - 1);

    if (l <= 2)
      return;

    for (int i = 0; i < l; i++) {
      guruguru[y - i][x] = '#';
    }
    y = y - (l - 1);

    l = l - 2;
    d = 0;
    f(guruguru, l, d, x, y);
  }
}

void f0(char guruguru[100][100], int l, int d, int x, int y) {
  for (int i = 0; i < l; i++) {
    guruguru[i][0] = '#';
  }

  f(guruguru, l, 0, 0, 0);
}

int main() {
  char guruguru_0[100][100];
  char guruguru_1[100][100];
  char guruguru_2[100][100];
  char guruguru_3[100][100];
  for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
      guruguru_0[i][j] = ' ';
      guruguru_1[i][j] = ' ';
      guruguru_2[i][j] = ' ';
      guruguru_3[i][j] = ' ';
    }
  }

  f0(guruguru_0, 100, 0, 0, 0);
  f0(guruguru_3, 99, 0, 0, 0);
  f0(guruguru_2, 98, 0, 0, 0);
  f0(guruguru_1, 97, 0, 0, 0);

  int n;
  cin >> n;
  for (int roop = 0; roop < n; roop++) {
    int l;
    cin >> l;
    if (l % 4 == 0) {
      int x = 50 - l / 2;
      int y = 50 - l / 2;
      for (int i = y; i < y + l; i++) {
        for (int j = x; j < x + l; j++) {
          cout << guruguru_0[i][j];
        }
        cout << endl;
      }
      cout << endl;
    } else if (l % 4 == 1) {
      int x = 50 - l / 2 - 2;
      int y = 50 - l / 2 - 2;
      for (int i = y; i < y + l; i++) {
        for (int j = x; j < x + l; j++) {
          cout << guruguru_1[i][j];
        }
        cout << endl;
      }
      cout << endl;
    } else if (l % 4 == 2) {
      int x = 50 - l / 2 - 1;
      int y = 50 - l / 2 - 1;
      for (int i = y; i < y + l; i++) {
        for (int j = x; j < x + l; j++) {
          cout << guruguru_2[i][j];
        }
        cout << endl;
      }
      cout << endl;
    } else if (l % 4 == 3) {
      int x = 50 - l / 2 - 1;
      int y = 50 - l / 2 - 1;
      for (int i = y; i < y + l; i++) {
        for (int j = x; j < x + l; j++) {
          cout << guruguru_3[i][j];
        }
        cout << endl;
      }
      cout << endl;
    }
  }
}

/*
int main(){

   int n;
   cin>>n;
   for(int roop=0; roop<n; roop++){
       int l;
       cin>>l;
       char guruguru[100][100];
       //memcpy(guruguru,init,sizeof(init));
       for(int i=0; i<l; i++){
           for(int j=0; j<l; j++){
               guruguru[i][j]=' ';
           }
       }

       f0(guruguru,l,0,0,0);

       for(int i=0; i<l; i++){
           for(int j=0; j<l; j++){
               cout<<guruguru[i][j];
           }
           cout<<endl;
       }
       cout<<endl;
   }
}

*/