UVa 12160 - Unlock the Lock
// Accepted
// you may use array instead of map to reduce more time and even optimized BFS.
#include<bits/stdc++.h>
using namespace std;
int L, U, R;
const int high = 10010;
const int mod = 10000;
typedef long long LL;
int ar[high];
typedef map<LL, LL>mpll;
mpll visited, cost;
void BFS(int start, int desti)
{
int u , v;
queue<int>Q;
Q.push(start);
while(!Q.empty())
{
u = Q.front();
Q.pop();
for(int i=0; i<R; i++)
{
LL sum = (u + ar[i])%mod;
if(visited[sum] == 0)
{
visited[sum] = 1;
cost[sum] = cost[u] + 1;
Q.push(sum);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
int i=0 , tc=0;
while(cin >> L >> U >> R)
{
if(!L && !U && !R) break;
visited.clear();
cost.clear();
for(i=0; i<R; i++)
{
cin >> ar[i];
}
BFS(L, U);
cout << "Case " << ++tc << ": ";
if(!cost[U]) cout << "Permanently Locked\n";
else cout << cost[U] << "\n";
//cout << cost[U] << "\n";
}
return 0;
}
// you may use array instead of map to reduce more time and even optimized BFS.
#include<bits/stdc++.h>
using namespace std;
int L, U, R;
const int high = 10010;
const int mod = 10000;
typedef long long LL;
int ar[high];
typedef map<LL, LL>mpll;
mpll visited, cost;
void BFS(int start, int desti)
{
int u , v;
queue<int>Q;
Q.push(start);
while(!Q.empty())
{
u = Q.front();
Q.pop();
for(int i=0; i<R; i++)
{
LL sum = (u + ar[i])%mod;
if(visited[sum] == 0)
{
visited[sum] = 1;
cost[sum] = cost[u] + 1;
Q.push(sum);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
int i=0 , tc=0;
while(cin >> L >> U >> R)
{
if(!L && !U && !R) break;
visited.clear();
cost.clear();
for(i=0; i<R; i++)
{
cin >> ar[i];
}
BFS(L, U);
cout << "Case " << ++tc << ": ";
if(!cost[U]) cout << "Permanently Locked\n";
else cout << cost[U] << "\n";
//cout << cost[U] << "\n";
}
return 0;
}
Comments
Post a Comment