POJ - 3278 Catch That Cow
2016-02-24 20:25
369 查看
1.题面
http://poj.org/problem?id=32782.解题思路
标准的bfs,在坐标轴上有一个点,你可以选择将这个点向正方向或是负方向移动一步,或是讲他的坐标×2,使用queue进行bfs搜索,顺便使用set判重,愉快地过了3.解题代码
/***************************************************************** > File Name: tmp.cpp > Author: Uncle_Sugar > Mail: uncle_sugar@qq.com > Created Time: 2016年02月18日 星期四 20时54分36秒 ****************************************************************/ # include <cstdio> # include <cstring> # include <cmath> # include <queue> # include <cstdlib> # include <iostream> # include <iomanip> # include <set> # include <map> # include <vector> # include <stack> using namespace std; const int debug = 1; const int size = 100000 + 10; typedef long long ll; typedef pair<int,int> pir; char vis[size]; bool inrange(int x){ return x>=0&&x<=size; } int solve(int s,int e){ static queue<pir> que; static int next[3]; while(!que.empty()) que.pop(); memset(vis,0,sizeof(vis)); if (s==e) return 0; que.push(pir(s,0));vis[s] = 1; while (!que.empty()){ pir T = que.front();que.pop(); int step = T.second + 1; next[0] = T.first + 1; next[1] = T.first - 1; next[2] = 2*T.first; for (int i=0;i<3;i++){ if (inrange(next[i])&&vis[next[i]]==0){ if (next[i]==e) return step; que.push(pir(next[i],step));vis[next[i]] = 1; } } } return -1; } int main() { std::ios::sync_with_stdio(false);cin.tie(0); int i,j,k; int s,e; while (cin >> s >> e){ cout << solve(s,e) << endl; } return 0; }
相关文章推荐
- sessionStorage用法
- 相关性矩阵的灰阶图
- GDKOI总结
- 从上往下打印二叉树
- 系统架构
- 图像低频高频区域分离
- java生产者消费者模式练习
- asp.net Repeater控件使用
- C++基础知识易错点总结(1)
- 突破阈限,持续努力
- ICPCCamp 2016 Day 7 - Moscow SU Trinity Contest(Hans Zimmer-期望)
- C++11之基于范围的for循环
- C#之构造方法
- PAT-1026 程序运行时间
- 原始套接字SOCK_RAW发送UDP数据包
- BigDecimal取余运算
- BigDecimal取余运算
- 网络交换卡
- GDKOI总结
- BigDecimal取余运算