裸BFS题若干
1poj 3278
http://poj.org/problem?id=3278
#include<math.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
#define N 100005
#define M 155
#define INF 0x3f3f3f3f
struct node
{
int x, t;
}st;
int n, k, a, b, x, y, t, cnt, ans;
bool v[N];
void Bfs(int n)
{
ans = 0;
queue<node>q;
while(!q.empty()) q.pop();
memset(v, 0, sizeof(v));
st.x = n;
st.t = 0;
q.push(st);
v[st.x] = 1;
while(!q.empty())
{
node head = q.front();
q.pop();
if(head.x == k)
{
ans = head.t;
return;
}
for(int i = 0; i < 3; i++)
{
node next = head;
if(i == 0) next.x = head.x + 1;
else if(i == 1) next.x = head.x - 1;
else next.x = head.x * 2;
next.t = head.t + 1;
if(next.x < 0 || next.x > 100000) continue;
if(v[next.x] == 1 ) continue;
// 一开始写成一行,if(v[next.x] == 1 || next.x < 0 || next.x > 100000) continue; 找了好久的bug,以后能分开的还是分开吧,不要省那一两行
q.push(next);
v[next.x] = 1;
}
}
}
int main()
{
while(cin>>n>>k){
Bfs(n);
cout<<ans<<endl;
}
return 0;
}
裸BFS题若干的更多相关文章
- USACO 3.2 msquare 裸BFS
又是个裸BFS... 和西安网赛那道1006一样的,只不过加上了要记录方案.顺便复习map 记录方案直接在bfs队列的结点里加一个vector<int> opt,把从开头一直到当前结点的操 ...
- 一道很经典的 BFS 题
一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...
- D. Kilani and the Game 解析(裸BFS、實作)
Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...
- HDU 1728 逃离迷宫 BFS题
题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...
- POJ 水题若干
POJ 3176 Cow Bowling 链接: http://poj.org/problem?id=3176 这道题可以算是dp入门吧.可以用一个二维数组从下向上来搜索从而得到最大值. 优化之后可以 ...
- HDU1372:Knight Moves(经典BFS题)
HDU1372:Knight Moves(BFS) Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- zzuli2130卡时bfs题
https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130 2130: hipercijevi Time Limit: 1 Sec Memory Li ...
- POJ-1274The Perfect Stall,二分匹配裸模板题
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23313 Accepted: 103 ...
- tarjan讲解(用codevs1332(tarjan的裸题)讲解)
主要借助这道比较裸的题来讲一下tarjan这种算法 tarjan是一种求解有向图强连通分量的线性时间的算法.(用dfs来实现) 如果两个顶点可以相互通达,则称两个顶点强连通.如果有向图G的每两个顶点都 ...
随机推荐
- Pycharm2019.1.3破解
搬运: T3ACKYHDVF-eyJsaWNlbnNlSWQiOiJUM0FDS1lIRFZGIiwibGljZW5zZWVOYW1lIjoi5bCP6bifIOeoi+W6j+WRmCIsImFzc ...
- tensorflow学习笔记七----------RNN
和神经网络不同的是,RNN中的数据批次之间是有相互联系的.输入的数据需要是要求序列化的. 1.将数据处理成序列化: 2.将一号数据传入到隐藏层进行处理,在传入到RNN中进行处理,RNN产生两个结果,一 ...
- vim比较文件
横向分割显示: $ vim -o filename1 filename2 纵向分割显示: $ vim -O filename1 filename2 ctl w w 切换文件
- php内置函数分析之current()、next()、prev()、reset()、end()
current()初始指向插入到数组中的第一个单元 next() 将数组的内部指针向前移动一位 prev() 将数组的内部指针倒回一位 reset() 将数组的内部指针指向第一个单元 end() 将数 ...
- 如何删除Github上的仓库
1.首先,进入自己的github账户页面,点击头像选择下面的Your repositorys,点击进入 2.进入以后选中自己要删除的仓库,点击进去该仓库界面 3.找到Settings按钮,点击进入 4 ...
- java编写算法题格式(链表和二叉树)
(1)链表 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; ...
- 安装mod_rpaf让apache获取访客真实IP
安装mod_rpaf让apache获取访客真实IP 安装mod_rpaf让apache获取访客真实IP 作者:朱 茂海 /分类:Apache 字号:L M S mod_rpaf是apache ...
- 【leetcode】1146. Snapshot Array
题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) ini ...
- iOS 指定位置切圆角不生效问题
如果是在VC中操作,需要在viewDidLayoutSubviews方法里 - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; ...
- spring boot不要放在tomcat下启动,因为自身就带了集成tomcat
spring boot不要放在tomcat下启动,因为自身就带了集成tomcat