alexjercan/bugnet huggingface dataset:
Problem p00723 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int m;
vector<string> train;

bool input() {
  cin >> m;
  for (int i = 0; i < m; i++) {
    string tmp;
    cin >> tmp;
    train.push_back(tmp);
  }
  return true;
}

string rev_str(string s) {
  string ret;
  for (int i = 0; i < s.size(); i++) {
    ret += s[s.size() - 1 - i];
  }

  return ret;
}

int solve(int n) {
  vector<string> mem;

  for (int i = 1; i < train[n].size(); i++) {
    string strage1, strage2;
    string s[10];
    for (int j = 0; j < i; j++) {
      strage1 += train[n][j];
    }
    for (int j = i; j < train[n].size(); j++) {
      strage2 += train[n][j];
    }

    s[0] = strage1 + strage2;
    s[1] = rev_str(strage1) + strage2;
    s[2] = strage1 + rev_str(strage2);
    s[3] = strage2 + strage1;
    s[4] = rev_str(strage2) + strage1;
    s[5] = strage2 + rev_str(strage1);
    s[6] = rev_str(strage1) + rev_str(strage2);
    s[7] = rev_str(strage2) + rev_str(strage1);
    s[8] = rev_str(s[0]);
    s[9] = rev_str(s[3]);

    for (int j = 0; j < 10; j++) {
      bool flag = true;
      for (int k = 0; k < mem.size(); k++) {
        if (mem[k] == s[j]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        mem.push_back(s[j]);
      }
    }
  }
  return mem.size();
}

int main() {
  input();
  for (int i = 0; i < m; i++) {
    cout << solve(i) << endl;
  }
  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int m;
vector<string> train;

bool input() {
  cin >> m;
  for (int i = 0; i < m; i++) {
    string tmp;
    cin >> tmp;
    train.push_back(tmp);
  }
  return true;
}

string rev_str(string s) {
  string ret;
  for (int i = 0; i < s.size(); i++) {
    ret += s[s.size() - 1 - i];
  }

  return ret;
}

int solve(int n) {
  vector<string> mem;

  for (int i = 1; i < train[n].size(); i++) {
    string strage1, strage2;
    string s[8];
    for (int j = 0; j < i; j++) {
      strage1 += train[n][j];
    }
    for (int j = i; j < train[n].size(); j++) {
      strage2 += train[n][j];
    }

    s[0] = strage1 + strage2;
    s[1] = rev_str(strage1) + strage2;
    s[2] = strage1 + rev_str(strage2);
    s[3] = strage2 + strage1;
    s[4] = rev_str(strage2) + strage1;
    s[5] = strage2 + rev_str(strage1);
    s[6] = rev_str(strage1) + rev_str(strage2);
    s[7] = rev_str(strage2) + rev_str(strage1);
    s[8] = rev_str(s[0]);
    s[9] = rev_str(s[3]);

    for (int j = 0; j < 10; j++) {
      bool flag = true;
      for (int k = 0; k < mem.size(); k++) {
        if (mem[k] == s[j]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        mem.push_back(s[j]);
      }
    }
  }
  return mem.size();
}

int main(void) {
  input();
  for (int i = 0; i < m; i++) {
    cout << solve(i) << endl;
  }
  return 0;
}
Problem p00723 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define pb push_back

const int INF = 100000000;

int main(void) {
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s;
    set<string> sset;
    string sr = s;
    reverse(s.begin(), s.end());
    for (int i = 0; i < s.size() - 1; i++) {
      string s1 = s.substr(0, i + 1);
      string s2 = s.substr(i + 1);
      string s1r = sr.substr(sr.size() - (i + 1));
      string s2r = sr.substr(0, sr.size() - (i + 1));

      sset.insert(s1 + s2);
      sset.insert(s1r + s2);
      sset.insert(s1 + s2r);
      sset.insert(s1r + s2r);

      sset.insert(s2 + s1);
      sset.insert(s2r + s1);
      sset.insert(s2 + s1r);
      sset.insert(s2r + s1r);
    }
    cout << sset.size() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define pb push_back

const int INF = 100000000;

int main(void) {
  while (1) {
    string s;
    getline(cin, s);
    if (s == ".")
      break;

    stack<char> st;
    bool success = true;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '(' || s[i] == '[') {
        st.push(s[i]);
      }
      if (s[i] == ')') {
        if (st.size() == 0) {
          success = false;
          break;
        }

        char c = st.top();
        st.pop();
        if (c != '(')
          success = false;
      }
      if (s[i] == ']') {
        if (st.size() == 0) {
          success = false;
          break;
        }

        char c = st.top();
        st.pop();
        if (c != '[')
          success = false;
      }
    }
    if (st.size() != 0)
      success = false;

    if (success) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}
Problem p00723 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}

int main(void) {
  int n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    set<string> abcd;
    for (int i = 1; i < str.length(); i++) {
      string str1, str2;
      str1 = str.substr(0, i);
      str2 = str.substr(i, str.length() - i);
      abcd.insert(str1 + str2);
      abcd.insert(str2 + str1);
      abcd.insert(rev(str1) + str2);
      abcd.insert(str1 + rev(str2));
      abcd.insert(rev(str2) + str1);
      abcd.insert(str2 + rev(str1));
      abcd.insert(rev(str2) + rev(str1));
      abcd.insert(rev(str1) + rev(str2));
    }
    cout << abcd.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define printit                                                                \\
  for (int i = 0; i < abcd.size(); i++)                                        \\
    cout << abcd[i].first << " " << abcd[i].second << endl;

string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}

int main(void) {
  int n;
  cin >> n;
  while (n--) {
    int size;
    string str;
    cin >> str;
    vector<pair<string, string>> abcd;
    for (int i = 1; i < str.length(); i++) {
      pair<string, string> p;
      p.first = str.substr(0, i);
      p.second = str.substr(i, str.length() - i);
      abcd.push_back(p);
    }
    size = abcd.size();
    for (int i = 0; i < size; i++) {
      abcd.push_back(make_pair(abcd[i].second, abcd[i].first));
    }
    size = abcd.size();
    for (int i = 0; i < size; i++) {
      abcd.push_back(make_pair(rev(abcd[i].first), abcd[i].second));
      abcd.push_back(make_pair(abcd[i].first, rev(abcd[i].second)));
      abcd.push_back(make_pair(rev(abcd[i].first), rev(abcd[i].second)));
    }
    size = abcd.size();
    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        if (i == j)
          continue;
        if (abcd[i].first + abcd[i].second == abcd[j].first + abcd[j].second) {
          abcd[j].first = "";
          abcd[j].second = "";
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < abcd.size(); i++) {
      if (abcd[i].first != "")
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00723 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string r(string s);
int main() {
  int n, i, j, k, l, f[8] = {};
  cin >> n;
  string str, s1, s2;
  for (i = 0; i < n; i++) {
    cin >> str;
    int si = str.size();
    vector<string> v;
    for (j = 1; j < si; j++) {
      string s[8];
      s1 = str.substr(0, j);
      s2 = str.substr(j, si - j);
      f[0] = 0;
      f[1] = 0;
      f[2] = 0;
      f[3] = 0;
      f[4] = 0;
      f[5] = 0;
      f[6] = 0;
      f[7] = 0;
      s[0] = s1 + s2;
      s[1] = s2 + s1;
      s[2] = r(s2) + s1;
      s[3] = s2 + r(s1);
      s[4] = s1 + r(s2);
      s[5] = r(s1) + s2;
      s[6] = r(s1) + r(s2);
      s[7] = r(s2) + r(s1);
      for (k = 0; k < v.size(); k++) {
        for (l = 0; l < 8; l++)
          if (s[l] == v[k])
            f[l] = 1;
      }
      for (k = 1; k < 8; k++)
        for (l = 0; l < k; l++)
          if (s[k] == s[l])
            f[k] = 1;

      for (l = 0; l < 8; l++)
        if (f[l] == 0)
          v.push_back(s[l]);
    }
    cout << v.size() << endl;
  }
  return 0;
}
string r(string s) {
  string b = "";
  int i;
  for (i = 0; i < s.size(); i++)
    b += s[s.size() - i - 1];
  return b;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string r(string s);
int main() {
  int n, i, j, k, f;
  cin >> n;
  string str, s1, s2;
  for (i = 0; i < n; i++) {
    cin >> str;
    int s = str.size();
    vector<string> v;
    for (j = 1; j < s; j++) {
      s1 = str.substr(0, j);
      s2 = str.substr(j, s - j);
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (s1 + s2 == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(s1 + s2);
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (s2 + s1 == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(s2 + s1);
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (r(s2) + s1 == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(r(s2) + s1);
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (s2 + r(s1) == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(s2 + r(s1));
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (s1 + r(s2) == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(s1 + r(s2));
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (r(s1) + s2 == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(r(s1) + s2);
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (r(s1) + r(s2) == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(r(s1) + r(s2));
      f = 0;
      for (k = 0; k < v.size(); k++) {
        if (r(s2) + r(s1) == v[k])
          f = 1;
      }
      if (f == 0)
        v.push_back(r(s2) + r(s1));
    }
    cout << v.size() << endl;
  }
  return 0;
}
string r(string s) {
  string b = "";
  int i;
  for (i = 0; i < s.size(); i++)
    b += s[s.size() - i - 1];
  return b;
}
Problem p00723 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
  int m;
  cin >> m;

  for (int i = 0; i < m; i++) {
    set<string> pattern;
    string arrival;
    cin >> arrival;
    for (int j = 1; j < arrival.size(); j++) {
      string train[2];
      train[0] = arrival.substr(0, j);
      train[1] = arrival.substr(j);

      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);

      reverse(train[0].begin(), train[0].end());
      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);

      reverse(train[1].begin(), train[1].end());
      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);

      reverse(train[0].begin(), train[0].end());
      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);
    }
    cout << pattern.size() << endl;
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
  int m;
  cin >> m;

  set<string> pattern;
  for (int i = 0; i < m; i++) {
    string arrival;
    cin >> arrival;
    for (int j = 1; j < arrival.size(); j++) {
      string train[2];
      train[0] = arrival.substr(0, j);
      train[1] = arrival.substr(j);
      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);

      reverse(train[0].begin(), train[1].end());
      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);

      reverse(train[1].begin(), train[1].end());
      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);

      reverse(train[0].begin(), train[1].end());
      pattern.insert(train[0] + train[1]);
      pattern.insert(train[1] + train[0]);
    }
    cout << pattern.size();
  }

  return 0;
}
Problem p00723 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
  int m;
  string str;
  cin >> m;

  while (m--) {
    set<string> train;
    cin >> str;

    for (unsigned int i = 1; i < str.size(); i++) {
      string str1[2], str2[2];
      str1[0] = str.substr(0, i);
      str1[1] = "";

      for (int j = str1[0].size() - 1; j >= 0; j--) {
        str1[1] += str1[0][j];
      }

      str2[0] = str.substr(i);
      str2[1] = "";

      for (int j = str2[0].size() - 1; j >= 0; j--) {
        str2[1] += str2[0][j];
      }

      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
          if (train.empty())
            train.insert(str1[j] + str2[k]);
          if (train.find(str1[j] + str2[k]) == train.end())
            train.insert(str1[j] + str2[k]);
          if (train.find(str2[j] + str1[k]) == train.end())
            train.insert(str2[j] + str1[k]);
        }
      }
    }
    cout << train.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int m;
  string str;
  cin >> m;

  while (m--) {
    string train[10000];
    int p = 0;
    cin >> str;

    for (unsigned int i = 1; i < str.size(); i++) {
      string str1[2], str2[2];
      str1[0] = str.substr(0, i);
      str1[1] = "";

      for (int j = str1[0].size() - 1; j >= 0; j--) {
        str1[1] += str1[0][j];
      }

      str2[0] = str.substr(i);
      str2[1] = "";

      for (int j = str2[0].size() - 1; j >= 0; j--) {
        str2[1] += str2[0][j];
      }

      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 2; k++) {
          int flag = 1;
          if (!(p)) {
            train[p] = str1[j] + str2[k];
            p++;
          }

          for (int l = 0; l < p; l++) {
            if (train[l] == str1[j] + str2[k]) {
              flag = 0;
              break;
            }
          }

          if (flag) {
            train[p] = str1[j] + str2[k];
            p++;
          }

          flag = 1;

          for (int l = 0; l < p; l++) {
            if (train[l] == str2[j] + str1[k]) {
              flag = 0;
              break;
            }
          }

          if (flag) {
            train[p] = str2[j] + str1[k];
            p++;
          }
        }
      }
    }

    cout << p << endl;
  }
}
Problem p00723 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int INF = 10000000;

string memo[1000];
int memoMax = 0;

int setstr(string str) {
  for (int i = 0; i < memoMax; i++) {
    if (str == memo[i])
      return 0;
  }
  memo[memoMax++] = str;
  return 1;
}

string strrev(string str) {
  for (int i = 0; i < str.length() / 2; i++) {
    char c = str[i];
    str[i] = str[str.length() - i - 1];
    str[str.length() - i - 1] = c;
  }
  return str;
}

int cast(string str) {
  int len = str.length();

  int sum = 0;
  sum += setstr(str);

  for (int i = 1; i < len; i++) {
    string str1 = str.substr(0, i);
    string str2 = str.substr(i, len - i);

    sum += setstr(str1 + strrev(str2));
    sum += setstr(strrev(str1) + str2);
    sum += setstr(strrev(str1) + strrev(str2));

    sum += setstr(str2 + str1);
    sum += setstr(str2 + strrev(str1));
    sum += setstr(strrev(str2) + str1);
  }

  sum += setstr(strrev(str));

  memoMax = 0;

  return sum;
}

int main() {
  int m;
  string str;

  cin >> m;

  for (int i = 0; i < m; i++) {

    cin >> str;

    cout << cast(str) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int INF = 10000000;

string memo[40000];
int memoMax = 0;

int setstr(string str) {
  for (int i = 0; i < memoMax; i++) {
    if (str == memo[i])
      return 0;
  }
  memo[memoMax++] = str;
  return 1;
}

string strrev(string str) {
  for (int i = 0; i < str.length() / 2; i++) {
    char c = str[i];
    str[i] = str[str.length() - i - 1];
    str[str.length() - i - 1] = c;
  }
  return str;
}

int cast(string str) {
  int len = str.length();

  int sum = 0;
  sum += setstr(str);

  for (int i = 1; i < len; i++) {
    string str1 = str.substr(0, i);
    string str2 = str.substr(i, len - i);

    sum += setstr(str1 + strrev(str2));
    sum += setstr(strrev(str1) + str2);
    sum += setstr(strrev(str1) + strrev(str2));

    sum += setstr(str2 + str1);
    sum += setstr(str2 + strrev(str1));
    sum += setstr(strrev(str2) + str1);
  }

  sum += setstr(strrev(str));

  return sum;
}

int main() {
  int m;
  string str;

  cin >> m;

  for (int i = 0; i < m; i++) {

    cin >> str;

    cout << cast(str) << endl;
  }

  return 0;
}
Problem p00723 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  while (n--) {
    set<string> kinds;
    string str;
    cin >> str;

    for (int i = 1; i < str.size(); i++) {
      string s1 = str.substr(i), s2 = str.substr(0, i);
      string r1 = s1, r2 = s2;
      reverse(r1.begin(), r1.end());
      reverse(r2.begin(), r2.end());

      string s[8] = {
          s1 + s2, s1 + r2, r1 + s2, r1 + r2,
          s2 + s1, r2 + s1, s2 + r1, r2 + r1,
      };
      for (int j = 0; j < 8; j++)
        kinds.insert(s[j]);
    }
    cout << kinds.size() << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  while (n--) {
    set<string> kinds;
    string str;
    cin >> str;

    for (int i = 1; i < str.size(); i++) {
      string s1 = str.substr(i), s2 = str.substr(0, i);
      string r1 = s1, r2 = s2;
      reverse(r1.begin(), r1.end());
      reverse(r2.begin(), r2.end());

      string s[8] = {
          s1 + s2, s1 + r2, r1 + s2, r1 + r2,
          s2 + s1, r2 + s1, s2 + r1, r2 + r1,
      };
      for (int j = 0; j < 8; j++)
        kinds.insert(s[i]);
    }
    cout << kinds.size() << endl;
  }
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)

typedef long long ll;

int h, w, ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

bool isok(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }

int dfs(int i, int j, int s, vector<vector<int>> a) {
  if (s > 10)
    return 0;
  REP(k, 4) {
    int x = i + dx[k], y = j + dy[k];
    if (!isok(x, y) || a[x][y] == 1)
      continue;
    while (isok(x, y)) {
      if (a[x][y] == 3) {
        ans = min(ans, s);
        break;
      }
      if (a[x][y] == 1) {
        a[x][y] = 0;
        x -= dx[k];
        y -= dy[k];
        dfs(x, y, s + 1, a);
        a[x + dx[k]][y + dy[k]] = 1;
        break;
      }
      x += dx[k];
      y += dy[k];
    }
  }
  return 0;
}

int main(void) {
  while (cin >> w >> h && w) {
    vector<vector<int>> a(h, vector<int>(w));
    int x, y;
    REP(i, h) {
      REP(j, w) {
        cin >> a[i][j];
        if (a[i][j] == 2) {
          x = i;
          y = j;
          a[i][j] = 0;
        }
      }
    }
    ans = 11;
    dfs(x, y, 1, a);

    if (ans > 10)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)

typedef long long ll;

int h, w, ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

bool isok(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }

int dfs(int i, int j, int s, vector<vector<int>> a) {
  if (s > 10)
    return 0;
  REP(k, 4) {
    int x = i + dx[k], y = j + dy[k];
    vector<vector<int>> b(a);
    if (!isok(x, y) || a[x][y] == 1)
      continue;
    while (isok(x, y)) {
      if (a[x][y] == 3) {
        ans = min(ans, s);
        break;
      }
      if (a[x][y] == 1) {
        b[x][y] = 0;
        x -= dx[k];
        y -= dy[k];
        dfs(x, y, s + 1, b);
        break;
      }
      x += dx[k];
      y += dy[k];
    }
  }
  return 0;
}

int main(void) {
  while (cin >> w >> h && w) {
    vector<vector<int>> a(h, vector<int>(w));
    int x, y;
    REP(i, h) {
      REP(j, w) {
        cin >> a[i][j];
        if (a[i][j] == 2) {
          x = i;
          y = j;
          a[i][j] = 0;
        }
      }
    }
    ans = 11;
    dfs(x, y, 1, a);

    if (ans > 10)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <limits.h>
#include <string>
#include <vector>

using namespace std;

int map[100][100];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int w, h, out;

void rec(int x, int y, int num) {
  int nx, ny;
  if (num >= out)
    return;

  for (int i = 0; i <= 3; i++) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (map[ny][nx] == 1)
      continue;
    if (nx < 0 || nx >= w)
      continue;
    if (ny < 0 || ny >= h)
      continue;

    while (1) {

      if (map[ny][nx] == 3) {
        out = num;
        return;
      }

      nx += dx[i];
      ny += dy[i];

      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        break;

      if (map[ny][nx] == 1) {
        map[ny][nx] = 0;
        rec(nx - dx[i], ny - dy[i], num + 1);
        map[ny][nx] = 1;
        break;
      }
    }
  }
}

int main() {
  while (cin >> w >> h, (w || h)) {
    int sx, sy;

    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        cin >> map[j][i];

        if (map[j][i] == 2) {
          sx = i;
          sy = j;
        }
      }
    }

    out = 11;
    rec(sx, sy, 1);

    if (out > 10)
      out = -1;

    cout << out << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>
#include <string>
#include <vector>

using namespace std;

int map[100][100];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int w, h, out;

void rec(int x, int y, int num) {
  int nx, ny;
  if (num > out)
    return;

  for (int i = 0; i <= 3; i++) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (map[ny][nx] == 1)
      continue;
    if (nx < 0 || nx >= w)
      continue;
    if (ny < 0 || ny >= h)
      continue;

    while (1) {

      if (map[ny][nx] == 3) {
        out = num;
        return;
      }

      nx += dx[i];
      ny += dy[i];

      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        break;

      if (map[ny][nx] == 1) {
        map[ny][nx] = 0;
        rec(nx - dx[i], ny - dy[i], num + 1);
        map[ny][nx] = 1;
        break;
      }
    }
  }
}

int main() {
  while (cin >> w >> h, (w || h)) {
    int sx, sy;
    out = INT_MAX;

    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        cin >> map[j][i];

        if (map[j][i] == 2) {
          sx = i;
          sy = j;
        }
      }
    }

    out = INT_MAX;
    rec(sx, sy, 1);

    if (out > 10)
      out = -1;

    cout << out << endl;
  }

  return 0;
}