poj 3126 Prime Path
来源:互联网 发布:php gtk 还有人维护吗 编辑:程序博客网 时间:2024/06/10 20:59
简单的bfs 。。
#include<stdio.h>#include<queue>#include<math.h>#include<string.h>using namespace std;int judge[10000];int step[10000];void zhishu(){ int i; for(i=1000;i<=9999;i++) { int flag=1; for(int j=2;j<=sqrt(i);j++) { if(i%j==0) { flag=0; break; } } if(flag==1) judge[i]=1; }}int main(){ int T; scanf("%d",&T); memset(judge,0,sizeof(judge)); zhishu(); queue<int>Q; while(T--) { int start,end; scanf("%d%d",&start,&end); if(start==end) { printf("0\n"); continue; } if(judge[start]==0||judge[end]==0) { printf("Impossible\n"); continue; } memset(step,-1,sizeof(step)); step[start]=0; Q.push(start); int bigflag=1; while((!Q.empty())&&bigflag==1) { int old=Q.front(); Q.pop(); for(int i=1;i<=1000&&bigflag==1;i=i*10) for(int j=0;j<=9;j++) { int new1=old%i+old/(i*10)*i*10+j*i; if(judge[new1]==1&&step[new1]==-1) { step[new1]=step[old]+1; if(new1==end) { bigflag=0; printf("%d\n",step[end]); break; } Q.push(new1); } } } while(!Q.empty()) Q.pop(); if(bigflag==1) printf("Impossible\n"); } return 0;}
0 0
- POJ 3126 Prime Path
- POJ 3126 Prime Path
- poj 3126Prime Path
- POJ -3126-Prime Path
- POJ 3126 Prime Path
- poj 3126 Prime Path
- POJ 3126 - Prime Path
- POJ 3126 Prime Path
- poj 3126 prime path
- POJ 3126 Prime Path
- Prime Path poj 3126
- POJ 3126 Prime Path
- poj 3126 Prime Path
- POJ 3126 Prime Path
- poj 3126 Prime path
- poj - 3126 - Prime Path
- POJ 3126 Prime Path
- POJ 3126 Prime Path
- listView或者scrollView中包含gridView或listView的解决方法
- LevelDB简介及入门手册
- 神经网络的学习 机器学习基础(4)
- Java中print、printf、println的区别
- 深入浅出理解Paxos算法
- poj 3126 Prime Path
- 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出, 然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
- markdown学习2:Markdown编辑器之比较
- leetcode笔记--Add Binary
- python 编码问题
- 金额转换,阿拉伯数字转换成中国传统形式。例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整
- HDU 4602 Partition
- CTeX终于要更新了
- 安卓 单例设计模式 查看方法