BFS+打印路径
题目是给你起点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+打印路径的更多相关文章
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- 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 ...
- UVA-816.Abbott's Tevenge (BFS + 打印路径)
本题大意:给定一个迷宫,让你判断是否能从给定的起点到达给定的终点,这里起点需要输入起始方向,迷宫的每个顶点也都有行走限制,每个顶点都有特殊的转向约束...具体看题目便知... 本题思路:保存起点和终点 ...
- 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 ...
- POJ 3414--Pots(BFS+回溯路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9963 Accepted: 4179 Special Ju ...
- UVA-10480-Sabotage(最大流最小割,打印路径)
链接: https://vjudge.net/problem/UVA-10480 题意: The regime of a small but wealthy dictatorship has been ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- bfs输出路径 && 最短路(迪杰斯特拉)输出路径
问题描述 解决方法 1.像第一个问题那就是最短路问题(我代码采用迪杰斯特拉算法)实现 2.换乘次数最少,那就用bfs广搜来寻找答案.但是我的代码不能保证这个最少换乘是最短路程 代码 1 #includ ...
- LCS(打印路径) POJ 2250 Compromise
题目传送门 题意:求单词的最长公共子序列,并要求打印路径 分析:LCS 将单词看成一个点,dp[i][j] = dp[i-1][j-1] + 1 (s1[i] == s2[j]), dp[i][j] ...
随机推荐
- Memcache--02 源码安装nginx,php
目录 一.session共享问题介绍 二.环境准备 一.session共享问题介绍 session主要用于服务端存储用户会话信息,cookie用于浏览器存储用户会话信息. 单系统服务session都存 ...
- rename 重命名文件
1. 使用范例 范例1: 批量修改文件名 [root@localhost data]# touch {a,b,c,d,e}.txt [root@localhost data]# ls a.txt ...
- selenium下拉一个框内的滚动条
js='document.getElementsByClassName("route-tree")[0].scrollTop=10000'
- [CF] 8C Looking for Order
状压模板题 CF难度2000? 我得好好了解一下CF的难度机制了 反正CF的难度比洛谷真实就好了 Code #include<algorithm> #include<iostream ...
- bzoj3991 [SDOI2015]寻宝游戏 树链的并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3991 题解 貌似这个东西叫做树链的并,以前貌似写过一个类似的用来动态维护虚树. 大概就是最终的 ...
- 修改Win7登陆界面墙纸
修改Win7登陆界面墙纸 修改注册表.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\W ...
- 数组与List互转的坑
一.数组转List 非原始类型的数组转List有很多种方式:假设有Integer[] arr = {"a", "b", "c"}; 1.Li ...
- php怎么启动exe文件
PHP作为一种服务器端的脚本语言,象编写简单,或者是复杂的动态网页这样的任务,它完全能够胜任.但事情不总是如此,有时为了实现某个功能,必须借助于操作系统的外部程序(或者称之为命令),这样可以做到事半功 ...
- 2A3T我的PMP备考及考试心得20181208
2018年的下半年由于工作不是很忙,所以生活中有更好的精力去做些自己的事情.出于工作需要,我决定考个证书充实下自己,在各大网站搜索解惑后决定考PMP,并报了个培训班 一.PMP考试简介 共200道选择 ...
- machine learning 之 Recommender Systems
整理自Andrew Ng的machine learning 课程 week 9. 目录: Problem Formulation(问题的形式) Content Based Recommendation ...