Problem Link
Problem Type : BFS.
Difficulty Level : 5(uHUNT)
Author : Imdad.
Here, is the c++ implementation of this problem.
Problem Type : BFS.
Difficulty Level : 5(uHUNT)
Author : Imdad.
Here, is the c++ implementation of this problem.
#include <cstdio>
#include <queue>
using namespace std;
typedef struct{
int val;
int step;
}node;
int main(){
int i,button[11],tmp,cas = 1,s,f,b,visited[10000];
bool found;
node current,new_node;
while(scanf("%d %d %d", &s, &f, &b ) && !(!s && !f && !b)){
queueQ;
found = false;
for(i = 0; i < 10000; i++)
visited[i] = 0;
for(i = 0; i < b; i++)
scanf("%d", &button[i]);
current.val = s;
current.step = 0;
visited[current.val] = 1;
Q.push(current);
while(!Q.empty()){
current = Q.front();
Q.pop();
if(current.val == f){
found = true;
printf("Case %d: %d\n", cas++, current.step);
}
for(i = 0; i < b; i++){
tmp = (current.val % 10000 + button[i] % 10000) % 10000;
if(!visited[tmp]){
new_node.val = tmp;
new_node.step = current.step + 1;
Q.push(new_node);
visited[tmp] = 1;
}
}
}
if(!found)
printf("Case %d: Permanently Locked\n",cas++);
}
return 0;
}

No comments:
Post a Comment