Monday, August 22, 2016

UVa 12160 - Unlock the Lock

// UVa 12160 - Unlock the Lock

#include <iostream>
#include <string.h>
#include <queue>
#include <stdio.h>
using namespace std;

int main() {
	int l, u, rr, cases = 0;
	while (cin >> l >> u >> rr && (l || u || rr)) {
		cases++;
		int r[10];
		for (int i = 0; i < rr; i++)
			cin >> r[i];
		int T[10000];
		memset(T, 127, sizeof(T));
		queue<int> q;
		q.push(l);
		T[l] = 0;
		bool found = false;
		while (!q.empty() && !found) {
			int f = q.front();
			q.pop();
			for (int i = 0; i < rr; i++) {
				int j = (f + r[i]) % 10000;
				if (T[j] > T[f] + 1) {
					T[j] = T[f] + 1;
					q.push(j);
					if (j == u) {
						found = true;
						break;
					}
				}
			}
		}
		if (found)
			printf("Case %d: %d\n", cases, T[u]);
		else
			printf("Case %d: Permanently Locked\n", cases);
	}
	return 0;
}

No comments:

Post a Comment