BFS 3071. Number Game

本文深入探讨了游戏开发领域的核心技术,包括Unity3D、Cocos2d-X等游戏引擎的使用,以及OpenGL ES、OpenCV等图像处理技术在游戏中的应用。同时,还涉及了AR特效、音视频直播流媒体等前沿技术,旨在为开发者提供全面的游戏开发知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is an interesting game. We start with a number K. Then three operations is allowed:

1. Multiply current number by 2
2. Divide current number by 2, if current number is even.
3. Increase current number by 1

Our target is to get number P with minimum operations. You can assume 0 < P,K < 100000, and the number can not be greater than or equal to 100000 at any time.

Input

The first line is an integer T, the number of test cases. Then T cases follows.

Each test case contain two numbers K and P in one line separated by a space.

Output

Output the minimum number of operations. If it is impossible to get P from K, output -1 instead.

Sample Input

2
11 20
99999 50000

Sample Output

7

-1

#include<iostream>
using namespace std;
const int n=100000;
int visit[n];
struct num{
	int x,step;
}c[n];
int bfs(int s,int t){
	memset(visit,0,sizeof(visit));
	visit[s]=1;
	c[0].x=s;
	c[0].step=0;
	int top=1,temp;
	for(int i=0;i<top;i++){
		if(c[i].x==t)
			return c[i].step;
		temp=c[i].x*2;
		if(temp<n&&!visit[temp]){
			visit[temp]=1;
			c[top].step=c[i].step+1;
			c[top].x=temp;
			top++;
		}
		if(c[i].x%2==0){
			temp=c[i].x/2;
			if(temp>0&&!visit[temp]){
				visit[temp]=1;
				c[top].step=c[i].step+1;
				c[top].x=temp;
				top++;
			}
		}
		temp=c[i].x+1;
		if(temp<n&&!visit[temp]){
			visit[temp]=1;
			c[top].step=c[i].step+1;
			c[top].x=temp;
			top++;
		}
	}
	return -1;
}
int main(){
	int N,s,t;
	cin>>N;
	while(N--){
		cin>>s>>t;
		cout<<bfs(s,t)<<endl;
	}
	return 0;
}


#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 0x7ffffff #define rep(i,s,t) for(register ll i = s;i <= t;++i) #define per(i,t,s) for(register ll i = t;i >= s;--i) const ll N = 25; struct node { ll x; ll y; ll rnk; bool operator < (const node& u) const { return rnk < u.rnk; } bool operator == (const node& u) const { return x == u.x && y == u.y && rnk == u.rnk; } }; ll n; ll m; ll ans = INF; ll dx[5] = {0,0,1,0,-1}; ll dy[5] = {0,1,0,-1,0}; char a[N][N] = {}; set <node> s; inline ll read() { ll x = 0; ll y = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') y = -y; c = getchar(); } while(c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ '0'); c = getchar(); } return x * y; } inline void write(ll x) { if(x < 0) { putchar('-'); write(-x); return; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); } inline bool posfind(node tmp,set <node> cur) { for(register auto i : cur) if(i.x == tmp.x && i.y == tmp.y) return true; return false; } inline void bfs() { queue <pair<set <node>,ll> > q; q.push({s,0}); while(!q.empty()) { set <node> cur = q.front().first; ll stp = q.front().second; q.pop(); node head = *cur.begin(); if(a[head.x][head.y] == '@') { ans = stp; break; } ll cnt = 0; set <node> tmp; for(register auto i : cur) { cnt++; if(cnt == 1 || cnt == cur.size()) continue; tmp.insert({i.x,i.y,i.rnk + 1}); } rep(i,1,4) { ll nx = head.x + dx[i]; ll ny = head.y + dy[i]; node tmp1 = {nx,ny,1}; if(posfind(tmp1,tmp) || a[nx][ny] == '#' || nx < 1 || nx > n || ny < 1 || ny > m) continue; set <node> tmp2 = tmp; tmp2.insert(tmp1); q.push({tmp2,stp + 1}); } } } int main() { cin >> n >> m; rep(i,1,n) { rep(j,1,m) { cin >> a[i][j]; if(a[i][j] >= '0' && a[i][j] <= '9') s.insert({i,j,(ll)(a[i][j] - '0')}); } } bfs(); if(ans == INF) cout << -1; else cout << ans; return 0; }请针对以下问题优化上述代码的时间空间复杂度,极度精确保留源代码风格# CF225D Snake ## 题目描述 Let us remind you the rules of a very popular game called "Snake" (or sometimes "Boa", "Python" or "Worm"). The game field is represented by an $ n×m $ rectangular table. Some squares of the field are considered impassable (walls), all other squares of the fields are passable. You control a snake, the snake consists of segments. Each segment takes up exactly one passable square of the field, but any passable square contains at most one segment. All segments are indexed by integers from $ 1 $ to $ k $ , where $ k $ is the snake's length. The $ 1 $ -th segment is the head and the $ k $ -th segment is the tail. For any $ i $ ( $ 1<=i<k $ ), segments with indexes $ i $ and $ i+1 $ are located in the adjacent squares of the field, that is, these squares share a common side. One of the passable field squares contains an apple. The snake's aim is to reach the apple and eat it (that is, to position its head in the square with the apple). The snake moves throughout the game. During one move the snake can move its head to an adjacent field square. All other segments follow the head. That is, each segment number $ i $ $ (1<i<=k) $ moves to the square that has just had segment number $ i-1 $ . Consider that all segments including the head move simultaneously (see the second test sample). If the snake's head moves to an unpassable square or to the square, occupied by its other segment, the snake dies. That's why we will consider such moves unvalid. Your task is to determine the minimum number of valid moves that the snake needs to reach the apple. ## 输入格式 The first line contains two space-separated integers $ n $ and $ m $ ( $ 1<=n,m<=15 $ ) — the number of rows and columns of the game field. Next $ n $ lines describe the game field. Each of these lines contains $ m $ characters. Character "\#" represents a wall, "." is a passable square, "@" is an apple. The snake's first segment is represented by character "1", the second one segment — by character "2" and so on. The game field description doesn't contain any characters besides "\#', ".", "@" and digits (except 0). It is guaranteed that the described field is correct. It is guaranteed that the described field contains exactly one apple and exactly one snake, the snake's length is at least 3 and at most 9. ## 输出格式 Print a single integer to the output — the minimum number of moves needed to reach the apple. If the snake can't reach the apple, print -1. ## 输入输出样例 #1 ### 输入 #1 ``` 4 5 ##... ..1#@ 432#. ...#. ``` ### 输出 #1 ``` 4 ``` ## 输入输出样例 #2 ### 输入 #2 ``` 4 4 #78# .612 .543 ..@. ``` ### 输出 #2 ``` 6 ``` ## 输入输出样例 #3 ### 输入 #3 ``` 3 2 3@ 2# 1# ``` ### 输出 #3 ``` -1 ```
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值