HDU1973 http://acm.hdu.edu.cn/showproblem.php?pid=1973
2015-07-16 10:34
561 查看
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<queue> #include<math.h> #define N 10010 using namespace std; int vis ; char s1[5], s2[5], s3[5]; struct node { int step; char st[5]; }; int prime(int y) { int i, k = (int)sqrt(y); for(i = 2 ; i <= k ; i++) if(y % i == 0) return 0; return 1; } int BFS(char s[]) { queue<node>Q; node now, next; int i, j, h, x = 0; memset(vis, 0, sizeof(vis)); memset(s3, 0, sizeof(s3)); for(i = 0 ; i < 4 ; i++) x = x * 10 + (s[i] - '0'); vis[x] = 1; strcpy(now.st, s); now.step = 0; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); if(strcmp(now.st, s2) == 0) return now.step; for(i = 0 ; i < 4 ; i++)//各个位(即个位,十位,百位,千位) { for(j = 0 ; j < 10 ; j++)//0到9十个数 { if(i == 0 && j == 0)//千位不为0 continue; if(now.st[i] == j + '0')//原有的数不需要替换 continue; strcpy(s3, now.st); now.st[i] = j + '0';//0到9其中的一个数去替换四位数气中的一位 x = 0; for(h = 0 ; h < 4 ; h++) x = x * 10 + (now.st[h] - '0'); if(prime(x) == 1 && !vis[x]) { vis[x] = 1; next.step = now.step + 1; strcpy(next.st, now.st); i = 0;/***/ j = 0;/***/ Q.push(next); } strcpy(now.st, s3); } } } return -1; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%s%s", s1, s2); printf("%d\n", BFS(s1)); } return 0; }
相关文章推荐
- 一个手写的 http client
- 【Linux 内核网络协议栈源码剖析】sendto 函数剖析
- iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
- 使用mina解析http协议的使用
- iOS开发工具-网络封包分析工具Charles
- 【Android小练手】安卓端实时获取HttpUrl数据
- dhroid - NetJSONAdapter 网络化的adapter
- 2014中国网络性能报告
- dhroid - DhNet 网络http工具
- 【源码剖析】tinyhttpd —— C 语言实现最简单的 HTTP 服务器
- HTTP协议详解
- TCP同步和异步连接_学习笔记
- FPGA实现网络通信时的网络字节序问题
- 巧用Android网络通信技术,在网络上直接传输对象
- 基于java的http服务器
- 微软“小冰”识狗与人工神经网络(I)
- HTTP 笔记与总结(6)referer 头与防盗链
- 企业社交网络平台与传统OA办公系统之间的较量
- 第一部分:HTTP协议之请求和响应
- BZOJ 1475: 方格取数( 网络流 )