| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
奇怪的错误。。。在我加注释的那一行,如果不是在bfs中直接输出,而是返回值,就会WA
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 10000;
bool IsPrime[maxn];
int s, e;
bool used[maxn];
struct point {
int x;
int s;
point(int x, int s) : x(x), s(s) {}
};
void sieve(int n) {
for (int i = 0; i <= n; i++)
IsPrime[i] = true;
IsPrime[0] = IsPrime[1] = false;
for (int i = 2; i <= n; i++) {
if (IsPrime[i]) {
for (int j = i * 2; j <= n; j += i)
IsPrime[j] = false;
}
}
}
int bfs() {
queue<point> q;
q.push(point(s, 0));
used[s] = true;
while (!q.empty()) {
point tmp = q.front();
q.pop();
if (tmp.x == e)
cout << tmp.s << endl;//return tmp.s;
for (int i = 1; i <= 1000; i *= 10) {
int num = tmp.x;
num /= i;
num %= 10;
int w = tmp.x - num * i;
for (int j = 0; j < 10; j++) {
if (!(i == 1000 && j == 0)) {
int currNum = w + j * i;
if (currNum != num && IsPrime[currNum] && !used[currNum]) {
q.push(point(currNum, tmp.s + 1));
used[currNum] = true;
}
}
}
}
}
}
int main() {
int n;
cin >> n;
sieve(maxn);
while (n--) {
cin >> s >> e;
memset(used, false, sizeof(used));
bfs();
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator