[問題解]Catch That Cowあの牛を捕まえてC++
11482 ワード
Catch That Cowあの牛を捕まえて題 Description Input Output Sample Input Sample Output 構想 コード タイトル
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?
農夫のジョンは乳牛が逃げたと知らせられた!だから彼は、すぐに幽発して、できるだけ早くその乳牛を捕まえて、彼らはすべて数軸の上に立っています.ジョンはN(O≦N≦100000)のところにいて、乳牛はK(O≦K≦100000)のところにいます.ジョンは2つの方法で移動して、歩いて瞬時に移動します:歩いて毎秒ジョンをxのところからx+1あるいはx-1のところまで歩かせることができます;瞬移は1秒以内にxから消え、2 xに現れる.しかし、逃げた乳牛は、悲劇的に自分の立場がどんなに悪いのか気づかず、そこに立って動かない.では、ジョンはどのくらいの時間でその牛を捕まえるのだろうか.
Input Line 1:Two space-separated integers:N and Kは2つの整数NとK. しかない
Output Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow. 最短時間. Sample Input
5 17 Farmer John starts at point 5 and the fugitive cow is at point 17.
Sample Output
4 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.
構想
典型的なbfsボード問題は、1次元配列で行い、for 0~1拡張前1布と後退1布のたびに、さらに2 xステップ拡張し、境界マークが配列がちょうど100000であれば、境界を判断してもREになり、さらに半分多く開くと大丈夫です
コード#コード#
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?
農夫のジョンは乳牛が逃げたと知らせられた!だから彼は、すぐに幽発して、できるだけ早くその乳牛を捕まえて、彼らはすべて数軸の上に立っています.ジョンはN(O≦N≦100000)のところにいて、乳牛はK(O≦K≦100000)のところにいます.ジョンは2つの方法で移動して、歩いて瞬時に移動します:歩いて毎秒ジョンをxのところからx+1あるいはx-1のところまで歩かせることができます;瞬移は1秒以内にxから消え、2 xに現れる.しかし、逃げた乳牛は、悲劇的に自分の立場がどんなに悪いのか気づかず、そこに立って動かない.では、ジョンはどのくらいの時間でその牛を捕まえるのだろうか.
Input
Output
5 17 Farmer John starts at point 5 and the fugitive cow is at point 17.
Sample Output
4 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.
構想
典型的なbfsボード問題は、1次元配列で行い、for 0~1拡張前1布と後退1布のたびに、さらに2 xステップ拡張し、境界マークが配列がちょうど100000であれば、境界を判断してもREになり、さらに半分多く開くと大丈夫です
コード#コード#
#include
using namespace std;
int n,k;
int dir[2]={1,-1};
struct node{
int x;
int d;
node(int xx,int dd){
x=xx;
d=dd;
}
};
bool in(int x){
return x>=0 && x<=150000;
}
bool vis[150005];
int bfs(int sx){
queue<node> q;
if(sx==k){
return 0;
}
q.push(node(sx,0));
while(!q.empty()){
node now=q.front();
//q.pop();
for(int i=0;i<2;i++){
int tx=now.x+dir[i];
if(in(tx) && !vis[tx]){
vis[tx]=1;
if(tx==k){
return now.d+1;
}
q.push(node(tx,now.d+1));
}
}
int tx=2*now.x;
if(in(tx) && !vis[tx]){
vis[tx]=1;
if(tx==k){
return now.d+1;
}
q.push(node(tx,now.d+1));
}
}
}
int main(){
while(1);
string s;
cin>>n>>k;
getline(cin,s);
cout<<bfs(n);
return 0;
}