Loading...

搜索小练#6

题目

// Prime Path 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 10000+10;
const int INF = 21474836;

int t, res, cnt;

int dp[N], v[N], prime[N];

inline int work (int num, int t, int c) {
	if (t == 0) return num/10*10+c;
	if (t == 1) return num/100*100+c*10+num%10;
	if (t == 2) return num/1000*1000+c*100+num%100;
	return c*1000+num%1000;
}

int main () {
	memset (v, 1, sizeof v);
	for (int i = 2; i <= N; ++ i) {
		if (v[i]) prime[++cnt] = i;
			for (int j = 1; j<=cnt && i*prime[j]<=N; ++ j) 
				v[i*prime[j]] = 0;
	}
	cin >> t;
	while (t --> 0) {
		int a, b;
		cin >> a >> b;
		if (a == b) {puts("0");continue;}
		memset (dp, INF, sizeof dp);
		dp[a] = 0;
		queue<int>q;
		q.push(a);
		while (!q.empty()) {
			int cur = q.front(); q.pop();
			for (int i = 0; i < 4; ++ i)
				for (int j = 0; j < 10; ++ j) {
					if (i == 3 && j == 0) continue;
					int nxt = work (cur, i, j);
					if (!v[nxt] || dp[nxt] <= dp[cur]) continue;
					dp[nxt] = dp[cur]+1;
					q.push(nxt);
				}
		}
		cout << dp[b] << '\n';
	}
}

PS:

1.easy \(bfs\)

2.but is not very easy to find a thought

3.I felt that I have thought vaguely but I can'nt make it.

I will still work hard!

posted @ 2020-07-23 14:09  Youngore  阅读(52)  评论(0)    收藏  举报