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

#include <algorithm>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

typedef std::pair<int, int> P;

std::vector<std::vector<int>> map;
int x, y;
int ans = std::numeric_limits<int>::max(); //??????

void dfs(int count, std::vector<std::pair<int, int>> delta, int posi,
         int posj) { //???????????§??????????????????
  if (count > 10) {
    return;
  }
  if (map[posi][posj] == 3) {
    ans = std::min(ans, count);
    return;
  }
  auto copy = map;
  for (auto i = 0; i < delta.size(); i++) {
    copy[delta[i].first][delta[i].second] =
        0; //??????????????£????????????????§????
  }
  if (posi + 1 < y) {
    if (copy[posi + 1][posj] != 1) {
      for (int i = posi; i < y; i++) {
        if (copy[i][posj] == 1) {
          auto deltacopy = delta;
          deltacopy.push_back(std::make_pair(i, posj));
          dfs(count + 1, deltacopy, i - 1, posj);
          break;
        }
        if (copy[i][posj] == 3) {
          dfs(count + 1, delta, i, posj);
          break;
        }
      }
    }
  }
  if (posi - 1 >= 0) {
    if (copy[posi - 1][posj] != 1) {
      for (int i = posi; i >= 0; i--) {
        if (copy[i][posj] == 1) {
          auto deltacopy = delta;
          deltacopy.push_back(std::make_pair(i, posj));
          dfs(count + 1, deltacopy, i + 1, posj);
          break;
        }
        if (copy[i][posj] == 3) {
          dfs(count + 1, delta, i, posj);
          break;
        }
      }
    }
  }
  if (posj + 1 < x) {
    if (copy[posi][posj + 1] != 1) {
      for (int j = posj; j < x; j++) {
        if (copy[posi][j] == 1) {
          auto deltacopy = delta;
          deltacopy.push_back(std::make_pair(posi, j));
          dfs(count + 1, deltacopy, posi, j - 1);
          break;
        }
        if (copy[posi][j] == 3) {
          dfs(count + 1, delta, posi, j);
          break;
        }
      }
    }
  }
  if (posj - 1 >= 0) {
    if (copy[posi][posj - 1] != 1) {
      for (int j = posj; j >= 0; j--) {
        if (copy[posi][j] == 1) {
          auto deltacopy = delta;
          deltacopy.push_back(std::make_pair(posi, j));
          dfs(count + 1, deltacopy, posi, j + 1);
          break;
        }
        if (copy[posi][j] == 3) {
          dfs(count + 1, delta, posi, j);
          break;
        }
      }
    }
  }
}

int main() {
  while (cin >> x >> y, x * y) {
    map.resize(y);
    int si, sj;
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        int A;
        cin >> A;
        map[i].push_back(A);
        if (A == 2)
          si = i, sj = j;
      }
    }
    dfs(0, {}, si, sj);
    if (ans == std::numeric_limits<int>::max())
      ans = -1;
    std::cout << ans << std::endl;
    ans = std::numeric_limits<int>::max();
    map.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

std::vector<std::vector<int>> map;
int x, y;
int ans = 10000000; //??????

void dfs(int count, std::vector<std::vector<int>> _map, int posi,
         int posj) { //???????????§??????????????????
  if (count > 10) {
    ans = 10000000;
    return;
  }
  bool f = 1;
  if (posi + 1 < x) {
    if (_map[posi + 1][posj] == 1)
      f *= 1;
    else
      f *= 0;
  }
  if (posi - 1 >= 0) {
    if (_map[posi - 1][posj] == 1)
      f *= 1;
    else
      f *= 0;
  }
  if (posj + 1 < y) {
    if (_map[posi][posj + 1] == 1)
      f *= 1;
    else
      f *= 0;
  }
  if (posj - 1 >= 0) {
    if (_map[posi][posj - 1] == 1)
      f *= 1;
    else
      f *= 0;
  }
  if (f) {
    return;
  }
  if (_map[posi][posj] == 3) {
    ans = std::min(ans, count);
    return;
  }
  //??????????????????
  for (int i = posi; i >= 0; i--) {
    if (_map[i][posj] == 1) {
      auto copy = _map;
      copy[i][posj] = 0;
      dfs(count + 1, copy, i + 1, posj);
      break;
    }
    if (_map[i][posj] == 3) {
      dfs(count + 1, _map, i, posj);
      break;
    }
  }
  for (int i = posi; i < x; i++) {
    if (_map[i][posj] == 1) {
      auto copy = _map;
      copy[i][posj] = 0;
      dfs(count + 1, copy, i - 1, posj);
      break;
    }
    if (_map[i][posj] == 3) {
      dfs(count + 1, _map, i, posj);
      break;
    }
  }
  for (int j = posj; j >= 0; j--) {
    if (_map[posi][j] == 1) {
      auto copy = _map;
      copy[posi][j] = 0;
      dfs(count + 1, copy, posi, j + 1);
      break;
    }
    if (_map[posi][j] == 3) {
      dfs(count + 1, _map, posi, j);
      break;
    }
  }
  for (int j = posj; j < y; j++) {
    if (_map[posi][j] == 1) {
      auto copy = _map;
      copy[posi][j] = 0;
      dfs(count + 1, copy, posi, j - 1);
      break;
    }
    if (_map[posi][j] == 3) {
      dfs(count + 1, _map, posi, j);
      break;
    }
  }
}

int main() {
  while (cin >> x >> y, x * y) {
    map.resize(x);
    int si, sj;
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        int A;
        cin >> A;
        map[i].push_back(A);
        if (A == 2)
          si = i, sj = j;
      }
    }
    dfs(0, map, si, sj);
    if (ans == 10000000)
      ans = -1;
    std::cout << ans << std::endl;
    ans = 10000000;
    map.clear();
  }
  return 0;
}