#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. textbox不支持Ctrl+A

    http://stackoverflow.com/questions/5885739/why-are-some-textboxes-not-accepting-control-a-shortcut-t ...

  2. toad for sqlserver5.7

    toad for sqlserver5.7 虽然SSMS很好很强大,不过有时候使用一些第三方工具可以使MSSQL DBA们更加的方便管理MSSQL toad for sqlserver5.7就是这样一 ...

  3. 1741. Communication Fiend(dp)

    刷个简单的DP缓缓心情 1A #include <iostream> #include<cstdio> #include<cstring> #include< ...

  4. RAPIDXML 中文手册,根据官方文档完整翻译!

    简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...

  5. Java实现RC4加解密

    package com.vrv.paw.utils; public class RC4Util { public static String decry_RC4(byte[] data, String ...

  6. Self-Paced Training (3) - Docker Operations

    AgendaTroubleshooting ContainersOverview of Security PracticesPrivate RegistryIntro to Docker Machin ...

  7. Java Web编程的主要组件技术——Struts的高级功能

    参考书籍:<J2EE开源编程精要15讲> Struts对国际化的支持 "国际化"(I18N)指一个应用程序在运行时能根据客户端请求所来的国家/地区.语言的不同显示不同的 ...

  8. 【C#学习笔记】读access2007

    using System; using System.Data.OleDb; namespace ConsoleApplication { class Program { static void Ma ...

  9. H.264学习笔记之一(层次结构,NAL,SPS)

    一 H.264句法 1.1元素分层结构 H.264编码器输出的Bit流中,每个Bit都隶属于某个句法元素.句法元素被组织成有层次的结构,分别描述各个层次的信息. 图1 H.264分层结构由五层组成,分 ...

  10. Oracle“死锁”模拟

    本着实验优先的原则,先模拟死锁的发生,然后在列一下死锁产生的四个必要条件和处理死锁的一般策略. 1.创建两个简单的表t1_deadlock和t2_deadlock,每个表中仅仅包含一个字段asys@o ...