2012年12月2日

UVa 12160 - Unlock the Lock


#include <cstdio>
#include <queue>

int main() {
  int l, u, r, c = 1;
  while (scanf("%d%d%d", &l, &u, &r) && l || u || r) {
    int i, method[20];
    for (i = 0; i < r; i++)
      scanf("%d", &method[i]);
    printf("Case %d: ", c++);
    std::queue<int> q;
    int flag = 0 , d[10000] = {};
    q.push(l);
    while (!q.empty()) {
      int now = q.front(), dis = d[now];
      q.pop();
      if (now == u) {
        printf("%d\n", dis);
        flag = 1;
        break;
      }
      for (i = 0; i < r; i++) {
        int next = (now + method[i]) % 10000;
        if (!d[next] && next != l) {
          q.push(next);
          d[next] = dis + 1;
        }
      }
    }
    if (!flag) puts("Permanently Locked");
  }
  return 0;
}

沒有留言:

張貼留言