Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <queue>
- #define MAXN 10000
- #include <vector>
- string change(string input; int arg){
- int i,a,b;
- if(arg==0){
- if(input[0]!=9)
- input[0]++;
- return input;
- }
- if(arg==1){
- if(input[3]!=0)
- input[3]--;
- return input;
- }
- if(arg=2){
- a=input[0];
- for(i=0;i<4;i++){
- if(i=3)
- input[i]=a;
- input[i]=input[i+1];
- }
- }
- if(arg=3){
- a=input[3];
- for(i=3;i>-1;i--){
- if(i=0)
- input[i]=a;
- input[i]=input[i-1];
- }
- }
- }
- struct numb{string name; string previous;};
- bool used[MAXN];
- vector bfs (numb start;numb finish){
- int prev[MAXN]
- queue<numb> q;
- vector res;
- q.push(start);
- used[start]=true;
- numb cur;
- int j=0;
- cur=start;
- prev[cur.name]=start
- res.push_back(finish);
- while(!q.empty()){
- for(int i=0;i<4;i++){
- if(used[change(cur.name,i)]==false){
- cur.previous=cur.name;
- cur.name=change(cur.name,i);
- prev[cur.name]=cur.previous;
- q.push(cur);
- used[cur.name]=true;
- if(cur.name==finish){
- j=1;
- }
- }
- }
- if(j=0){
- cur=q.front();
- q.pop();
- }
- else{
- while(cur.name!=start.name){
- res.push_back(prev[cur.name]);
- cur.name=prev[cur.name];
- }
- return res;
- }
- }
- }
- int main(){
- numb start, finish;
- vector res;
- cin>>start.name; finish.name;
- res=bfs(start; finish);
- for(inti=res.size-1;i>=0;i--){
- cout<< res[i];
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment