题目是给你起点sx,和终点gx;牛在起点可以进行下面两个操作:

步行:John花一分钟由任意点X移动到点X-1或点X+1。

瞬移:John花一分钟由任意点X移动到点2*X。

你要输出最短步数及打印路径。

最短步数用bfs就行了。

至于路径,用一个结构体就可以去存每个点的父节点,再递归输出路径就行了。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define MAX 100010
int sx,gx,ans;
struct mask
{
    mask(int x,int step):x(x),step(step){};
    int x,step,f;
};/
struct node
{
    int x;
}p[MAX];//保存父节点
queue<mask>q;
int dx[]={1,-1};
int flag=0;
bool vis[MAX];
bool check(int r)
{
    return (0<=r&&r<=MAX);
}
void bfs()
{
    flag=0;
    while(!q.empty())q.pop();
   q.push(mask(sx,0));
   memset(vis,false,sizeof(vis));
   vis[sx]=true;//printf("%d\n",q.front());
   while(q.size())
   {
      mask tmp=q.front();q.pop();
     // printf("ok\n");
      if(tmp.x==gx)
      {
          ans=tmp.step;
          flag=1;
          break;
      }
      for(int i=0;i<2;i++)
      {
          int nx=tmp.x+dx[i];
          if(check(nx)&&!vis[nx])
          {
              vis[nx]=true;
              p[nx].x=tmp.x;
              q.push(mask(nx,tmp.step+1));
          }
      }
      int nx1=tmp.x*2;
      if(check(nx1)&&!vis[nx1])
          {
              vis[nx1]=true;
                p[nx1].x=tmp.x;
              q.push(mask(nx1,tmp.step+1));
          }
   }
}
void pri(int x1)
{
    if(x1==sx){
        printf("%d ",sx);
        return  ;
    }
    pri(p[x1].x);
    printf("%d ",x1);
}
int main()
{
    while(~scanf("%d %d",&sx,&gx)){
     if(sx==-1&&gx==-1)break;
     bfs();
       if(flag)
         printf("%d\n",ans);
        else printf("-1\n");
        pri(gx);
        printf("\n");
    }
}

BFS+打印路径的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. Codeforces 3A-Shortest path of the king(BFS打印路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  3. UVA-816.Abbott's Tevenge (BFS + 打印路径)

    本题大意:给定一个迷宫,让你判断是否能从给定的起点到达给定的终点,这里起点需要输入起始方向,迷宫的每个顶点也都有行走限制,每个顶点都有特殊的转向约束...具体看题目便知... 本题思路:保存起点和终点 ...

  4. hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. POJ 3414--Pots(BFS+回溯路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 4179   Special Ju ...

  6. UVA-10480-Sabotage(最大流最小割,打印路径)

    链接: https://vjudge.net/problem/UVA-10480 题意: The regime of a small but wealthy dictatorship has been ...

  7. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  8. bfs输出路径 && 最短路(迪杰斯特拉)输出路径

    问题描述 解决方法 1.像第一个问题那就是最短路问题(我代码采用迪杰斯特拉算法)实现 2.换乘次数最少,那就用bfs广搜来寻找答案.但是我的代码不能保证这个最少换乘是最短路程 代码 1 #includ ...

  9. LCS(打印路径) POJ 2250 Compromise

    题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...

随机推荐

  1. sudo、su、suid

    sudo 是一种权限管理机制,管理员可以授权普通用户去执行 root 权限的操作,而不需要知道 root 的密码.sudo 以其他用户身份执行命令,默认以root身份执行.配置文件/etc/sudoe ...

  2. python-验证功能的装饰器示例

    user_list=[ {'}, {'}, {'} ] current_dict={'username':None,'login':False} def auth(auth_type): def au ...

  3. 动态规划—distinct-subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  4. 【LeetCode】未分类(tag里面没有)(共题)

    [334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...

  5. ltp-ddt eth_parallel_processing

    ETH_S_FUNC_PARALLEL_PROCESSING: source 'common.sh'; prepare_nfs_mount.sh "/mnt/nfs_mount"| ...

  6. php中判断数组键值,array_key_exists和isset区别

    $arr = array('key' => NULL); if(isset($arr['key'])){ echo 'isset'; } else { echo 'unset'; } echo ...

  7. Windows中的Work线程和GUI线程

    Windows线程分为两种:Worker线程.GUI线程 worker线程:是指完全不牵扯到图形用户界面(GUI),纯粹做运算的线程. GUI线程:负责建造窗口以及处理消息循环(拥有消息队列).任何一 ...

  8. django之静态文件的设置

    一:静态文件 Django中提供了一种解析的方式配置静态文件路径.静态文件可以放在项目根目录下,也可以放在应用的目录下,由于有些静态文件在项目中是通用的,所以推荐放在项目的根目录下,方便管理. 为了提 ...

  9. js reduce用法

    let books = [ 0, {bookName:"python",price:10,count:1}, {bookName:"Ruby",count:2, ...

  10. tuple写法

    name = ("wen") 类型为strname = ("wen",) 类型为tuple