移动 II

移动 II
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 106(28 users) Total Accepted: 58(27 users) Rating:  Special Judge: No
Description

在坐标轴[0,500]上存在两点A,B。

点A可以多次移动,每次移动需要遵循如下规则:

1.向后移动一步。

2.向前移动一步。

3.跳到当前坐标*2的位置上。


要求:利用宽搜算法编程求解从A移动到B的步数最少的方案,为使答案统一,要求搜索按照规则1、2、3的顺序进行。

Input

输入包含多组测试用例。

每组测试用例要求输入两个整数A,B。

Output

按要求输出步数最少的方案。

向后走输出"step back"。

向前走输出"step forward"。

跳跃输出"jump"。

对于每组结果需要追加一个空行。

Sample Input
5 17
5 18
3 499
Sample Output
step back
jump
jump
step forward

jump
step back
jump

step forward
jump
jump
jump
step back
jump
jump
step forward
jump
jump
step back


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#include <stack>
using namespace std;
#pragma warning(disable : 4996)
const int MAXN = 600;

typedef struct Point
{
	int x;
	int cnt;
	int y; //1 step back 2 step forward 3 jump
}Point;
queue<Point>Q;
bool visited[MAXN];
Point path[1000000];

void bfs(int x, int y)
{
	while (!Q.empty())
	{
		Q.pop();
	}
	Point pre, next;
	pre.x = x;
	pre.cnt = 0;
	pre.y = -1;
	visited[pre.x] = true;
	Q.push(pre);
	while (!Q.empty())
	{
		pre = Q.front();
		Q.pop();

		if(pre.x == y)
		{
			return;
		}
		next.x = pre.x - 1;
		if(!visited[next.x] && next.x >= 0)
		{
			next.cnt = pre.cnt + 1;
			next.y = 1;
			path[next.x] = pre;
			visited[next.x] = true;
			Q.push(next);
		}

		next.x = pre.x + 1;
		if(!visited[next.x] && next.x >= 0 && next.x <= MAXN)
		{
			next.cnt = pre.cnt + 1;
			next.y = 2;
			path[next.x] = pre;
			visited[next.x] = true;
			Q.push(next);
		}

		next.x = pre.x * 2;
		if(!visited[next.x] && next.x >= 0 && next.x <= MAXN)
		{
			next.cnt = pre.cnt + 1;
			next.y = 3;
			path[next.x] = pre;
			visited[next.x] = true;
			Q.push(next);
		}
	}
}

void print(int n)
{
	Point pre;
	pre = path[n];
	if(pre.y != -1)
	{
		print(pre.x);
	}
	if(pre.y == 1)
	{
		printf("step back\n");
	}
	else if(pre.y == 2)
	{
		printf("step forward\n");
	}
	else if(pre.y == 3)
	{
		printf("jump\n");
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int n, m;
	while (scanf("%d %d", &n, &m) != EOF)
	{
		if(n == m)
		{
			printf("\n");
			continue;
		}
		else if (n > m)
		{
			for (int i = 1; i <= n - m; i++)
			{
				printf("step back\n");
			}
		}
		else
		{
			memset(visited, false, sizeof(visited));
			bfs(n, m);
			print(m);
			if(path[m].x == m + 1)
			{
				printf("step back\n");
			}
			else if(path[m].x == m - 1)
			{
				printf("step forward\n");
			}
			else if(path[m].x * 2 == m)
			{
				printf("jump\n");
			}
		}
		printf("\n");
	}
	return 0;
}


posted @ 2013-06-05 10:07  N3verL4nd  阅读(125)  评论(0编辑  收藏  举报