poj3126 Prime Path
来源:互联网 发布:linux命令创建多层目录 编辑:程序博客网 时间:2024/06/03 19:13
package bfs;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**问题请参考http://poj.org/problem?id=3126
* @author rayli
* @date:2014-7-17 上午9:18:47
*
*/
public class PrimePath
{
static int ans;//存最后结果
boolean vist[] = new boolean[10005];//用来标记已遍历的
boolean fine = false;//判断有没有找到结果
boolean judgeprime(int num)//判断素数
{
if(num == 2 || num == 3)
return true;
else if(num <=1)
return false;
else if (num > 3)
for(int i=2; i<=Math.sqrt(num); i++)
{
if(num % i == 0)
{
return false;
}
}
return true;
}
int izero(int num, int w)//把num从左边数第w位置0
{
int tmp[] = new int[5];
int t = num;
//把num数的每一位存放到对应的数组里面
for(int i=4; i>=1; i--)
{
tmp[i] = num % 10;
num /= 10;
}
//把num从左边数第w位置0
t -= (tmp[w] * Math.pow(10, 4-w));
return t;
}
void BFS(int m1, int m2)
{
if(m1 == m2)
{
ans = 0;
fine = true;
return;
}
Queue<Integer> q = new LinkedList<Integer>();
vist[m1] = true;
q.add(m1);
ans = 0;
/**
* 队列不为空且没有找到结果
*/
while(!q.isEmpty() && !fine)
{
int tt = q.size();
ans++;
while(tt-->0)
{
int tmp = q.poll();
if(tmp == m2)
return;
//个、十、百、千位
for(int i=4; i>=1; i--)
{
int t = izero(tmp, i);
//每个位从0到9遍历
for(int j=0; j<=9; j++)
{
int now = (int)(t + (j * Math.pow(10, 4-i)));
if(now == m2)
{
fine = true;
return;
}
else if(judgeprime(now) && !vist[now] && now >=1000)//刚开始没有判定now >=1000,答案一直不对
{
vist[now] = true;//如果是素数并大于1000且没有被放过队列里面
q.add(now);
}
}
}
}
}
}
void output()
{
if(fine)
{
System.out.println(ans);
}
else
{
System.out.println(" Impossible");
}
}
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
while(n-->0)
{
int n1 = cin.nextInt();
int n2 = cin.nextInt();
PrimePath pp = new PrimePath();
pp.BFS(n1, n2);
pp.output();
}
}
}
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/**问题请参考http://poj.org/problem?id=3126
* @author rayli
* @date:2014-7-17 上午9:18:47
*
给定两个四位素数a b,要求把a变换到b
变换的过程要保证 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位不同,而且每步得到的素数都不能重复。
求从a到b最少需要的变换次数。无法变换则输出Impossible
**/
public class PrimePath
{
static int ans;//存最后结果
boolean vist[] = new boolean[10005];//用来标记已遍历的
boolean fine = false;//判断有没有找到结果
boolean judgeprime(int num)//判断素数
{
if(num == 2 || num == 3)
return true;
else if(num <=1)
return false;
else if (num > 3)
for(int i=2; i<=Math.sqrt(num); i++)
{
if(num % i == 0)
{
return false;
}
}
return true;
}
int izero(int num, int w)//把num从左边数第w位置0
{
int tmp[] = new int[5];
int t = num;
//把num数的每一位存放到对应的数组里面
for(int i=4; i>=1; i--)
{
tmp[i] = num % 10;
num /= 10;
}
//把num从左边数第w位置0
t -= (tmp[w] * Math.pow(10, 4-w));
return t;
}
void BFS(int m1, int m2)
{
if(m1 == m2)
{
ans = 0;
fine = true;
return;
}
Queue<Integer> q = new LinkedList<Integer>();
vist[m1] = true;
q.add(m1);
ans = 0;
/**
* 队列不为空且没有找到结果
*/
while(!q.isEmpty() && !fine)
{
int tt = q.size();
ans++;
while(tt-->0)
{
int tmp = q.poll();
if(tmp == m2)
return;
//个、十、百、千位
for(int i=4; i>=1; i--)
{
int t = izero(tmp, i);
//每个位从0到9遍历
for(int j=0; j<=9; j++)
{
int now = (int)(t + (j * Math.pow(10, 4-i)));
if(now == m2)
{
fine = true;
return;
}
else if(judgeprime(now) && !vist[now] && now >=1000)//刚开始没有判定now >=1000,答案一直不对
{
vist[now] = true;//如果是素数并大于1000且没有被放过队列里面
q.add(now);
}
}
}
}
}
}
void output()
{
if(fine)
{
System.out.println(ans);
}
else
{
System.out.println(" Impossible");
}
}
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
while(n-->0)
{
int n1 = cin.nextInt();
int n2 = cin.nextInt();
PrimePath pp = new PrimePath();
pp.BFS(n1, n2);
pp.output();
}
}
}
阅读全文
0 0
- poj3126 - Prime Path
- hdu1973 || poj3126 Prime Path
- POJ3126 Prime Path
- POJ3126--Prime Path
- POJ3126 Prime Path
- [POJ3126]Prime Path+BFS
- POJ3126 Prime Path 素数
- Prime Path(poj3126)
- poj3126 Prime Path
- POJ3126--- Prime Path
- POJ3126 Prime Path(bfs)
- POJ3126 Prime Path(BFS)
- POJ3126 - Prime Path
- POJ3126-Prime Path
- poj3126 Prime Path
- poj3126 prime path
- POJ3126 Prime Path
- poj3126 prime path bfs
- 2014年07月11日
- poj1315 Don\'t Get Rooked
- web前端常用代码集合
- poj1426 Find The Multiple
- poj2251 Dungeon Master
- poj3126 Prime Path
- 为什么ESP8266 TCP透传过程会丢包?8266流控原理以及如何设置
- poj 3278 Catch That Cow
- poj2965 The Pilots Brothers\' refrigerator
- poj1328 Radar Installation(java)
- poj2109 Power of Cryptography(java)
- poj2586 Y2K Accounting Bug(贪心,java)
- poj 1573 Robot Motion(java 模拟)
- poj 1125 Stockbroker Grapevine