#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<math.h>
#define N 10010 using namespace std; int vis[N];
char s1[], s2[], s3[]; struct node
{
int step;
char st[];
}; int prime(int y)
{
int i, k = (int)sqrt(y);
for(i = ; i <= k ; i++)
if(y % i == )
return ;
return ;
} int BFS(char s[])
{
queue<node>Q;
node now, next;
int i, j, h, x = ;
memset(vis, , sizeof(vis));
memset(s3, , sizeof(s3));
for(i = ; i < ; i++)
x = x * + (s[i] - '');
vis[x] = ;
strcpy(now.st, s);
now.step = ;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(strcmp(now.st, s2) == )
return now.step;
for(i = ; i < ; i++)//各个位(即个位,十位,百位,千位)
{
for(j = ; j < ; j++)//0到9十个数
{
if(i == && j == )//千位不为0
continue;
if(now.st[i] == j + '')//原有的数不需要替换
continue;
strcpy(s3, now.st);
now.st[i] = j + '';//0到9其中的一个数去替换四位数气中的一位
x = ;
for(h = ; h < ; h++)
x = x * + (now.st[h] - '');
if(prime(x) == && !vis[x])
{
vis[x] = ;
next.step = now.step + ;
strcpy(next.st, now.st);
i = ;/***/
j = ;/***/
Q.push(next);
}
strcpy(now.st, s3);
}
}
}
return -;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%s%s", s1, s2);
printf("%d\n", BFS(s1));
}
return ;
}

HDU1973 http://acm.hdu.edu.cn/showproblem.php?pid=1973的更多相关文章

  1. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  2. KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...

  3. HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意: 一个字符串,有多少个subsequence是回文串. 别人的题解: 用dp[i][j]表示这一段里 ...

  4. 待补 http://acm.hdu.edu.cn/showproblem.php?pid=6602

    http://acm.hdu.edu.cn/showproblem.php?pid=6602 终于能够看懂的题解: https://blog.csdn.net/qq_40871466/article/ ...

  5. HDU-1257 导弹拦截系统 http://acm.hdu.edu.cn/showproblem.php?pid=1257

    Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...

  6. http://acm.hdu.edu.cn/showproblem.php?pid=2579

    #include<stdio.h> #include<string.h> #include<queue> #define N 110 int m, n, k, x1 ...

  7. KMP应用http://acm.hdu.edu.cn/showproblem.php?pid=2594

    riemann与marjorie拼接后riemannmarjorie前缀与后缀公共部分为 rie 长度为 3(即next[l] = next[14]的值,l为拼接后的长度)但:aaaa与aa拼接后aa ...

  8. HDU 2544 最短路 http://acm.hdu.edu.cn/showproblem.php?pid=2544

    //代码: //方法1:Dijkstra's Algorithm #include<stdio.h> #include<math.h> #include<string.h ...

  9. HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...

随机推荐

  1. jQuery--隐藏事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. WebBrowser自动点击链接 广告自动点击 Ads Auto Click

    点击付费广告, 是目前比较流行的一种网络营销模式, 但是,如果你单纯的靠手工一个一个去点, 总觉得不划算  , 能不能实现自动的去点击呢? 答案是肯定的 .NET 里面的 WebBrowser, 可以 ...

  3. Web内容管理系统 Magnolia 安装使用-挖掘优良的架构(2)

    在Windows上安装社区版  tomcat集成版 Magnolia CMS社区版本为免费发行,不需要任何GNU通用公共许可协议(第3版)条款下的授权(这个许可协议允许您在特定条款和条件下,重新分配和 ...

  4. bzoj2797

    对和排序,显然最小是a1+a2,次小a1+a3 然后穷举哪里是a2+a3 这样a1,a2,a3就求出来了 注意a2+a3只可能是前n+1项中的一个,所以穷举这步是O(n)的 接下来我们把已经确定的数的 ...

  5. TIOBE 2015年5月编程语言排行榜 Visual Studio系列在上升

    TIOBE 编程语言社区排行榜是编程语言流行趋势的一个指标,每月更新,这份排行榜排名基于互联网上有经验的程序员. 课程和第三方厂商的数量.排名使用著名的搜索引擎(诸如 Google.MSN.Yahoo ...

  6. android开发中eclipse里xml开发的自动提示和使用帮助快捷键提示

    Eclipse Android 代码自动提示功能 Eclipse for android 设置代码提示功能 打 开 Eclipse 依次选择 Window > Preferences > ...

  7. UVA 11865 Stream My Contest(最小树形图)

    题意:N台机器,M条有向边,总资金C,现要到搭建一个以0号机(服务器)为跟的网路,已知每条网线可以把数据从u传递到v,其带宽为d,花费为c,且d越大,传输速度越快,问能够搭建的传输速度最快的网络d值是 ...

  8. kendo grid输入框验证方法

    $("#grid").kendoGrid({ dataSource: dataSrc, //toolbar: ["save", "取消"], ...

  9. UVa 11300 Spreading the Wealth 分金币

    圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...

  10. db file scattered read 等待事件

    db file scattered read 等待事件: 我们经常会见到db file scattered read  等待事件,在生产环境中,这个等待事件可能更为常见.这个事件表明用户进程正在读数据 ...