POJ3278http://poj.org/problem?id=3278
2015-07-20 13:36
597 查看
http://poj.org/problem?id=3278
题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步
题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<queue> #define max(a, b)(a > b ? a : b) #define N 100010 using namespace std; struct node { int x, step; }; int m, n; bool vis ; int judge(int x) { if(x <= 100000 && x >= 0 && !vis[x]) return 1; return -1; } int BFS(int x) { queue<node>Q; node now, next; now.x = x; now.step = 0; vis[now.x] = true; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); if(now.x == n) return now.step; next.x = now.x + 1; if(judge(next.x) == 1) { vis[next.x] = true; next.step = now.step + 1; Q.push(next); } next.x = now.x - 1; if(judge(next.x) == 1) { vis[next.x] = true; next.step = now.step + 1; Q.push(next); } next.x = now.x * 2; if(judge(next.x) == 1) { vis[next.x] = true; next.step = now.step + 1; Q.push(next); } } return -1; } int main() { while(scanf("%d%d", &m, &n)!= EOF) { memset(vis, false, sizeof(vis)); printf("%d\n", BFS(m)); } return 0; }
相关文章推荐
- TCP连接的TIME-WAIT状态
- HTTP状态码大全
- 黑马程序员---------Java网络编程
- python调用tcpdump抓包过滤
- 计算机网络读书笔记-----数据链路层
- linux 网络编程-基础篇
- Nginx源代码分析之网络超时管理(九)
- cocos2dx 获取网络图片
- TCP/IP详解学习笔记(2)--IP协议
- 计算机网络
- nginx使用ssl模块配置HTTPS支持
- iOS 网络编程 NSURLSessionDataTask 添加cookie
- HTTP协议详解
- 专业工具,网络随行
- 失控阅读笔记自然之流变
- 基于Google Volley的Android Http连接库
- 实例讲解httpd虚拟主机的配置以及https的配置
- Android http请求代码
- [机器迁移]如何通过网络快速传输海量(小)文件
- Android 开源框架Universal-Image-Loader加载https图片