Catch That Cow
Time Limit: 2000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
#include <iostream> #include <string.h> #include <stdio.h> #include <queue> using namespace std; #define max 100002 ///看了别人的代码 说广搜的标记数组 会很大 如果开成局部的会PE bool vis[max]; int deep[max]; int n,k; int bfs() { queue<int>q; q.push(n); memset(vis,false,sizeof(vis)); ///全没有遍历过 vis[n]=true; ///起点遍历过了。。 memset(deep,0,sizeof(deep)); ///初始都定义为0 走了0步 17 deep[n]=0; while(!q.empty()) { int tp=q.front(); q.pop(); vis[tp]=true; if(tp==k) break; else { int ans1=tp+1; int ans2=tp-1; int ans3=tp*2; if(ans1<=100000&&!vis[ans1]) { q.push(ans1); deep[ans1]=deep[tp]+1; //cout<<deep[ans1]<<endl; vis[ans1]=true; } if(ans2>=0&&!vis[ans2]) ///注意边界条件 刚刚>0 WA了 { q.push(ans2); deep[ans2]=deep[tp]+1; //cout<<deep[ans2]<<endl; vis[ans2]=true; } if(ans3<=100000&&!vis[ans3]) { q.push(ans3); deep[ans3]=deep[tp]+1; // cout<<deep[ans3]<<endl; vis[ans3]=true; } } } return deep[k]; } int main() { cin>>n>>k; printf("%d\n",bfs()); return 0; }
第一道广搜题 参考别人的代码~~~~http://kymowind.blog.163.com/blog/static/1842222972011717112727688/
poj1426 http://blog.csdn.net/lyy289065406/article/details/6647917
Prime Path
Time Limit: 1000 MS Memory Limit: 65536 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
1733
3733
3739
3779
8779
8179
Input
Output
Sample Input
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
题意:从第一个数编导第二个数最少需要多少步 要求:每次只能改变一个数且这个数与之前的数都不一样 同时这个数时素数
#include <iostream> #include <string.h> #include <stdio.h> #include <queue> #include <math.h> using namespace std; int prime[10000]; void inin() { int i,j; for(i=1000;i<=10000;i++){ for(j=2;j<i;j++) if(i%j==0){ prime[i]=false; break; } if(j==i) prime[i]=true; } } int vis[10000]; ///是否遍历过 int t[6]; ///存放各个位的数 int num[10000]; ///最后输出的值 int bfs(int n,int m) { int temp; queue<int>q; q.push(n); memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); vis[n]=1; memset(t,0,sizeof(t)); while(!q.empty()) { int tp=q.front(); q.pop(); if(tp==m) return num[tp]; t[0]=tp/1000; t[1]=tp%1000/100; t[2]=tp%100/10; t[3]=tp%10; for(int i=0;i<4;i++) { temp=t[i]; for(int j=0;j<=9;j++) if(temp!=j) { t[i]=j; int ans=t[0]*1000+t[1]*100+t[2]*10+t[3]; if(!vis[ans]&&prime[ans]) { num[ans]=num[tp]+1; vis[ans]=1; q.push(ans); } if(ans==m) return num[ans]; } t[i]=temp; } } return -1; } int main() { int n,m,tt; inin(); cin>>tt; while(tt--) { cin>>n>>m; if(bfs(n,m)!=-1) cout<<bfs(n,m)<<endl; else cout<<"Impossible"<<endl; } return 0; }
之前判断素数的函数不是这样写的 一直WA 可是我的错在哪里了呢 先贴上 以后看看
int prim(int n) { for(int i=2;i<sqrt(n);i++) { if(n%i==0) { return false; break; } } return true; }