Problem p02245 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

typedef std::vector<std::vector<int>> TPuzzle;

void MakeGoal(TPuzzle &Goal) {
  Goal.resize(3);
  for (int i = 0; i < 3; ++i) {
    Goal[i].resize(3);
    for (int j = 0; j < 3; ++j) {
      Goal[i][j] = (1 + 3 * i + j) % 9;
    }
  }
}

void ZeroPosition(int &Row, int &Col, const TPuzzle &Puzzle) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (Puzzle[i][j] == 0) {
        Row = i;
        Col = j;
      }
    }
  }
}

enum TMovePattern { mpUp, mpDown, mpLeft, mpRight };

bool Movable(const TPuzzle &Puzzle, const TMovePattern &Pattern) {
  int Row, Col;
  ZeroPosition(Row, Col, Puzzle);
  switch (Pattern) {
  case mpUp:
    return Row != 0;
  case mpDown:
    return Row != 2;
  case mpLeft:
    return Col != 0;
  case mpRight:
    return Col != 2;
  }
  assert(false);
  return false;
}

TPuzzle Move(TPuzzle Puzzle, const TMovePattern &Pattern) {
  int Row, Col;
  ZeroPosition(Row, Col, Puzzle);
  switch (Pattern) {
  case mpUp:
    std::swap(Puzzle[Row][Col], Puzzle[Row - 1][Col]);
    break;
  case mpDown:
    std::swap(Puzzle[Row][Col], Puzzle[Row + 1][Col]);
    break;
  case mpLeft:
    std::swap(Puzzle[Row][Col], Puzzle[Row][Col - 1]);
    break;
  case mpRight:
    std::swap(Puzzle[Row][Col], Puzzle[Row][Col + 1]);
    break;
  }
  return Puzzle;
}

void RenewTargets(const TPuzzle &Puzzle, const TMovePattern &Pattern,
                  std::vector<TPuzzle> *Targets, std::set<TPuzzle> &History) {
  if (!Movable(Puzzle, Pattern))
    return;
  TPuzzle Moved = Move(Puzzle, Pattern);
  if (History.find(Moved) != History.end())
    return;
  Targets->push_back(Moved);
  History.insert(Moved);
}
//?¬??????????????????????????????¶???????????????

//????¬??????????????????????????¨?????????£???????????°?????????
//?¬?????????????????????????????????§?????????????????????????????????
//???????????§????±\\??´????????£????????????
//?¬???????????????????????????????????????????????????????
//?±\\??´????????????
//????????????
int CountMove(std::vector<TPuzzle> *NextTargets, const TPuzzle &Goal, int Count,
              std::set<TPuzzle> &History) {
  if (std::find(NextTargets->begin(), NextTargets->end(), Goal) !=
      NextTargets->end())
    return Count;
  std::vector<TPuzzle> *NewTargets = new std::vector<TPuzzle>();
  for (std::vector<TPuzzle>::const_iterator It = NextTargets->begin();
       It != NextTargets->end(); ++It) {
    RenewTargets(*It, mpUp, NewTargets, History);
    RenewTargets(*It, mpDown, NewTargets, History);
    RenewTargets(*It, mpLeft, NewTargets, History);
    RenewTargets(*It, mpRight, NewTargets, History);
  }
  delete NextTargets;
  return CountMove(NewTargets, Goal, 1 + Count, History);
}

int CountMinimumMove(const TPuzzle &Puzzle, const TPuzzle &Goal) {
  std::set<TPuzzle> History;
  return CountMove(new std::vector<TPuzzle>(1, Puzzle), Goal, 0, History);
}

int main() {
  TPuzzle Goal;
  MakeGoal(Goal);
  TPuzzle Start(3, std::vector<int>(3));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      int Input;
      std::cin >> Input;
      Start[i][j] = Input;
    }
  }
  std::cout << CountMinimumMove(Start, Goal) << std::endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

typedef std::vector<std::vector<int>> TPuzzle;

void MakeGoal(TPuzzle &Goal) {
  Goal.resize(3);
  for (int i = 0; i < 3; ++i) {
    Goal[i].resize(3);
    for (int j = 0; j < 3; ++j) {
      Goal[i][j] = (1 + 3 * i + j) % 9;
    }
  }
}

void ZeroPosition(int &Row, int &Col, const TPuzzle &Puzzle) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (Puzzle[i][j] == 0) {
        Row = i;
        Col = j;
      }
    }
  }
}

enum TMovePattern { mpUp, mpDown, mpLeft, mpRight };

bool Movable(const TPuzzle &Puzzle, const TMovePattern &Pattern) {
  int Row, Col;
  ZeroPosition(Row, Col, Puzzle);
  switch (Pattern) {
  case mpUp:
    return Row != 0;
  case mpDown:
    return Row != 2;
  case mpLeft:
    return Col != 0;
  case mpRight:
    return Col != 2;
  }
  assert(false);
  return false;
}

TPuzzle Move(TPuzzle Puzzle, const TMovePattern &Pattern) {
  int Row, Col;
  ZeroPosition(Row, Col, Puzzle);
  switch (Pattern) {
  case mpUp:
    std::swap(Puzzle[Row][Col], Puzzle[Row - 1][Col]);
    break;
  case mpDown:
    std::swap(Puzzle[Row][Col], Puzzle[Row + 1][Col]);
    break;
  case mpLeft:
    std::swap(Puzzle[Row][Col], Puzzle[Row][Col - 1]);
    break;
  case mpRight:
    std::swap(Puzzle[Row][Col], Puzzle[Row][Col + 1]);
    break;
  }
  return Puzzle;
}

void RenewTargets(const TPuzzle &Puzzle, const TMovePattern &Pattern,
                  std::vector<TPuzzle> &Targets, std::set<TPuzzle> &History) {
  if (!Movable(Puzzle, Pattern))
    return;
  TPuzzle Moved = Move(Puzzle, Pattern);
  if (History.find(Moved) != History.end())
    return;
  Targets.push_back(Moved);
  History.insert(Moved);
}
//?¬??????????????????????????????¶???????????????

//????¬??????????????????????????¨?????????£???????????°?????????
//?¬?????????????????????????????????§?????????????????????????????????
//???????????§????±\\??´????????£????????????
//?¬???????????????????????????????????????????????????????
//?±\\??´????????????
//????????????
int CountMove(const std::vector<TPuzzle> &NextTargets, const TPuzzle &Goal,
              int Count, std::set<TPuzzle> &History) {
  if (std::find(NextTargets.begin(), NextTargets.end(), Goal) !=
      NextTargets.end())
    return Count;
  std::vector<TPuzzle> NewTargets;
  for (std::vector<TPuzzle>::const_iterator It = NextTargets.begin();
       It != NextTargets.end(); ++It) {
    RenewTargets(*It, mpUp, NewTargets, History);
    RenewTargets(*It, mpDown, NewTargets, History);
    RenewTargets(*It, mpLeft, NewTargets, History);
    RenewTargets(*It, mpRight, NewTargets, History);
  }
  return CountMove(NewTargets, Goal, 1 + Count, History);
}

int CountMinimumMove(const TPuzzle &Puzzle, const TPuzzle &Goal) {
  std::set<TPuzzle> History;
  return CountMove(std::vector<TPuzzle>(1, Puzzle), Goal, 0, History);
}

int main() {
  TPuzzle Goal;
  MakeGoal(Goal);
  TPuzzle Start(3, std::vector<int>(3));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      int Input;
      std::cin >> Input;
      Start[i][j] = Input;
    }
  }
  std::cout << CountMinimumMove(Start, Goal) << std::endl;
  return 0;
}