题目传送门

 /*
题意:从一个数到另外一个数,每次改变一个数字,且每次是素数
BFS:先预处理1000到9999的素数,简单BFS一下。我没输出Impossible都AC,数据有点弱
*/
/************************************************
Author :Running_Time
Created Time :2015-8-2 15:46:57
File Name :POJ_3126.cpp
*************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
bool is_prime[MAXN];
bool vis[MAXN];
int x, y, res;
struct Digit {
int d[];
int step;
}; void solve(void) {
memset (is_prime, true, sizeof (is_prime));
for (int i=; i<=; ++i) {
for (int j=; j<i; ++j) {
if (i % j == ) {
is_prime[i] = false; break;
}
}
}
} int get_num(int *a) {
int ret = ;
for (int i=; i<=; ++i) {
ret = ret * + a[i];
}
return ret;
} void BFS(int u, int v) {
Digit tmp;
for (int i=; i>=; --i) {
tmp.d[i] = u % ; u /= ;
}
tmp.step = ;
queue<Digit> Q; Q.push (tmp); vis[u] = true;
int ans = -;
while (!Q.empty ()) {
Digit x = Q.front (); Q.pop ();
int m = get_num (x.d);
if (m == v) {
ans = x.step; break;
}
for (int i=; i<=; ++i) {
for (int j=; j<=; ++j) {
if (i == && j == ) continue;
if (x.d[i] != j) {
Digit y = x;
y.d[i] = j; m = get_num (y.d);
if (is_prime[m] && !vis[m]) {
vis[m] = true; y.step++; Q.push (y);
}
}
}
}
}
if (ans == -) puts ("Impossible");
else printf ("%d\n", ans);
} int main(void) { //POJ 3126 Prime Path
solve ();
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &x, &y);
memset (vis, false, sizeof (vis));
BFS (x, y);
} return ;
}

最新文章

  1. Android Fragment使用(一) 基础篇 温故知新
  2. 支持coclock模式
  3. UML大战需求分析阅读笔记1
  4. Raspberry Pi B+ 定时向物联网yeelink上传CPU GPU温度
  5. js中对arry数组的各种操作小结
  6. C#动态二维数组输出
  7. chown命令详解
  8. html弹窗,与弹出对话框
  9. Android PopupWindow菜单
  10. arcgis api for js入门开发系列九热力图效果
  11. HighCharts之2D带有Legend的饼图
  12. shell脚本登录远程服务器并下载至本地
  13. Windows 10修复
  14. SpringBoot-@value自定义参数
  15. php里获取第一个中文首字母并排序
  16. 数据库表名最大长度(Oracle=30;SqlServer=128;)
  17. PCL特征点与配准(1)
  18. .NET:CLR via C# Exceptions and State Management
  19. TypeError: &#39;newline&#39; is an invalid keyword argument for this function 错误解决
  20. P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

热门文章

  1. codevs1154 能量项链
  2. readdir() 获取文件类型
  3. PHP 关键词
  4. 【进击后端】Ubuntu 命令行 安装nginx
  5. MySQL主从复制搭建教程收集(待实践)
  6. JSP中自动刷新
  7. MongoDB小结19 - find【查询条件$all】
  8. [Jexus系列] 一、安装并运行 Jexus
  9. clamav完整查杀linux病毒实战(摘抄)
  10. oralce之复杂查询举例