luogu P1588 丢失的牛 宽搜
1 #include <iostream> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 int main() 6 { 7 int t,x,y,loc,cnt; 8 int vis[210000]; 9 queue <int> que1,que2; 10 cin >> t; 11 while (t != 0) 12 { 13 while (que1.empty() == false) 14 que1.pop(); 15 while (que2.empty() == false) 16 que2.pop(); 17 memset(vis,0,sizeof(vis)); 18 19 cin >> x >> y; 20 que1.push(x); 21 que2.push(0); 22 while (que1.empty() == false) 23 { 24 loc = que1.front(); 25 cnt = que2.front(); 26 que1.pop(); 27 que2.pop(); 28 if (loc == y) 29 { 30 cout << cnt << endl; 31 break; 32 } 33 if (loc + 1 <= 2 * 100000 && vis[loc + 1] == 0) 34 { 35 vis[loc + 1] = 1; 36 que1.push(loc + 1); 37 que2.push(cnt + 1); 38 } 39 if (loc - 1 >= 1 && vis[loc - 1] == 0) 40 { 41 vis[loc - 1] = 1; 42 que1.push(loc - 1); 43 que2.push(cnt + 1); 44 } 45 if (loc * 2 <= 2 * 100000 && vis[2 * loc] == 0) 46 { 47 vis[loc * 2] = 1; 48 que1.push(loc * 2); 49 que2.push(cnt + 1); 50 } 51 } 52 t--; 53 } 54 return 0; 55 }
心之所动 且就随缘去吧