hrust OJ 1316 移动II
来源:互联网 发布:c语言进程的创建 编辑:程序博客网 时间:2024/05/01 05:44
移动 IITime Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 107(29 users)Total Accepted: 59(28 users)Rating: Special Judge: NoDescription
在坐标轴[0,500]上存在两点A,B。
点A可以多次移动,每次移动需要遵循如下规则:
1.向后移动一步。
2.向前移动一步。
3.跳到当前坐标*2的位置上。
要求:利用宽搜算法编程求解从A移动到B的步数最少的方案,为使答案统一,要求搜索按照规则1、2、3的顺序进行。
Input输入包含多组测试用例。
每组测试用例要求输入两个整数A,B。
Output按要求输出步数最少的方案。
向后走输出"step back"。
向前走输出"step forward"。
跳跃输出"jump"。
对于每组结果需要追加一个空行。
Sample Input5 175 183 499Sample Output
step backjumpjumpstep forwardjumpstep backjumpstep forwardjumpjumpjumpstep backjumpjumpstep forwardjumpjumpstep back//广搜+路径记录
#include<iostream>#include<map>#include<queue>#include<string.h>using namespace std;int mark[505];int root[505];int A, B;void bfs(){ queue<int>J; int temp; J.push(A); mark[A]=1; while(J.size()) { temp=J.size(); while(temp--) { int next=J.front(); if(next==B) return ; if(next-1<=500&&next-1>=0&&mark[next-1]==0) { root[next-1]=next; J.push(next-1); mark[next-1]=1; } if(next+1>=0&&next+1<=500&&mark[next+1]==0) { root[next+1]=next; J.push(next+1); mark[next+1]=1; } if(2*next>=0&&2*next<=500&&mark[2*next]==0) { root[2*next]=next; J.push(2*next); mark[2*next]=1; } J.pop(); } }}void print(int i){ if(i==A) return ; print(root[i]); if(i==root[i]-1) cout << "step back" << endl; else if(i==root[i]+1) cout << "step forward" << endl; else cout << "jump" << endl;}int main(){ while(cin >> A >> B) { memset(mark, 0, sizeof(mark)); bfs(); print(B); cout << endl; } return 0;}
- hrust OJ 1316 移动II
- 哈理工OJ 1316 移动 II (广度优先搜索+路径打印)
- 移动 II
- 移动 II
- LeetCode OJ:Subsets II
- LeetCode OJ:Permutations II
- LeetCode OJ - Subsets II
- LeetCode OJ Permutations II
- LeetCode OJ Subsets II
- [OJ ] Majority Element II
- 华为oj 坐标移动
- 【OJ中级】坐标移动
- 华为OJ 坐标移动
- 华为oj 坐标移动
- 华为Oj 坐标移动
- 华为oj 坐标移动
- OJ------坐标移动
- 华为oj--坐标移动
- Android
- MySQL索引的查看创建和删除
- POJ3260:The Fewest Coins(混合背包)
- Python学习(十三)------- GUI程序设计
- 【C++】fwrite与fread
- hrust OJ 1316 移动II
- 黑马程序员----【javaSE基础】IO--字节流
- hdu 4619(sap解二分匹配)
- hdu4300
- C#操作Informix数据库
- 又议android中的manifest清单文件
- 黑马程序员----【javaSE基础】IO--字符流
- UVA 350 Pseudo-Random Numbers
- C++虚函数与虚函数表(编译器查看)