hdu 1973 bfs+素数判断

题意:给出两个四位数,现要改变第一个数中的个,十,百,千位当中的一个数
使它最终变成第二个数,要求这过程中形成的数是素数,问最少的步骤
题解:素数筛选+bfs
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0

注意第一位不能变成0即可

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 #define MOD 1000000007
10 const int INF=0x3f3f3f3f;
11 const double eps=1e-5;
12 #define cl(a) memset(a,0,sizeof(a))
13 #define ts printf("*****\n");
14 const int MAXN=1005;
15 int n,m,tt,time;
16 struct node
17 {
18     char s[5];
19     int t;
20 }st,ed;
21
22 bool prime[10005];
23 bool vis[10005];
24 void isprime() {//素数筛选
25     int i,j;
26     for(i=2; i<10005; i++)prime[i]=1;
27     prime[0]=0,prime[1]=0;
28
29
30     for(i=2; i<10005; i++) {
31         if(prime[i]) {
32             for(j=2*i; j<10005; j+=i) {
33                 prime[j]=0;
34             }
35         }
36     }
37 }
38 void bfs()
39 {
40     node now,next;
41     queue<node> q;
42     q.push(st);
43     int x=0;
44     for(int i=0;i<4;i++)    x=x*10+(st.s[i]-‘0‘);
45     vis[x]=1;
46     while(!q.empty())
47     {
48         now=q.front();
49         q.pop();
50         if(strcmp(ed.s,now.s)==0)
51         {
52             printf("%d\n",now.t);
53         }
54         for(int i=0;i<4;i++)    //4位
55         {
56             strcpy(next.s,now.s);
57             next.t=now.t+1;
58             for(int j=0;j<=9;j++)   //尝试在每位填数字
59             {
60                 if(i==0&&j==0)  continue;
61                 if(next.s[i]-‘0‘==j)    continue;   //原来就有的就不用填了
62                 next.s[i]=j+‘0‘;
63                 x=0;
64                 for(int w=0;w<4;w++)    x=x*10+(next.s[w]-‘0‘);
65                 if(prime[x]&&!vis[x])
66                 {
67                     vis[x]=1;
68                     q.push(next);
69                 }
70             }
71         }
72     }
73
74 }
75 int main()
76 {
77     int i,j,k;
78     #ifndef ONLINE_JUDGE
79     freopen("1.in","r",stdin);
80     #endif
81     isprime();
82     scanf("%d",&tt);
83     while(tt--)
84     {
85         scanf("%s%s",st.s,ed.s);
86         st.t=0;
87         cl(vis);
88         bfs();
89     }
90 }
时间: 2025-01-16 10:41:33

hdu 1973 bfs+素数判断的相关文章

hdu 1175 bfs 转弯题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 和之前的1728类似.就是判断转弯数,建立一个用于记录转弯数的数组.. 还有就是对于特殊情况要进行考虑,比如起点与终点相同的情况,对于本题来说是不可以消去的应该输出NO.还有就是起点或终点是零这也是不行的,因为0代表没有棋子... 还有在判断能不能走的时候要小心,对于判断条件一定要小心,不要图赶快写.. 错误的地方都写在注释中了.. 代码: // hdu 1175 bfs 转弯数 //1.起点

HDU 1072 bfs

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7083    Accepted Submission(s): 3409 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a tim

hdu 1252 BFS

1 /* 2 题意:给出一个m*m矩阵表示的完全图,且每个点都有自环,每条边都有一种颜色:有三个玩家ABC的三张纸片分别初始在 3 某三个位置(可以重叠),纸片可以沿着边走一步,可以走的路径的规则为:若A要走到某个点i,则A-i的颜色要和B-C的颜 4 色相同:问最少要走多少步.(题意太难懂了,看了别人的说明才弄懂了题意) 5 6 题解:BFS 7 首先初步分析题意似乎很难用图论来解决,那么就是搜索/DP/数据结构,然后从搜索方面去思考的话,就是要找状态,然 8 后初步列出所有信息,三个点得位置

HDU Redraw Beautiful Drawings 判断最大流是否唯一解

点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1660    Accepted Submission(s): 357 Problem Description Alice and Bob are playing together. Alice is crazy about

POJ 1811 大素数判断

数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> using namespace std; __int64 pri[]= {2,3,5,7,11,13,17,19,23,29,31};//用小素数表做随机种子避免第一类卡米

POJ1811 Prime Test(miller素数判断&amp;&amp;pollar_rho大数分解)

http://blog.csdn.net/shiyuankongbu/article/details/9202373 发现自己原来的那份模板是有问题的,而且竟然找不出是哪里的问题,所以就用了上面的链接上的一份代码,下面只是寄存一下这份代码,以后打印出来当模板好了. #pragma warning(disable:4996) #include <iostream> #include <cstring> #include <algorithm> #include <c

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

HDU 2521 反素数

反素数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4238    Accepted Submission(s): 2456 Problem Description 反素数就是满足对于任意i(0<i<x),都有g(i)<g(x),(g(x)是x的因子个数),则x为一个反素数.现在给你一个整数区间[a,b],请你求出该区间的x使

POJ3641 Pseudoprime numbers(快速幂+素数判断)

POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Carmichael Number 的简化版 /* * Created: 2016年03月30日 22时32分15秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring&g