日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uestc 851 方老师与素数

發(fā)布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uestc 851 方老师与素数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

看了大牛的代碼

新技能get√

如何優(yōu)雅的計算廣搜的次數(shù)

1 #include<cstdio> 2 #include<cstring> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 #include<queue> 7 #include<string> 8 #include<cmath> 9 using namespace std; 10 #define MAXN 10001 11 12 bool prime[MAXN]; 13 int vis[MAXN]; 14 int a, b, t; 15 16 void deal() 17 { 18 for (int i = 2; i <= MAXN; ++i) 19 if (!prime[i]) 20 for (int j = i*i; j <= MAXN; j += i) 21 prime[j] = true; 22 /*for (int i = 0; i < 50; ++i) 23 if (!prime[i]) cout << i << " "; 24 cout << endl;*/ 25 } 26 27 int bfs() 28 { 29 queue<int> q; 30 q.push(a); 31 q.push(-1); 32 int ans = 0; 33 while (!q.empty()) { 34 int t = q.front(); 35 if (t == -1) { 36 q.pop(); 37 if (q.empty()) return -1; 38 ans++; 39 q.push(t); continue; 40 }q.pop(); 41 if (t == b) return ans; 42 int s; 43 for (int i = 1; i <= 9; ++i) { 44 s = i * 1000 + t % 1000; 45 if (!vis[s] && !prime[s]) { 46 q.push(s); 47 vis[s] = true; 48 } 49 } 50 for (int i = 0; i <= 9; ++i) { 51 s = i * 100 + t % 100 + t/1000*1000; 52 if (!vis[s] && !prime[s]) { 53 q.push(s); 54 vis[s] = true; 55 } 56 } 57 for (int i = 0; i <= 9; ++i) { 58 s = i * 10 + t % 10 + t/100*100; 59 if (!vis[s] && !prime[s]) { 60 q.push(s); 61 vis[s] = true; 62 } 63 } 64 for (int i = 0; i <= 9; ++i) { 65 s = i + t / 10 * 10; 66 if (!vis[s] && !prime[s]) { 67 q.push(s); 68 vis[s] = true; 69 } 70 } 71 } 72 return -1; 73 } 74 75 int main() 76 { 77 int t; 78 cin >> t; 79 deal(); 80 while (t--) { 81 cin >> a >> b; 82 memset(vis,0,sizeof(vis)); 83 int res = bfs(); 84 if (res == -1) puts("Impossible"); 85 else cout << res << endl; 86 } 87 return 0; 88 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/usedrosee/p/4260173.html

總結

以上是生活随笔為你收集整理的uestc 851 方老师与素数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。