#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. C++中变量自动初始化的问题

    C++中有一些变量在如果没有赋初值会被编译器自动赋值为0,但有的变量又不会这样,而得到一个随机数,下面具体讨论一下: 首先看一下C++中的几个存储区:1.栈区:由编译器自动分配释放 ,存放函数的参数值 ...

  2. chrome控制台小技巧

    对于大多数开发人员来说,chrome控制台最常用的命令就是 console.log()了,然后还有一些其他类似的命令,如: console.info()   提示信息 console.error() ...

  3. 基于XMPP的即时通信系统的建立(三)— 程序设计概览

    XMPP与HTTP的比较 XMPP的优势 Ÿ   1. 推送数据 HTTP只能从服务器哪里请求数据,除非服务器正在响应客户端请求,否则不能向客户端发送数据.但XMPP连接是双向的,任何一方在任何时候都 ...

  4. UVa 10003 (可用四边形不等式优化) Cutting Sticks

    题意: 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用. 分析: d(i, j)表示切割第i个切点到第j个切点这段所需的最小费用.则有d(i, j) = ...

  5. attachEvent,addEventListener事件绑定

    兼容各主流浏览器的事件绑定(在同一个事件上添加多个处理函数). 1.绑定方法: //IE attachEvent(事件名, 函数) oBtn.attachEvent('onclick', aaa); ...

  6. 移动APP服务端API设计应该考虑到的问题

    2014年,移动APP的热度丝毫没有减退,并没有像桌面软件被WEB网站那样所取代, 不但如此,越来越多的传统应用.网站也都开始制作自己的移动APP,也就是我们常说的IOS客户端.android客户端. ...

  7. mysql 分页存储过程 一次返回两个记录集(行的条数,以及行记录),DataReader的Read方法和NextResult方法

    DELIMITER $$ USE `netschool`$$ DROP PROCEDURE IF EXISTS `fn_jk_GetCourses`$$ CREATE DEFINER=`root`@` ...

  8. WebView线性进度条

    参考:http://www.cnblogs.com/hubli/p/4835549.html APP会跳转网页时候,请参考:http://blog.csdn.net/raphael55/article ...

  9. HDU 5305 Friends (DFS,穷举+剪枝)

    题意: 给定n个人,m对朋友关系,如果对于每个人,只能刚好选择其所有朋友中的一半的人进行聊天(只是我和我的朋友,不是我的朋友和我的朋友),那么有多少种情况?只要一个选择不同,视为不同情况. 思路: 比 ...

  10. 如何借助于UML完成我们对系统的设计?谈谈我的理解

    首先要说的是我对面向对象的理解,以及设计类的依据: http://www.cnblogs.com/xinchrome/p/4904931.html 理解了这篇文章,也就理解了现在要说的. 在面向对象编 ...