-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOOKMACH_wrong.cpp
More file actions
92 lines (85 loc) · 1.79 KB
/
COOKMACH_wrong.cpp
File metadata and controls
92 lines (85 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<stack>
#include<cstring>
using namespace std;
stack<int> st;
inline int scan()
{
register int n=0;
register char c;
c=getchar_unlocked();
while( c<'0' || c>'9' )
c=getchar_unlocked();
while( c>='0' && c<='9' )
{
n=n*10+c-'0';
c=getchar_unlocked();
}
return n;
}
bool visited[10000007]; // number of visited nodes
inline void test()
{
int s,f; int tmp,tmp2,tmp3;
memset(visited,0,sizeof(visited));
s=scan(); f=scan(); // s-> initial , f-> final
register int i,j;
int curr_level=0; // stores the curr level number
int curr_count=0; // stores the current count of level
int next_count=0; // no. of nodes in the next level
while( !st.empty() )
st.pop();
st.push(s);
st.push(1);
while( !st.empty() ) // while empty or the number is found
{
curr_count=st.top();
st.pop();
cout<<"curr count: "<<curr_count<<endl;
next_count=0;
while(curr_count--) // for all the nodes in the current level
{
tmp=st.top();
cout<<" popped item : "<<tmp<<endl;
visited[tmp]=true;
st.pop();
if( tmp==f )
{
cout<<curr_level<<endl;
return;
}
else
{
// cout<<" else"<<endl;
if( tmp%2==0 && (tmp/2)!=0 && !visited[tmp/2] )
{
next_count++;
st.push(tmp/2);
}
else if( tmp%2==1 && ((tmp-1)/2)!=0 && !visited[(tmp-1)/2] )
{
next_count++;
st.push((tmp-1)/2);
}
if( (2*tmp)<=10000000 && !visited[2*tmp] )
{
next_count++;
st.push(2*tmp);
}
}
}
st.push(next_count);
// cout<<" next_count : "<<next_count<<endl;
curr_level++;
}
}
int main()
{
int t=scan();
while(t--)
test();
return 0;
}