码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3126 primepath bfs

时间:2016-01-23 00:50:54      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://poj.org/problem?id=3126

题意:1维的坐标轴,给出起点和终点,求从起点到终点变换经历的最短的步数。起点,终点和中间变换的数字都是4位,而且都是质数。

思路:普通的广搜、精神状态不佳、找了许久的bug。后来发现是prime函数和pow函数都很丧心病狂的写错了、

附 AC 代码:

技术分享
  1 // T_T 电脑突然重启什么都没有了。。没有了、
  2 //大概4*9入口的广搜。 从初始点开始 对于每个扩展点加入队列 知道找到ed、或者尝试完所有可能的情况、
  3 
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <iostream>
  7 using namespace std;
  8 #include <math.h>
  9 #define maxn 100000
 10 #include <queue>
 11 int st, ed;
 12 bool vis[10000];
 13 int step[10000];
 14 
 15 int val1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 16 int val2[10] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};
 17 int val3[10] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900};
 18 int val4[10] = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000};
 19 
 20 queue<int>que;
 21 
 22 bool prime(int n) {
 23     for (int i=2; i<=sqrt(n); ++i) {
 24         if (n%i == 0)
 25             return false;
 26     }
 27     return true;
 28 }
 29 
 30 int pow(int a, int b) {
 31     if (b == 0) return 1;
 32     for (int i=1; i<b; ++i) {
 33         a *= 10;
 34     }
 35     return a;
 36 }
 37 
 38 void dfs() {
 39     memset(vis, 0, sizeof(vis));
 40     for (int i=0; i<10000; ++i) {
 41         step[i] = maxn;
 42     }
 43     while(!que.empty())
 44         que.pop();
 45     que.push(st);
 46     step[st] = 0;
 47     vis[st] = 1;
 48 
 49     while(!que.empty()) {
 50         int now = que.front();
 51         que.pop();
 52         if (now == ed) {
 53             return;
 54         }
 55 
 56         int temp = now;
 57         int val[4];
 58         int cnt = 0;
 59         while (temp) {
 60             val[cnt] = temp % 10;
 61             temp /= 10;
 62             val[cnt] *= pow(10, cnt);
 63             cnt++;
 64         }
 65 
 66         temp = now;
 67         temp -= val[0];
 68         for (int i=0; i<10; ++i) {
 69             temp += val1[i];
 70             if (!vis[temp] && temp % 2 && temp <= ed) {
 71                 if (prime(temp)) {
 72                  step[temp] = step[now] + 1;
 73                  vis[temp] = 1;
 74                  que.push(temp);
 75                 }
 76             }
 77             temp -= val1[i];
 78         }
 79 
 80         temp = now;
 81         temp -= val[1];
 82         for (int i=0; i<10; ++i) {
 83             temp += val2[i];
 84             if (!vis[temp] && temp % 2) {
 85                 if (prime(temp)) {
 86                  step[temp] = step[now] + 1;
 87                  vis[temp] = 1;
 88                  que.push(temp);
 89                 }
 90             }
 91             temp -= val2[i];
 92         }
 93 
 94         temp = now;
 95         temp -= val[2];
 96         for (int i=0; i<10; ++i) {
 97             temp += val3[i];
 98             if (!vis[temp] && temp % 2) {
 99                 if (prime(temp)) {
100                  step[temp] = step[now] + 1;
101                  vis[temp] = 1;
102                  que.push(temp);
103                 }
104             }
105             temp -= val3[i];
106         }
107 
108         temp = now;
109         temp -= val[3];
110 
111         for (int i=0; i<9; ++i) {
112             temp += val4[i];
113             if (!vis[temp] && temp % 2) {
114                 if (prime(temp)) {
115                  step[temp] = step[now] + 1;
116                  vis[temp] = 1;
117                  que.push(temp);
118                 }
119             }
120             temp -= val4[i];
121         }
122     }
123     return;
124 }
125 
126 int main() {
127     int t;
128     cin >> t;
129     while(t--) {
130         cin >> st >> ed;
131         dfs();
132         int ans = step[ed];
133         if (ans == maxn) {
134             cout << "Impossible\n";
135         }
136         else cout << ans << endl;
137     }
138     return 0;
139 }
View Code

 

POJ 3126 primepath bfs

标签:

原文地址:http://www.cnblogs.com/icode-girl/p/5152569.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!