zoukankan      html  css  js  c++  java
  • Catch That Cow

    #include <iostream>
    #include <queue>
    #include <cstring>
    using namespace std;
    
    struct node {
    	int x;
    	int step;
    };
    
    int n, k, vis[100010];
    node s, e;
    
    int bfs()
    {
    	queue<node> q;
    	node t, p;
    	s.x = n;
    	s.step = 0;
    	vis[s.x] = 1;
    	q.push(s);
    	while(!q.empty())
    	{
    		t = q.front();
    		q.pop();
    		
    		if(t.x == e.x)	return t.step;
    		if(t.x < 0 || t.x > 100000)
    			continue;
    		
    		// 向右走一步 
    		if(t.x + 1 <= 100000 && vis[t.x + 1] == 0)
    		{
    			p.x = t.x + 1;
    			p.step = t.step + 1;
    			vis[p.x] = 1;
    			q.push(p);
    		}
    		
    		// 向左走一步 
    		if(t.x - 1 >= 0 && vis[t.x - 1] == 0)
    		{
    			p.x = t.x - 1;
    			p.step = t.step + 1;
    			vis[p.x] = 1;
    			q.push(p);
    		}
    		
    		// 向右走两倍步数 
    		if(t.x * 2 <= 100000 && vis[t.x * 2] == 0)
    		{
    			p.x = t.x * 2;
    			p.step = t.step + 1;
    			vis[p.x] = 1;
    			q.push(p);
    		}
    	}
    	return 0;
    }
    
    int main()
    {
    	int result;
    	while(cin >> n >> k)
    	{
    		memset(vis, 0, sizeof(vis));
    		s.x = n, e.x = k;
    		result = bfs();
    		cout << result << endl;
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    抽卡 状压DP+期望DP+系数递推
    20190903考试反思
    20190823考试反思
    约瑟夫类问题研究
    树位DP
    20190823考试反思
    20190820考试反思
    20190818考试反思
    20190817考试反思
    PowerBuilder--Aes128加解密
  • 原文地址:https://www.cnblogs.com/mjn1/p/11630994.html
Copyright © 2011-2022 走看看