Skip to content

Commit 79fc248

Browse files
committed
3126
1 parent a09817d commit 79fc248

File tree

5 files changed

+171
-3
lines changed

5 files changed

+171
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
| 2.4. 搜索 | - |
6666
| :----: |:----:|
6767
| 深度优先搜索DFS | [POJ-2488](/reports/POJ2488-A%20Knight's%20Journey) [POJ-3083](/reports/POJ3083-Children%20of%20the%20Candy%20Corn) [POJ-3009](/reports/POJ3009%20–%20Curling%202.0) [POJ-1321](/reports/POJ1321-Chess%20Problem) |
68-
| 广度优先搜索BFS | [POJ-3278](/reports/POJ3278-Catch%20That%20Cow) [POJ-1426](http://exp-blog.com/2018/06/22/pid-813/) [POJ-3126](http://exp-blog.com/2018/06/23/pid-818/) [POJ-3414](http://exp-blog.com/2018/06/23/pid-821/) [POJ-2251](http://exp-blog.com/2018/06/23/pid-824/) |
68+
| 广度优先搜索BFS | [POJ-3278](/reports/POJ3278-Catch%20That%20Cow) [POJ-1426](/reports/POJ1426-Find%20The%20Multiple) [POJ-3126](http://exp-blog.com/2018/06/23/pid-818/) [POJ-3414](http://exp-blog.com/2018/06/23/pid-821/) [POJ-2251](http://exp-blog.com/2018/06/23/pid-824/) |
6969
| 简单搜索技巧和剪枝 | [POJ-1010](http://exp-blog.com/2018/06/23/pid-830/) [POJ-2362](http://exp-blog.com/2018/06/23/pid-839/) [POJ-1011](http://exp-blog.com/2018/06/23/pid-842/) [POJ-1416](http://exp-blog.com/2018/06/23/pid-846/) [POJ-2676](http://exp-blog.com/2018/06/23/pid-849/) [POJ-1129](http://exp-blog.com/2018/06/23/pid-860/) |
7070

7171

reports/POJ1426-Find The Multiple/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## [[POJ](http://poj.org/)] [[INDEX](https://github.com/lyy289065406/POJ-Solving-Reports)] [1426] [[Find The Multiple](http://poj.org/problem?id=1426)]
22

3-
> [Time: 1000MS] [Memory: 10000K] [难度: 初级] [分类: ]
3+
> [Time: 1000MS] [Memory: 10000K] [难度: 初级] [分类: BFS]
44
55
------
66

reports/POJ3126-Prime Path/README.md

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
## [[POJ](http://poj.org/)] [[INDEX](https://github.com/lyy289065406/POJ-Solving-Reports)] [3126] [[Prime Path](http://poj.org/problem?id=3126)]
2+
3+
> [Time: 1000MS] [Memory: 65536K] [难度: 初级] [分类: BFS]
4+
5+
------
6+
7+
## 问题描述
8+
9+
给定两个四位素数a b,要求把a变换到b
10+
11+
变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能重复。
12+
13+
求从a到b最少需要的变换次数。无法变换则输出Impossible
14+
15+
16+
## 解题思路
17+
18+
超级水题,40入口的**BFS + 素数判定**
19+
20+
不过剪枝之后就没有40入口了,入口数远小于40
21+
22+
无论是判定素数还是搜索素数,首先排除偶数,这样就剪掉一半枝叶了
23+
24+
判断素数用根号法判断,
25+
26+
如果一个数X不能被 `[2,√X]` 内的所有素数整除,那么它就是素数
27+
28+
可以判断的复杂度降到 logn
29+
30+
**注意**
31+
32+
- 千位的变换要保证千位不为0
33+
- 其实**素数也是用来辅助搜索剪枝**
34+
35+
36+
## AC 源码
37+
38+
39+
```c
40+
//Memory Time
41+
//212K 16MS
42+
43+
#include<iostream>
44+
using namespace std;
45+
46+
typedef class
47+
{
48+
public:
49+
int prime;
50+
int step;
51+
}number;
52+
53+
bool JudgePrime(int digit)
54+
{
55+
if(digit==2 || digit==3)
56+
return true;
57+
else if(digit<=1 || digit%2==0)
58+
return false;
59+
else if(digit>3)
60+
{
61+
for(int i=3;i*i<=digit;i+=2)
62+
if(digit%i==0)
63+
return false;
64+
return true;
65+
}
66+
}
67+
68+
int a,b;
69+
bool vist[15000];
70+
number queue[15000];
71+
72+
void BFS(void)
73+
{
74+
int i; //temporary
75+
int head,tail;
76+
queue[head=tail=0].prime=a;
77+
queue[tail++].step=0;
78+
vist[a]=true;
79+
80+
while(head<tail)
81+
{
82+
number x=queue[head++];
83+
if(x.prime==b)
84+
{
85+
cout<<x.step<<endl;
86+
return;
87+
}
88+
89+
int unit=x.prime%10; //获取x的个位
90+
int deca=(x.prime/10)%10; //获取x的十位
91+
92+
for(i=1;i<=9;i+=2) //枚举x的个位,保证四位数为奇数(偶数必不是素数)
93+
{
94+
int y=(x.prime/10)*10+i;
95+
if(y!=x.prime && !vist[y] && JudgePrime(y))
96+
{
97+
vist[y]=true;
98+
queue[tail].prime=y;
99+
queue[tail++].step=x.step+1;
100+
}
101+
}
102+
for(i=0;i<=9;i++) //枚举x的十位
103+
{
104+
int y=(x.prime/100)*100+i*10+unit;
105+
if(y!=x.prime && !vist[y] && JudgePrime(y))
106+
{
107+
vist[y]=true;
108+
queue[tail].prime=y;
109+
queue[tail++].step=x.step+1;
110+
}
111+
}
112+
for(i=0;i<=9;i++) //枚举x的百位
113+
{
114+
int y=(x.prime/1000)*1000+i*100+deca*10+unit;
115+
if(y!=x.prime && !vist[y] && JudgePrime(y))
116+
{
117+
vist[y]=true;
118+
queue[tail].prime=y;
119+
queue[tail++].step=x.step+1;
120+
}
121+
}
122+
for(i=1;i<=9;i++) //枚举x的千位,保证四位数,千位最少为1
123+
{
124+
int y=x.prime%1000+i*1000;
125+
if(y!=x.prime && !vist[y] && JudgePrime(y))
126+
{
127+
vist[y]=true;
128+
queue[tail].prime=y;
129+
queue[tail++].step=x.step+1;
130+
}
131+
}
132+
133+
}
134+
135+
cout<<"Impossible"<<endl;
136+
return;
137+
}
138+
139+
int main(void)
140+
{
141+
int test;
142+
cin>>test;
143+
while(test--)
144+
{
145+
cin>>a>>b;
146+
memset(vist,false,sizeof(vist));
147+
BFS();
148+
}
149+
return 0;
150+
}
151+
```
152+
153+
------
154+
155+
## 版权声明
156+
157+
 [![Copyright (C) EXP,2016](https://img.shields.io/badge/Copyright%20(C)-EXP%202016-blue.svg)](http://exp-blog.com) [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
158+
159+
160+
- Site: [http://exp-blog.com](http://exp-blog.com)
161+
- Mail: <a href="mailto:[email protected]?subject=[EXP's Github]%20Your%20Question%20(请写下您的疑问)&amp;body=What%20can%20I%20help%20you?%20(需要我提供什么帮助吗?)">[email protected]</a>
162+
163+
164+
------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (C)
2+
# Author: EXP
3+
# Site : http://exp-blog.com
4+

reports/POJ3278-Catch That Cow/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## [[POJ](http://poj.org/)] [[INDEX](https://github.com/lyy289065406/POJ-Solving-Reports)] [3278] [[Catch That Cow](http://poj.org/problem?id=3278)]
22

3-
> [Time: 2000MS] [Memory: 65536K] [难度: 初级] [分类: ]
3+
> [Time: 2000MS] [Memory: 65536K] [难度: 初级] [分类: BFS]
44
55
------
66

0 commit comments

Comments
 (0)