栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3126 Prime Path

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3126 Prime Path

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>using namespace std;struct item{    int num[4];} a, b;int prime[100], primenum, cost[10000];int toint(item &a){    int ans = 0;    for (int i = 0; i < 4; i++)        ans = ans * 10 + a.num[i];    return ans;}void makeprime(){    primenum = 1;    prime[0] = 2;    for (int i = 3; i < 100; i++)    {        bool ok = true;        for (int j = 0; prime[j] * prime[j] <= i; j++) if (i % prime[j] == 0) {     ok = false;     break; }        if (ok)        { prime[primenum++] = i;        }    }}void init(){    memset(cost, -1, sizeof(cost));    for (int i = 0; i < 4; i++)    {        char ch;        scanf("%c", &ch);        a.num[i] = ch - '0';    }    getchar();    for (int i = 0; i < 4; i++)    {        char ch;        scanf("%c", &ch);        b.num[i] = ch - '0';    }    getchar();}bool isprime(int a){    for (int i = 0; i < primenum && prime[i] * prime[i] <= a; i++)        if (a % prime[i] == 0) return false;    return true;}void work(){    cost[toint(a)] = 0;    queue<item> q;    q.push(a);    while (!q.empty())    {        item temp = q.front();        int costnow = cost[toint(temp)];        q.pop();        for (int i = 0; i < 4; i++)        { int x = temp.num[i]; for (int j = 0; j < 10; j++) {     if (i == 0 && j == 0)         continue;     if (j == x)         continue;     temp.num[i] = j;     int inttemp = toint(temp);     if (cost[inttemp] == -1 && isprime(inttemp))     {         cost[inttemp] = costnow + 1;         if (inttemp == toint(b))         {  printf("%dn", cost[inttemp]);  return;         }         q.push(temp);     } } temp.num[i] = x;        }    }}int main(){    int t;    makeprime();    scanf("%d", &t);    getchar();    while (t--)    {        init();        if (toint(a) == toint(b)) printf("0n");        work();    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369157.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号