POJ——3126Prime Path(双向BFS+素数筛打表)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16272 | Accepted: 9195 |
Description
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
Input
Output
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
单向BFS水题,但是双向让我调试了很久,因为写单向的时候是分4种情况,然后想着双向用for来放在一个循环里好了,结果样例输出答案完全不对,只有答案为1或2的时候可能会对,不解一个早上= =刚才想着算了把for去掉写麻烦点,结果又因为忘记删掉调试输出的语句,WA几发。现在还是不知道为什么原来的for是错的。双向BFS给我一个感觉:代码真长(虽然大部分是重复的)嗯这题写完之后上学期遗留的题除了一道题意本身不清楚+大部分AC代码本身也有明显错误的那道题之外全部A掉了。看看专题训练status里我刷了好几页的历史。真是个悲伤的故事……双向的时候要按层搜索
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=10010;
int vis[N],prime[N];
int color[N];
struct info
{
char s[6];
int step;
};
info S,T;
int change(char s[])
{
int r=0;
for (int i=0; i<4; i++)
r=r*10+s[i]-'0';
return r;
}
queue<info>Qf,Qb;
int T_bfs()
{
S.step=0;
T.step=0;
int lay=0;
while (!Qf.empty())
Qf.pop();
while (!Qb.empty())
Qb.pop();
Qf.push(S);
Qb.push(T);
color[change(S.s)]=1;
color[change(T.s)]=2;
while ((!Qf.empty())||(!Qb.empty()))
{
while(!Qf.empty()&&Qf.front().step==lay)
{
info now=Qf.front();
Qf.pop();
info v=now;
while (--v.s[0]>='1')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (++v.s[0]<='9')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (--v.s[1]>='0')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
}
v=now;
while (++v.s[1]<='9')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (--v.s[2]>='0')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
}
v=now;
while (++v.s[2]<='9')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (--v.s[3]>='0')
{
int num=change(v.s);
if(num%2==0)
continue;
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
}
v=now;
while (++v.s[3]<='9')
{
int num=change(v.s);
if(num%2==0)
continue;
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=1;
vis[num]=v.step;
Qf.push(v);
}
else if(color[num]==2)
{
return vis[num]+vis[change(now.s)];
}
}
}
}
//
while(!Qb.empty()&&Qb.front().step==lay)
{
info now=Qb.front();
Qb.pop();
info v=now;
while (--v.s[0]>='1')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
else if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (++v.s[0]<='9')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
else if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (--v.s[1]>='0')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
else if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (++v.s[1]<='9')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
else if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
}
//// v=now;
while (--v.s[2]>='0')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (++v.s[2]<='9')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
else if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
} // v=now;
while (--v.s[3]>='0')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
else if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
} v=now;
while (++v.s[3]<='9')
{
int num=change(v.s);
if(prime[num])
{
if(!color[num])
{
v.step=now.step+1;
color[num]=2;
vis[num]=v.step;
Qb.push(v);
}
else if(color[num]==1)
{
return vis[num]+vis[change(now.s)];
}
}
}
}
lay++;
}
}
int main(void)
{
int tcase,i,j;
for (i=0; i<N; i++)
prime[i]=1;
for (i=2; i<N; ++i)
for (j=2; j*i<N; ++j)
prime[i*j]=0;
scanf("%d",&tcase);
while (tcase--)
{
MM(vis);
MM(color);
scanf("%s%s",S.s,T.s);
if(strcmp(S.s,T.s)==0)
puts("0");
else
printf("%d\n",T_bfs()+1);
}
return 0;
}
POJ——3126Prime Path(双向BFS+素数筛打表)的更多相关文章
- poj3126Prime Path (BFS+素数筛)
素数筛:需要一个数组进行标记 最小的素数2,所有是2的倍数的数都是合数,对合数进行标记,然后找大于2的第一个非标记的数(肯定是素数),将其倍数进行标记,如此反复,若是找n以内的所有素数,只需要对[2, ...
- poj 3048 Max Factor(素数筛)
这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...
- [poj] 2549 Sumsets || 双向bfs
原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...
- HDU - 4548-美素数 (欧拉素数筛+打表)
小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为"美素数&quo ...
- POJ 3126 Prime Path (BFS + 素数筛)
链接 : Here! 思路 : 素数表 + BFS, 对于每个数字来说, 有四个替换位置, 每个替换位置有10种方案(对于最高位只有9种), 因此直接用 BFS 搜索目标状态即可. 搜索的空间也不大. ...
- poj 1077 Eight(双向bfs)
题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...
- POJ_3126 Prime Path 【BFS+素数打表】
一.题目 http://poj.org/problem?id=3126 二.分析 该题主要是要让我们找到一个$4$位素数到另一个$4$位素数的最少的变换次数,且要求保证每一次变换都满足 1.下一个数必 ...
- Uva 1599 Ideal Path - 双向BFS
题目连接和描述以后再补 这题思路很简单但还真没少折腾,前后修改提交了七八次才AC...(也说明自己有多菜了).. 注意问题: 1.看清楚原题的输入输出要求,刚了书上的中文题目直接开撸,以为输入输出都是 ...
- Codeforces_776B: Sherlock and his girlfriend(素数筛)
题目链接 题意:对2~n+1染色,一个数不能与其素因子同色. 故而只需两种颜色即可,素数染1,合数染2便可满足条件 #include<bits/stdc++.h> using namesp ...
随机推荐
- Fragment 创建及替换
1.Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应 ...
- [翻译] API测试的最佳实践 - 介绍
API测试的最佳实践 - 介绍 在上一篇“是什么让API测试很叼”一文中,我们讨论API与其他形式的软件测试的差异.部分是因为API之间的通信压根就没考虑让你能读懂,纯粹是为了方便计算机之间的交互而设 ...
- 再用python写一个文本处理的东东
朋友遇到一点麻烦,我自告奋勇帮忙.事情是这样的: - 他们的业务系统中,数据来自一个邮箱: - 每一个邮件包含一条记录: - 这些记录是纯文本的,字段之间由一些特殊字符分隔: - 他们需要从邮箱中批量 ...
- 洛谷 P1363 幻想迷宫
题目描述 背景 Background (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊…… LHX:momo...我们一定能走 ...
- COGS 1144. [尼伯龙根之歌] 精灵魔法
★ 输入文件:alfheim.in 输出文件:alfheim.out 简单对比时间限制:1 s 内存限制:128 MB [题目背景] 『谜题在丛林中散发芳香绿叶上露珠跳跃着歌唱火焰在隐 ...
- haml scss转换编写html css的前期工作
http://www.w3cplus.com/sassguide/install.html 先下载ruby $ gem sources $ gem sources --remove https://r ...
- 栈的应用——Rails
一.题目描述 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站顺序编号为1~n,经中转站C驶向B.中转站C,这是一个可以停放任意多节车厢的车站,但由于末端封顶,驶入C的车厢必须以相反的顺序驶出C ...
- java面试基础篇(三)
1.Q:ArrayList 和 LinkedList 有什么区别? A:ArrayList查询快!LinkedList增删快.ArrayList是基于索引的数据接口,它的底层是数组.空间占用相对小一些 ...
- dhtmlTree简单实例以及基本参数设置
demo实例参考: <link rel="STYLESHEET" type="text/css" href="css/dhtmlXTree.c ...
- NLP.TM | GloVe模型及其Python实现
在进行自然语言处理中,需要对文章的中的语义进行分析,于是迫切需要一些模型去描述词汇的含义,很多人可能都知道word2vector算法,诚然,word2vector是一个非常优秀的算法,并且被广泛运用, ...