素数路径Prime Path POJ-3126 素数,BFS

题目链接:Prime Path

题目大意

从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数。求最终把m变成n所需的最少次数。

思路

BFS。搜索的时候,最低位为0,2,4,6,8可以先排除(偶数不会是素数),最高位是0的也可以排除。这个题判断素数的次数比较少,可以不打素数表。

题解

第二次写的时候代码写的很乱。。没有第一遍干净了

  1 #include <iostream>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 
  6 int num, m, n, ans;
  7 int vis[10005];
  8 int a[10] = {0,1,2,3,4,5,6,7,8,9};
  9 int digit[5];
 10 struct point
 11 {
 12     int val;
 13     int step;
 14 }p;
 15 
 16 void div(int x)        //把数拆分
 17 {
 18     for(int i = 0; i < 4; i++)
 19     {
 20         digit[i] = x % 10;
 21         x = x/10;
 22     }
 23 }
 24 
 25 
 26 bool isPrime(int n)        
 27 {
 28     for(int i = 2; i*i <= n; i++)
 29     {
 30         if(n % i == 0)
 31         {
 32             return false;
 33         }
 34     }
 35     return true;
 36 }
 37 
 38 
 39 int bfs(int m, int n)
 40 {
 41     queue<point> q;
 42     p.val = m;
 43     p.step = 0;
 44     q.push(p);
 45     while(!q.empty())
 46     {
 47         point cur = q.front();
 48         if(cur.val == n)
 49         {
 50             return cur.step;
 51         }
 52         //cout << cur.val << " ";
 53         vis[cur.val] = 1;
 54         div(cur.val);
 55         for(int i = 0; i < 10; i++)
 56         {
 57             if(i == digit[0] || i == 2 || i == 4 || i == 6 || i == 8) continue;
 58             int x = digit[3]*1000 + digit[2]*100 + digit[1]*10 + i;
 59             if(vis[x] || !isPrime(x)) continue;
 60             p.val = x;
 61             p.step = cur.step+1;
 62             q.push(p);
 63         }
 64         for(int i = 0; i < 10; i++)
 65         {
 66             if(i == digit[1]) continue;
 67             int x = digit[3]*1000 + digit[2]*100 + i*10 + digit[0];
 68             if(vis[x] || !isPrime(x)) continue;
 69             p.val = x;
 70             p.step = cur.step+1;
 71             q.push(p);
 72         }
 73 
 74         for(int i = 0; i < 10; i++)
 75         {
 76             if(i == digit[2]) continue;
 77             int x = digit[3]*1000 + i*100 + digit[1]*10 + digit[0];
 78             if(vis[x] || !isPrime(x)) continue;
 79             p.val = x;
 80             p.step = cur.step+1;
 81             q.push(p);
 82         }
 83         for(int i = 1; i < 10; i++)
 84         {
 85             if(i == digit[3]) continue;
 86             int x = i*1000 + digit[2]*100 + digit[1]*10 + digit[0];
 87             if(vis[x]|| !isPrime(x)) continue;
 88             p.val = x;
 89             p.step = cur.step+1;
 90             q.push(p);
 91         }
 92         q.pop();
 93     }
 94     return -1;            //没有找到
 95 }
 96 
 97 
 98 int main(int argc, char const *argv[])
 99 {
100     #ifdef debug
101         freopen("test.txt","r",stdin);
102     #endif
103     cin >> num;
104     while(num--)
105     {
106         memset(vis, 0 ,sizeof(vis));
107         cin >> m >> n;
108         ans = bfs(m, n);
109         if (ans == -1)
110         {
111             cout << "Impossible" << endl;
112             continue;
113         }
114         cout << ans << endl;
115     }
116 }

 

转载于:https://www.cnblogs.com/SaltyFishQF/p/10299645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值