牛客编程巅峰赛S1赛季第1场 - 青铜&白银局(重现赛) B 魔法数字 简单bfs

链接:https://ac.nowcoder.com/acm/contest/6448/B
来源:牛客网

题目描述
题意:
一天,牛妹找牛牛做一个游戏,牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的。
操作共有三种,如下:
1.在当前数字的基础上加一,如:4转化为5
2.在当前数字的基础上减一,如:4转化为3
3.将当前数字变成它的平方,如:4转化为16
你能帮牛牛解决这个问题吗?

输入:
给定n,m,分别表示牛牛和牛妹的数字。

输出:
返回最少需要的操作数。
示例1
输入
复制

3,10

输出
复制

2

备注:

(1≤n,m≤1000)(1\leq n,m\leq1000)(1≤n,m≤1000)

简单的bfs对于每个状态有三条路可以走+1,-1平方
c++代码

#define debug
#ifdef debug
#include 
#include "/home/majiao/mb.h"
#endif


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN ((int)1e5+7)
#define ll long long int
#define QAQ (0)

using namespace std;

#define num(x) (x-'0')

#define show(x...)                             \
    do {                                       \
        cout << "\033[31;1m " << #x << " -> "; \
        err(x);                                \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

struct Node {
	int x, step;
} ;

class Solution {
public:

	int bfs(int n, int m) {
		queue<Node> q;
		bool vis[2048] = { 0 };
		q.push({n, 0});
		int MX = 2048;
		while(!q.empty()) {
			auto& no = q.front();
			vis[no.x] = true;
			if(no.x == m) 
				return no.step;
			if(no.x+1 >= 0 && no.x+1 < MX && !vis[no.x+1])
			   	q.push({no.x+1, no.step+1}), vis[no.x+1] = true;
			if(no.x-1 >= 0 && no.x-1 < MX && !vis[no.x-1])
			   	q.push({no.x-1, no.step+1}), vis[no.x-1] = true;
#define tmp (no.x*no.x)
			if(tmp>=0 && tmp<MX && !vis[tmp]) 
				q.push({tmp, no.step+1}), vis[tmp] = true;
			q.pop();
		}
		return -1;
	}

    int solve(int n, int m) {
		return bfs(n, m);
   	}
};

#ifdef debug
signed main() {
	freopen("test", "r", stdin);
	clock_t stime = clock();

	Solution s;
	cout << s.solve(3, 10) << endl;






	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
	return 0;
}
#endif




java代码

package newcode.魔法数字;

import java.io.*;
import java.util.*;
import java.math.*;

public class Solution {

	private static final boolean debug = false; //条件编译
	public static long startTIme, endTime;
	public final static int MAXN = (int) 1e5 + 7;
	public final static int INF = 0x7f7f7f7f;

//	public static int n, m, Q, K;

	class Node {
		int x, step;
		public Node(int x, int step) {
			this.x = x;
			this.step = step;
		}
	}
	
	public int solve (int n, int m) {
		// bfs即可
		Queue<Node> q = new LinkedList<Node>();
		q.add(new Node(n, 0));
		final int MX = 2048;
		boolean vis[] = new boolean[MX];
		while(!q.isEmpty()) {
			Node no = q.poll();
			if(no.x == m) return no.step;
			if(no.x+1 >= 0 && no.x+1 < MX && !vis[no.x+1]) {
				q.add(new Node(no.x+1, no.step+1));
				vis[no.x+1] = true;
			}
			if(no.x-1 >= 0 && no.x-1 < MX && !vis[no.x-1]) {
				q.add(new Node(no.x-1, no.step+1));
				vis[no.x-1] = true;
			}
			if(no.x*no.x < MX && !vis[no.x*no.x]) {
				q.add(new Node(no.x*no.x, no.step+1));
				vis[no.x*no.x] = true;
			}
		}
		return -1;
	}
	
	public static void main(String[] args) throws Exception {
		if (debug) {
			FileInputStream fis = new FileInputStream("/home/majiao/桌面/test");
			System.setOut(new PrintStream("/home/majiao/桌面/out"));
			System.setIn(fis);
			startTIme = System.currentTimeMillis();
		}
		Solution s = new Solution();
		System.out.println(s.solve(3, 10));

		if (debug) {
			showRunTime();
		}
	}

	static final void printf(String str, Object... obj) {
		System.out.printf(str, obj);
	}

	static final void show(Object... obj) {
		for (int i = 0; i < obj.length; i++)
			System.out.println(obj[i]);
	}

	static final void showRunTime() {
		endTime = System.currentTimeMillis();
		System.out.println(
				"start time:" + startTIme + "; end time:" + endTime + "; Run Time:" + (endTime - startTIme) + "(ms)");
	}

}

class Read { //自定义快读 Read

	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public Read(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public String nextLine() {
		String str = null;
		try {
			str = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public Double nextDouble() {
		return Double.parseDouble(next());
	}

	public BigInteger nextBigInteger() {
		return new BigInteger(next());
	}
}

你可能感兴趣的:(简单bfs)