POJ3126 Prime Path
http://poj.org/problem?id=3126
1733
3733
3739
3779
8779
8179
数组开大一点,开始数组开小了,结果就出错了
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<string.h>
#define max(a, b)(a > b ? a : b)
#define min(a, b)(a < b ? a : b)
#define INF 0xffffff
#define N 10010 using namespace std; struct node
{
char s[];
int step;
}; char s1[], s2[], s3[];
bool vis[N]; int prime(int n)
{
int i, k = (int)sqrt(n);
for(i = ; i <= k ; i++)
if(n % i == )
return ;
return ;
} int BFS(char s[])
{
queue<node>Q;
node now, next;
int i, j, h, x = ;
memset(vis, false, sizeof(vis));
memset(s3, , sizeof(s3));
for(i = ; i < ; i++)
x = x * + (s[i] - '');
vis[x] = true;
strcpy(now.s, s);
now.step = ;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(strcmp(now.s, s2) == )
return now.step;
for(i = ; i < ; i++)
{
for(j = ; j < ; j++)
{
if((i == && j == ) || now.s[i] == j + '')
continue;
strcpy(s3, now.s);
now.s[i] = j + '';
x = ;
for(h = ; h < ; h++)
x = x * + (now.s[h] - '');
if(!vis[x] && prime(x) == )
{
vis[x] = true;
next.step = now.step + ;
strcpy(next.s, now.s);
// i = 0;
// j = 0;
Q.push(next);
}
strcpy(now.s, s3);
}
}
}
return -;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%s%s", s1, s2);
printf("%d\n", BFS(s1));
}
return ;
}
POJ3126 Prime Path的更多相关文章
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- poj3126 Prime Path(c语言)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...
- POJ3126 Prime Path —— BFS + 素数表
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3126 Prime Path(BFS)
题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- POJ3126——Prime Path
非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ2126——Prime Path(BFS)
Prime Path DescriptionThe ministers of the cabinet were quite upset by the message from the Chief of ...
随机推荐
- HDU 4549 M斐波那契数列(矩阵幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549 题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]. 思路:手算一下可以发现 ...
- web.xml元素介绍
每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了对我们站台的配置设定.web.xml中定义元素有:◆站台的名称和说明◆针对环境参数(Context)做初始化工作◆Servlet的名 ...
- [转] Asp.net Report Viewer 简单实例
原文链接:http://www.aspsnippets.com/Green/Articles/ASPNet-Report-Viewer-control-Tutorial-with-example.as ...
- Brew 编译mod错误Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI
Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI.: Error: ...
- PostgreSQL 8.4.1
PHP100资讯:PostgreSQL 是一种对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大.特性最丰富和最复杂的自由软件数据库系统.它起源于伯克利(BSD)的数据库研究计划,目前是最 ...
- Android常用组件
UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...
- innodb buffer pool
add page to flush list buffer pool中的page,有三种状态: l free: 当前page未被使用 l clean: 当前page被使用,对应于数 ...
- Long Dominoes(ZOJ 2563状压dp)
题意:n*m方格用1*3的方格填充(不能重叠)求有多少种填充方法 分析:先想状态,但想来想去就是觉得不能覆盖所有情况,隔了一天,看看题解,原来要用三进制 0 表示横着放或竖放的最后一行,1表示竖放的中 ...
- 【C#】字符串与字符数组
字符串与字符数组的相互转换. 字符串转换成字符数组: string ss="abcdefg"; char[] cc=ss.ToCharArray(); 字符数组转换成字符串 ...
- oracleasm方式创建ASM
1.准备oracleasm包 [root@localhost oracle]# uname -r 2.6.18-164.el5 [oracle@localhost ~]$ ls -l total 26 ...