本文共 2309 字,大约阅读时间需要 7 分钟。
5 17
4 Hint The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
/*终于AC了 *///看了别人的代码 其实可以用结构体 建个数组记忆是否进过队 并且N不用那么大//N=100000+10就够了 因为太大也不可能是答案//优化空间可以改成 N=100000+10 //(不知道每次查长度需要时间不) 要的话改成结构体可以省时间 #include#include #include #include #include using namespace std;const int N=100000*6+10;queue q;bool book[N];int main(){ int a,b,re,tmp,i; while(scanf("%d%d",&a,&b)==2){ while(!q.empty()) q.pop(); re=0; memset(book,0,sizeof(book)); book[a]=1; q.push(a); while(!q.empty()){ int len=q.size(); for(i=0;i 0&&!book[tmp-1]){ book[tmp-1]=1; q.push(tmp-1); } if(tmp*2 #include #include using namespace std;const int N=100000;struct Node{ int v,t;}node,tmp;queue q;int main(){ int a,b; while(scanf("%d%d",&a,&b)==2){ while(!q.empty()) q.pop(); node.v=a; node.t=0; q.push(node); while(!q.empty()){ node=q.front(); q.pop(); if(node.v==b) break; tmp.v=node.v+1; tmp.t=node.t+1; q.push(tmp); tmp.v=node.v-1; tmp.t=node.t+1; q.push(tmp); tmp.v=node.v*2; tmp.t=node.t+1; q.push(tmp); } printf("%d\n",node.t); } return 0;}*//*想水一下 水不过 TLE#include #include using namespace std;const int N=100000;int b,re;void dfs(int now,int sum){ if(sum>re) return; else if(now==b){ if(sum
转载地址:http://dfmvi.baihongyu.com/