poj 3126
来源:互联网 发布:淘宝打折软件叫什么 编辑:程序博客网 时间:2024/05/01 23:45
看上去像是一道质数题,实际上就是BFS。。比较水,不多说直接上代码。
#include <stdio.h>#include <queue>#include <string.h>#define INF 10000000using namespace std;int judge[10005],dp[10005];void solution(){ int i,j; judge[0]=1; judge[1]=1; for(i=2;i<=10000;i++){ if(judge[i]==0) for(j=i+i;j<=10000;j+=i){ judge[j]=1; } } return;}int po(int n){ int e=1; if(n==0) return 1; for(int i=0;i<n;i++) e*=10; return e;}int main(){ int n,m,i,j,t; int p,date,charge; memset(judge,0,sizeof(judge)); solution(); //memset(dp,0,sizeof(dp)); scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); queue<int> q; for(i=1000;i<=10000;i++) dp[i]=INF; dp[n]=0; q.push(n); while(q.size()){ p=q.front(); q.pop(); charge=p; if(p==m) break; for(i=1;i<=4;i++){ date=charge%10; charge=charge/10; for(j=0;j<10;j++){ int nx=(p-date*po(i-1))+po(i-1)*j; //printf("nx=%d\n",nx); if(nx>=1000&&nx<=9999&&judge[nx]==0/*&&nx!=n*/&&dp[nx]==INF){ q.push(nx); dp[nx]=dp[p]+1; } } } } //if(dp[m]<INF) printf("%d\n",dp[m]); //else //printf("no\n"); } return 0;}
0 0
- POJ 3126
- poj 3126
- POJ 3126
- POJ 3126
- poj 3126
- poj 3126
- poj 3126
- POJ 3126
- poj 3126
- poj-3126
- poj 3126
- POJ 3126
- poj 3126
- 3126POJ
- poj 3126
- POJ 3126
- poj---3126
- poj 3126
- 面试题整理12 求字符串括号最大深度子串
- 如何在《微软技术社---新闻组,论坛,BBS》上访问自己参与过的话题?
- linux从开机加电到执行main函数之前
- OSGi,Java模块化框架的另类进化
- 验证码客户端验证
- poj 3126
- Confluence 用户属性(User Profile)宏
- Why does a virtual function get hidden?
- unix高级环境编程 例子 代码实现练习 第四章:文件和目录
- 正则表达式30分钟入门教程
- php designer 7註冊碼
- Confluence 用户状态列表(User Status List)宏
- 如何更好地进行性能测试需求分析
- 若初见,若寻常,若是一双人