标签:info find 自然数 tor follow string nts hint long
---恢复内容开始---
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14433 Accepted Submission(s):
4396
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<string> #include<cmath> #include<set> #include<queue> using namespace std; const int N_MAX = 100000+1; set<int>s; queue<int>que; int dp[N_MAX*10]; void bfs(int begin,int end) { s.insert(begin); que.push(begin); while (!que.empty()) { int dx = que.front(); que.pop(); for (int i = 0; i < 3; i++) { int x; switch (i) { case 0: if(dx-1>=0&&dx-1<N_MAX) x = dx - 1; break; case 1: if (dx + 1 < N_MAX&&dx+1>=0) x = dx + 1; break; case 2: if (2*dx>=0&&2 * dx < N_MAX) x = dx * 2; break; } set<int>::iterator it = s.find(x); if (it == s.end()) {//x这个点还没去过 s.insert(x); dp[x] = dp[dx] + 1; if (x == end)return;//找到end,返回 que.push(x); } } } } int main() { int N, K; while (scanf("%d%d", &N, &K)!=EOF) { memset(dp, 0, sizeof(dp)); bfs(N, K); ////// while (!que.empty()) que.pop(); ////// s.clear(); printf("%d\n", dp[K]); } return 0; }
标签:info find 自然数 tor follow string nts hint long
原文地址:http://www.cnblogs.com/ZefengYao/p/6731022.html