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

#include <iostream>
#include <queue>
#include <string>

#define MAX_K 10

using namespace std;

struct dial {
  dial() : depth(0) {
    for (int i = 0; i < MAX_K; ++i)
      d[i] = -1;
  }
  char depth;
  char d[MAX_K];
  int diff(const dial &t) const {
    int ret = 0;
    for (int i = 0; i < MAX_K; ++i) {
      if (d[i] != t.d[i])
        ++ret;
    }
    return ret;
  }
};

bool DialLock(int limit, int K, const dial &now, const dial &unlock) {
  if (now.depth > limit)
    return false;

  if (0 == unlock.diff(now)) {
    return true;
  }

  for (int i = 0; i < K; ++i) {
    int rot;
    if (now.d[i] != unlock.d[i]) {
      dial t = now;

      ++t.depth;
      rot = unlock.d[i] - now.d[i];
      rot += (rot < 0 ? MAX_K : 0);

      for (int m = 0; m < K - i; ++m) {
        t.d[i + m] = (now.d[i + m] + rot) % MAX_K;
        if (DialLock(limit, K, t, unlock))
          return true;
      }

      break;
    }
  }
  return false;
}
/*
int _DialLock(int K, const dial &init, const dial &unlock){
        int ret = 0;
        queue<dial> qd;

        qd.push( init );
        while( !qd.empty() ){
                dial now=qd.front();
                qd.pop();

                for(int i = 0; i < K; ++i){
                        cout << now.d[i] << ' ';
                }
                cout << endl;

                if( 0 == unlock.diff( now ) ){
                        ret = now.depth;
                        break;
                }

                for(int i = 0; i < K; ++i){
                        int rot;
                        if( now.d[i] != unlock.d[i] ){
                                dial t = now;

                                ++t.depth;
                                rot = unlock.d[i] - now.d[i];
                                rot += (rot<0?MAX_K:0);

                                for(int m = 0; m < K - i; ++m){
                                        t.d[i+m]=(now.d[i+m]+rot)%MAX_K;
                                        qd.push( t );
                                }

                                break;
                        }
                }
        }

        return ret;
}
*/

int main() {
  while (true) {
    int k;
    string a, b;
    dial init, unlock;

    cin >> k;
    if (k == 0)
      break;
    cin >> a >> b;
    for (int i = 0; i < k; ++i) {
      init.d[i] = a[i] - '0';
      unlock.d[i] = b[i] - '0';
    }
    init.depth = 0;

    for (int i = 0; i <= k; ++i) {
      if (DialLock(i, k, init, unlock)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>

#define MAX_K 10

using namespace std;

struct dial {
  dial() : depth(0) {
    for (int i = 0; i < MAX_K; ++i)
      d[i] = -1;
  }
  char depth;
  char d[MAX_K];
  int diff(const dial &t) const {
    int ret = 0;
    for (int i = 0; i < MAX_K; ++i) {
      if (d[i] != t.d[i])
        ++ret;
    }
    return ret;
  }
};

int DialLock(int K, const dial &init, const dial &unlock) {
  int ret = 0;
  queue<dial> qd;

  qd.push(init);
  while (!qd.empty()) {
    dial now = qd.front();
    qd.pop();

    /*
    for(int i = 0; i < K; ++i){
            cout << now.d[i] << ' ';
    }
    cout << endl;
    */

    if (0 == unlock.diff(now)) {
      ret = now.depth;
      break;
    }

    for (int i = 0; i < K; ++i) {
      int rot;
      if (now.d[i] != unlock.d[i]) {
        dial t = now;

        ++t.depth;
        rot = unlock.d[i] - now.d[i];
        rot += (rot < 0 ? MAX_K : 0);

        for (int m = 0; m < K - i; ++m) {
          t.d[i + m] = (now.d[i + m] + rot) % MAX_K;
          qd.push(t);
        }

        break;
      }
    }
  }

  return ret;
}

int main() {
  while (true) {
    int k;
    string a, b;
    dial init, unlock;

    cin >> k;
    if (k == 0)
      break;
    cin >> a >> b;
    for (int i = 0; i < k; ++i) {
      init.d[i] = a[i] - '0';
      unlock.d[i] = b[i] - '0';
    }
    init.depth = 0;

    cout << DialLock(k, init, unlock) << endl;
  }
  return 0;
}