POJ 3126:Prime Path

来源:互联网 发布:旅游网络市场调查 编辑:程序博客网 时间:2024/06/04 00:36

Prime Path
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

Submit Status

Description

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. 
— It is a matter of security to change such things every now and then, to keep the enemy in the dark. 
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know! 
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door. 
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime! 
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds. 
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime. 

Now, the minister of finance, who had been eavesdropping, intervened. 
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound. 
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you? 
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above. 
1033 
1733 
3733 
3739 
3779 
8779 
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

31033 81791373 80171033 1033

Sample Output

670

题意是给两个质数a和b,每次只能变a中的一位,且变完后的a也要是一个质数,问经过多少次变换能变成b。

广度优先搜索很典型了,只是还是觉得自己这样做比较麻烦,把a每一次能到达的数a‘弄到queue里面。只是一开始a'的判断TLE了,觉得是每次判断是不是质数浪费了时间。所以把从1000到10000的质数全都列了出来,这样不用一个一个判断了,在里面就是质数,不在里面就out。

我觉得其他人肯定有更好的方法,自己还是要好好学习。

代码:

#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <string>#include <queue>#include <cstring>#pragma warning(disable:4996)using namespace std;queue <int> q;int ans[10005];int a[1065];bool pend(int b){int i;for(i=0;i<=1060;i++){if(b==a[i])return true;}return false;}int main(){a[0]=1009;a[1]=1013;a[2]=1019;a[3]=1021;a[4]=1031;a[5]=1033;a[6]=1039;a[7]=1049;a[8]=1051;a[9]=1061;a[10]=1063;a[11]=1069;a[12]=1087;a[13]=1091;a[14]=1093;a[15]=1097;a[16]=1103;a[17]=1109;a[18]=1117;a[19]=1123;a[20]=1129;a[21]=1151;a[22]=1153;a[23]=1163;a[24]=1171;a[25]=1181;a[26]=1187;a[27]=1193;a[28]=1201;a[29]=1213;a[30]=1217;a[31]=1223;a[32]=1229;a[33]=1231;a[34]=1237;a[35]=1249;a[36]=1259;a[37]=1277;a[38]=1279;a[39]=1283;a[40]=1289;a[41]=1291;a[42]=1297;a[43]=1301;a[44]=1303;a[45]=1307;a[46]=1319;a[47]=1321;a[48]=1327;a[49]=1361;a[50]=1367;a[51]=1373;a[52]=1381;a[53]=1399;a[54]=1409;a[55]=1423;a[56]=1427;a[57]=1429;a[58]=1433;a[59]=1439;a[60]=1447;a[61]=1451;a[62]=1453;a[63]=1459;a[64]=1471;a[65]=1481;a[66]=1483;a[67]=1487;a[68]=1489;a[69]=1493;a[70]=1499;a[71]=1511;a[72]=1523;a[73]=1531;a[74]=1543;a[75]=1549;a[76]=1553;a[77]=1559;a[78]=1567;a[79]=1571;a[80]=1579;a[81]=1583;a[82]=1597;a[83]=1601;a[84]=1607;a[85]=1609;a[86]=1613;a[87]=1619;a[88]=1621;a[89]=1627;a[90]=1637;a[91]=1657;a[92]=1663;a[93]=1667;a[94]=1669;a[95]=1693;a[96]=1697;a[97]=1699;a[98]=1709;a[99]=1721;a[100]=1723;a[101]=1733;a[102]=1741;a[103]=1747;a[104]=1753;a[105]=1759;a[106]=1777;a[107]=1783;a[108]=1787;a[109]=1789;a[110]=1801;a[111]=1811;a[112]=1823;a[113]=1831;a[114]=1847;a[115]=1861;a[116]=1867;a[117]=1871;a[118]=1873;a[119]=1877;a[120]=1879;a[121]=1889;a[122]=1901;a[123]=1907;a[124]=1913;a[125]=1931;a[126]=1933;a[127]=1949;a[128]=1951;a[129]=1973;a[130]=1979;a[131]=1987;a[132]=1993;a[133]=1997;a[134]=1999;a[135]=2003;a[136]=2011;a[137]=2017;a[138]=2027;a[139]=2029;a[140]=2039;a[141]=2053;a[142]=2063;a[143]=2069;a[144]=2081;a[145]=2083;a[146]=2087;a[147]=2089;a[148]=2099;a[149]=2111;a[150]=2113;a[151]=2129;a[152]=2131;a[153]=2137;a[154]=2141;a[155]=2143;a[156]=2153;a[157]=2161;a[158]=2179;a[159]=2203;a[160]=2207;a[161]=2213;a[162]=2221;a[163]=2237;a[164]=2239;a[165]=2243;a[166]=2251;a[167]=2267;a[168]=2269;a[169]=2273;a[170]=2281;a[171]=2287;a[172]=2293;a[173]=2297;a[174]=2309;a[175]=2311;a[176]=2333;a[177]=2339;a[178]=2341;a[179]=2347;a[180]=2351;a[181]=2357;a[182]=2371;a[183]=2377;a[184]=2381;a[185]=2383;a[186]=2389;a[187]=2393;a[188]=2399;a[189]=2411;a[190]=2417;a[191]=2423;a[192]=2437;a[193]=2441;a[194]=2447;a[195]=2459;a[196]=2467;a[197]=2473;a[198]=2477;a[199]=2503;a[200]=2521;a[201]=2531;a[202]=2539;a[203]=2543;a[204]=2549;a[205]=2551;a[206]=2557;a[207]=2579;a[208]=2591;a[209]=2593;a[210]=2609;a[211]=2617;a[212]=2621;a[213]=2633;a[214]=2647;a[215]=2657;a[216]=2659;a[217]=2663;a[218]=2671;a[219]=2677;a[220]=2683;a[221]=2687;a[222]=2689;a[223]=2693;a[224]=2699;a[225]=2707;a[226]=2711;a[227]=2713;a[228]=2719;a[229]=2729;a[230]=2731;a[231]=2741;a[232]=2749;a[233]=2753;a[234]=2767;a[235]=2777;a[236]=2789;a[237]=2791;a[238]=2797;a[239]=2801;a[240]=2803;a[241]=2819;a[242]=2833;a[243]=2837;a[244]=2843;a[245]=2851;a[246]=2857;a[247]=2861;a[248]=2879;a[249]=2887;a[250]=2897;a[251]=2903;a[252]=2909;a[253]=2917;a[254]=2927;a[255]=2939;a[256]=2953;a[257]=2957;a[258]=2963;a[259]=2969;a[260]=2971;a[261]=2999;a[262]=3001;a[263]=3011;a[264]=3019;a[265]=3023;a[266]=3037;a[267]=3041;a[268]=3049;a[269]=3061;a[270]=3067;a[271]=3079;a[272]=3083;a[273]=3089;a[274]=3109;a[275]=3119;a[276]=3121;a[277]=3137;a[278]=3163;a[279]=3167;a[280]=3169;a[281]=3181;a[282]=3187;a[283]=3191;a[284]=3203;a[285]=3209;a[286]=3217;a[287]=3221;a[288]=3229;a[289]=3251;a[290]=3253;a[291]=3257;a[292]=3259;a[293]=3271;a[294]=3299;a[295]=3301;a[296]=3307;a[297]=3313;a[298]=3319;a[299]=3323;a[300]=3329;a[301]=3331;a[302]=3343;a[303]=3347;a[304]=3359;a[305]=3361;a[306]=3371;a[307]=3373;a[308]=3389;a[309]=3391;a[310]=3407;a[311]=3413;a[312]=3433;a[313]=3449;a[314]=3457;a[315]=3461;a[316]=3463;a[317]=3467;a[318]=3469;a[319]=3491;a[320]=3499;a[321]=3511;a[322]=3517;a[323]=3527;a[324]=3529;a[325]=3533;a[326]=3539;a[327]=3541;a[328]=3547;a[329]=3557;a[330]=3559;a[331]=3571;a[332]=3581;a[333]=3583;a[334]=3593;a[335]=3607;a[336]=3613;a[337]=3617;a[338]=3623;a[339]=3631;a[340]=3637;a[341]=3643;a[342]=3659;a[343]=3671;a[344]=3673;a[345]=3677;a[346]=3691;a[347]=3697;a[348]=3701;a[349]=3709;a[350]=3719;a[351]=3727;a[352]=3733;a[353]=3739;a[354]=3761;a[355]=3767;a[356]=3769;a[357]=3779;a[358]=3793;a[359]=3797;a[360]=3803;a[361]=3821;a[362]=3823;a[363]=3833;a[364]=3847;a[365]=3851;a[366]=3853;a[367]=3863;a[368]=3877;a[369]=3881;a[370]=3889;a[371]=3907;a[372]=3911;a[373]=3917;a[374]=3919;a[375]=3923;a[376]=3929;a[377]=3931;a[378]=3943;a[379]=3947;a[380]=3967;a[381]=3989;a[382]=4001;a[383]=4003;a[384]=4007;a[385]=4013;a[386]=4019;a[387]=4021;a[388]=4027;a[389]=4049;a[390]=4051;a[391]=4057;a[392]=4073;a[393]=4079;a[394]=4091;a[395]=4093;a[396]=4099;a[397]=4111;a[398]=4127;a[399]=4129;a[400]=4133;a[401]=4139;a[402]=4153;a[403]=4157;a[404]=4159;a[405]=4177;a[406]=4201;a[407]=4211;a[408]=4217;a[409]=4219;a[410]=4229;a[411]=4231;a[412]=4241;a[413]=4243;a[414]=4253;a[415]=4259;a[416]=4261;a[417]=4271;a[418]=4273;a[419]=4283;a[420]=4289;a[421]=4297;a[422]=4327;a[423]=4337;a[424]=4339;a[425]=4349;a[426]=4357;a[427]=4363;a[428]=4373;a[429]=4391;a[430]=4397;a[431]=4409;a[432]=4421;a[433]=4423;a[434]=4441;a[435]=4447;a[436]=4451;a[437]=4457;a[438]=4463;a[439]=4481;a[440]=4483;a[441]=4493;a[442]=4507;a[443]=4513;a[444]=4517;a[445]=4519;a[446]=4523;a[447]=4547;a[448]=4549;a[449]=4561;a[450]=4567;a[451]=4583;a[452]=4591;a[453]=4597;a[454]=4603;a[455]=4621;a[456]=4637;a[457]=4639;a[458]=4643;a[459]=4649;a[460]=4651;a[461]=4657;a[462]=4663;a[463]=4673;a[464]=4679;a[465]=4691;a[466]=4703;a[467]=4721;a[468]=4723;a[469]=4729;a[470]=4733;a[471]=4751;a[472]=4759;a[473]=4783;a[474]=4787;a[475]=4789;a[476]=4793;a[477]=4799;a[478]=4801;a[479]=4813;a[480]=4817;a[481]=4831;a[482]=4861;a[483]=4871;a[484]=4877;a[485]=4889;a[486]=4903;a[487]=4909;a[488]=4919;a[489]=4931;a[490]=4933;a[491]=4937;a[492]=4943;a[493]=4951;a[494]=4957;a[495]=4967;a[496]=4969;a[497]=4973;a[498]=4987;a[499]=4993;a[500]=4999;a[501]=5003;a[502]=5009;a[503]=5011;a[504]=5021;a[505]=5023;a[506]=5039;a[507]=5051;a[508]=5059;a[509]=5077;a[510]=5081;a[511]=5087;a[512]=5099;a[513]=5101;a[514]=5107;a[515]=5113;a[516]=5119;a[517]=5147;a[518]=5153;a[519]=5167;a[520]=5171;a[521]=5179;a[522]=5189;a[523]=5197;a[524]=5209;a[525]=5227;a[526]=5231;a[527]=5233;a[528]=5237;a[529]=5261;a[530]=5273;a[531]=5279;a[532]=5281;a[533]=5297;a[534]=5303;a[535]=5309;a[536]=5323;a[537]=5333;a[538]=5347;a[539]=5351;a[540]=5381;a[541]=5387;a[542]=5393;a[543]=5399;a[544]=5407;a[545]=5413;a[546]=5417;a[547]=5419;a[548]=5431;a[549]=5437;a[550]=5441;a[551]=5443;a[552]=5449;a[553]=5471;a[554]=5477;a[555]=5479;a[556]=5483;a[557]=5501;a[558]=5503;a[559]=5507;a[560]=5519;a[561]=5521;a[562]=5527;a[563]=5531;a[564]=5557;a[565]=5563;a[566]=5569;a[567]=5573;a[568]=5581;a[569]=5591;a[570]=5623;a[571]=5639;a[572]=5641;a[573]=5647;a[574]=5651;a[575]=5653;a[576]=5657;a[577]=5659;a[578]=5669;a[579]=5683;a[580]=5689;a[581]=5693;a[582]=5701;a[583]=5711;a[584]=5717;a[585]=5737;a[586]=5741;a[587]=5743;a[588]=5749;a[589]=5779;a[590]=5783;a[591]=5791;a[592]=5801;a[593]=5807;a[594]=5813;a[595]=5821;a[596]=5827;a[597]=5839;a[598]=5843;a[599]=5849;a[600]=5851;a[601]=5857;a[602]=5861;a[603]=5867;a[604]=5869;a[605]=5879;a[606]=5881;a[607]=5897;a[608]=5903;a[609]=5923;a[610]=5927;a[611]=5939;a[612]=5953;a[613]=5981;a[614]=5987;a[615]=6007;a[616]=6011;a[617]=6029;a[618]=6037;a[619]=6043;a[620]=6047;a[621]=6053;a[622]=6067;a[623]=6073;a[624]=6079;a[625]=6089;a[626]=6091;a[627]=6101;a[628]=6113;a[629]=6121;a[630]=6131;a[631]=6133;a[632]=6143;a[633]=6151;a[634]=6163;a[635]=6173;a[636]=6197;a[637]=6199;a[638]=6203;a[639]=6211;a[640]=6217;a[641]=6221;a[642]=6229;a[643]=6247;a[644]=6257;a[645]=6263;a[646]=6269;a[647]=6271;a[648]=6277;a[649]=6287;a[650]=6299;a[651]=6301;a[652]=6311;a[653]=6317;a[654]=6323;a[655]=6329;a[656]=6337;a[657]=6343;a[658]=6353;a[659]=6359;a[660]=6361;a[661]=6367;a[662]=6373;a[663]=6379;a[664]=6389;a[665]=6397;a[666]=6421;a[667]=6427;a[668]=6449;a[669]=6451;a[670]=6469;a[671]=6473;a[672]=6481;a[673]=6491;a[674]=6521;a[675]=6529;a[676]=6547;a[677]=6551;a[678]=6553;a[679]=6563;a[680]=6569;a[681]=6571;a[682]=6577;a[683]=6581;a[684]=6599;a[685]=6607;a[686]=6619;a[687]=6637;a[688]=6653;a[689]=6659;a[690]=6661;a[691]=6673;a[692]=6679;a[693]=6689;a[694]=6691;a[695]=6701;a[696]=6703;a[697]=6709;a[698]=6719;a[699]=6733;a[700]=6737;a[701]=6761;a[702]=6763;a[703]=6779;a[704]=6781;a[705]=6791;a[706]=6793;a[707]=6803;a[708]=6823;a[709]=6827;a[710]=6829;a[711]=6833;a[712]=6841;a[713]=6857;a[714]=6863;a[715]=6869;a[716]=6871;a[717]=6883;a[718]=6899;a[719]=6907;a[720]=6911;a[721]=6917;a[722]=6947;a[723]=6949;a[724]=6959;a[725]=6961;a[726]=6967;a[727]=6971;a[728]=6977;a[729]=6983;a[730]=6991;a[731]=6997;a[732]=7001;a[733]=7013;a[734]=7019;a[735]=7027;a[736]=7039;a[737]=7043;a[738]=7057;a[739]=7069;a[740]=7079;a[741]=7103;a[742]=7109;a[743]=7121;a[744]=7127;a[745]=7129;a[746]=7151;a[747]=7159;a[748]=7177;a[749]=7187;a[750]=7193;a[751]=7207;a[752]=7211;a[753]=7213;a[754]=7219;a[755]=7229;a[756]=7237;a[757]=7243;a[758]=7247;a[759]=7253;a[760]=7283;a[761]=7297;a[762]=7307;a[763]=7309;a[764]=7321;a[765]=7331;a[766]=7333;a[767]=7349;a[768]=7351;a[769]=7369;a[770]=7393;a[771]=7411;a[772]=7417;a[773]=7433;a[774]=7451;a[775]=7457;a[776]=7459;a[777]=7477;a[778]=7481;a[779]=7487;a[780]=7489;a[781]=7499;a[782]=7507;a[783]=7517;a[784]=7523;a[785]=7529;a[786]=7537;a[787]=7541;a[788]=7547;a[789]=7549;a[790]=7559;a[791]=7561;a[792]=7573;a[793]=7577;a[794]=7583;a[795]=7589;a[796]=7591;a[797]=7603;a[798]=7607;a[799]=7621;a[800]=7639;a[801]=7643;a[802]=7649;a[803]=7669;a[804]=7673;a[805]=7681;a[806]=7687;a[807]=7691;a[808]=7699;a[809]=7703;a[810]=7717;a[811]=7723;a[812]=7727;a[813]=7741;a[814]=7753;a[815]=7757;a[816]=7759;a[817]=7789;a[818]=7793;a[819]=7817;a[820]=7823;a[821]=7829;a[822]=7841;a[823]=7853;a[824]=7867;a[825]=7873;a[826]=7877;a[827]=7879;a[828]=7883;a[829]=7901;a[830]=7907;a[831]=7919;a[832]=7927;a[833]=7933;a[834]=7937;a[835]=7949;a[836]=7951;a[837]=7963;a[838]=7993;a[839]=8009;a[840]=8011;a[841]=8017;a[842]=8039;a[843]=8053;a[844]=8059;a[845]=8069;a[846]=8081;a[847]=8087;a[848]=8089;a[849]=8093;a[850]=8101;a[851]=8111;a[852]=8117;a[853]=8123;a[854]=8147;a[855]=8161;a[856]=8167;a[857]=8171;a[858]=8179;a[859]=8191;a[860]=8209;a[861]=8219;a[862]=8221;a[863]=8231;a[864]=8233;a[865]=8237;a[866]=8243;a[867]=8263;a[868]=8269;a[869]=8273;a[870]=8287;a[871]=8291;a[872]=8293;a[873]=8297;a[874]=8311;a[875]=8317;a[876]=8329;a[877]=8353;a[878]=8363;a[879]=8369;a[880]=8377;a[881]=8387;a[882]=8389;a[883]=8419;a[884]=8423;a[885]=8429;a[886]=8431;a[887]=8443;a[888]=8447;a[889]=8461;a[890]=8467;a[891]=8501;a[892]=8513;a[893]=8521;a[894]=8527;a[895]=8537;a[896]=8539;a[897]=8543;a[898]=8563;a[899]=8573;a[900]=8581;a[901]=8597;a[902]=8599;a[903]=8609;a[904]=8623;a[905]=8627;a[906]=8629;a[907]=8641;a[908]=8647;a[909]=8663;a[910]=8669;a[911]=8677;a[912]=8681;a[913]=8689;a[914]=8693;a[915]=8699;a[916]=8707;a[917]=8713;a[918]=8719;a[919]=8731;a[920]=8737;a[921]=8741;a[922]=8747;a[923]=8753;a[924]=8761;a[925]=8779;a[926]=8783;a[927]=8803;a[928]=8807;a[929]=8819;a[930]=8821;a[931]=8831;a[932]=8837;a[933]=8839;a[934]=8849;a[935]=8861;a[936]=8863;a[937]=8867;a[938]=8887;a[939]=8893;a[940]=8923;a[941]=8929;a[942]=8933;a[943]=8941;a[944]=8951;a[945]=8963;a[946]=8969;a[947]=8971;a[948]=8999;a[949]=9001;a[950]=9007;a[951]=9011;a[952]=9013;a[953]=9029;a[954]=9041;a[955]=9043;a[956]=9049;a[957]=9059;a[958]=9067;a[959]=9091;a[960]=9103;a[961]=9109;a[962]=9127;a[963]=9133;a[964]=9137;a[965]=9151;a[966]=9157;a[967]=9161;a[968]=9173;a[969]=9181;a[970]=9187;a[971]=9199;a[972]=9203;a[973]=9209;a[974]=9221;a[975]=9227;a[976]=9239;a[977]=9241;a[978]=9257;a[979]=9277;a[980]=9281;a[981]=9283;a[982]=9293;a[983]=9311;a[984]=9319;a[985]=9323;a[986]=9337;a[987]=9341;a[988]=9343;a[989]=9349;a[990]=9371;a[991]=9377;a[992]=9391;a[993]=9397;a[994]=9403;a[995]=9413;a[996]=9419;a[997]=9421;a[998]=9431;a[999]=9433;a[1000]=9437;a[1001]=9439;a[1002]=9461;a[1003]=9463;a[1004]=9467;a[1005]=9473;a[1006]=9479;a[1007]=9491;a[1008]=9497;a[1009]=9511;a[1010]=9521;a[1011]=9533;a[1012]=9539;a[1013]=9547;a[1014]=9551;a[1015]=9587;a[1016]=9601;a[1017]=9613;a[1018]=9619;a[1019]=9623;a[1020]=9629;a[1021]=9631;a[1022]=9643;a[1023]=9649;a[1024]=9661;a[1025]=9677;a[1026]=9679;a[1027]=9689;a[1028]=9697;a[1029]=9719;a[1030]=9721;a[1031]=9733;a[1032]=9739;a[1033]=9743;a[1034]=9749;a[1035]=9767;a[1036]=9769;a[1037]=9781;a[1038]=9787;a[1039]=9791;a[1040]=9803;a[1041]=9811;a[1042]=9817;a[1043]=9829;a[1044]=9833;a[1045]=9839;a[1046]=9851;a[1047]=9857;a[1048]=9859;a[1049]=9871;a[1050]=9883;a[1051]=9887;a[1052]=9901;a[1053]=9907;a[1054]=9923;a[1055]=9929;a[1056]=9931;a[1057]=9941;a[1058]=9949;a[1059]=9967;a[1060]=9973;int Test,temp,dis,wei1,wei2,wei3,wei4,i,k;cin>>Test;while(Test--){while(q.size())q.pop();cin>>temp>>dis;q.push(temp);memset(ans,0,sizeof(ans));while(1){temp=q.front();q.pop();if(temp==dis){cout<<ans[temp]<<endl;break;}wei1=temp%10;wei2=(temp%100-wei1)/10;wei4=temp/1000;wei3=temp/100-wei4*10;for(i=1;i<=9;i=i+2){k=wei4*1000+wei3*100+wei2*10+i;if(wei1!=i &&!ans[k]&& pend(k)){ans[k]=ans[temp]+1;q.push(k);}}for(i=0;i<=9;i++){k=wei4*1000+wei3*100+i*10+wei1;if(wei2!=i &&!ans[k]&& pend(k)){ans[k]=ans[temp]+1;q.push(k);}}for(i=0;i<=9;i++){k=wei4*1000+i*100+wei2*10+wei1;if(wei3!=i &&!ans[k]&& pend(k)){ans[k]=ans[temp]+1;q.push(k);}}for(i=1;i<=9;i++){k=i*1000+wei3*100+wei2*10+wei1;if(wei4!=i &&!ans[k]&& pend(k)){ans[k]=ans[temp]+1;q.push(k);}}}}return 0;}



0 0
原创粉丝点击